-
Notifications
You must be signed in to change notification settings - Fork 0
/
FRServer.m
138 lines (87 loc) · 5.16 KB
/
FRServer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// Server.m
// Server
//
// Created by Frederik Riedel on 03.12.14.
// Copyright (c) 2014 Frederik Riedel. All rights reserved.
//
#import "FRServer.h"
@implementation FRServer
+(void) dataFromURL:(NSString *) url HTTPMethod:(NSString *) HTTPMethod attributes:(NSDictionary *) attributes HTTPHeaderFieldDictionary:(NSDictionary *) HTTPHeaderFields andCallbackBlock: (void (^) (NSData *data)) block {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:HTTPMethod];
if(HTTPHeaderFields) {
for(NSString *key in HTTPHeaderFields) {
[request setValue:[HTTPHeaderFields valueForKey:key] forHTTPHeaderField:key];
}
NSLog(@"%@",HTTPHeaderFields);
}
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
if(attributes) {
NSString *attributesString=@"";
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:attributes
options:NSJSONWritingPrettyPrinted
error:&error];
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
attributesString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
NSLog(@"%@",attributesString);
request.HTTPBody = [attributesString dataUsingEncoding:NSUTF8StringEncoding];;
}
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"%@",response);
dispatch_async(dispatch_get_main_queue(), ^{
block(responseData);
});
});
}
+(void) imageFromURL:(NSString *)url HTTPMethod:(NSString *)HTTPMethod attributes:(NSDictionary *)attributes HTTPHeaderFieldDictionary:(NSDictionary *)HTTPHeaderFields andCallbackBlock:(void (^)(UIImage *))block {
void (^finishedDownloading)(NSData *data) = ^void(NSData *data) {
if(data) {
block([UIImage imageWithData:data]);
} else {
block(nil);
}
};
[FRServer dataFromURL:url HTTPMethod:HTTPMethod attributes:attributes HTTPHeaderFieldDictionary:HTTPHeaderFields andCallbackBlock:finishedDownloading];
}
+(void) jsonFromURL:(NSString *)url HTTPMethod:(NSString *)HTTPMethod attributes:(NSDictionary *)attributes HTTPHeaderFieldDictionary:(NSDictionary *)HTTPHeaderFields andCallbackBlock:(void (^)(NSDictionary *))block {
void (^finishedDownloading)(NSData *data) = ^void(NSData *data) {
if(data) {
block([NSJSONSerialization JSONObjectWithData:data options:0 error:nil]);
} else {
block(nil);
}
};
[FRServer dataFromURL:url HTTPMethod:HTTPMethod attributes:attributes HTTPHeaderFieldDictionary:HTTPHeaderFields andCallbackBlock:finishedDownloading];
}
+(void) stringFromURL:(NSString *)url HTTPMethod:(NSString *)HTTPMethod attributes:(NSDictionary *)attributes HTTPHeaderFieldDictionary:(NSDictionary *)HTTPHeaderFields andCallbackBlock:(void (^)(NSString *))block {
void (^finishedDownloading)(NSData *data) = ^void(NSData *data) {
if(data) {
NSString *string = [ [ NSString alloc ] initWithData:data encoding:NSUTF8StringEncoding ];
block(string);
} else {
block(nil);
}
};
[FRServer dataFromURL:url HTTPMethod:HTTPMethod attributes:attributes HTTPHeaderFieldDictionary:HTTPHeaderFields andCallbackBlock:finishedDownloading];
}
+(void) dataFromServerRequest:(FRServerRequest *)serverRequest {
[FRServer dataFromURL:serverRequest.url HTTPMethod:serverRequest.HTTPMethod attributes:serverRequest.attributes HTTPHeaderFieldDictionary:serverRequest.HTTPHeaderFields andCallbackBlock:serverRequest.block];
}
+(void) stringFromServerRequest:(FRServerRequest *)serverRequest {
[FRServer stringFromURL:serverRequest.url HTTPMethod:serverRequest.HTTPMethod attributes:serverRequest.attributes HTTPHeaderFieldDictionary:serverRequest.HTTPHeaderFields andCallbackBlock:serverRequest.block];
}
+(void) imageFromServerRequest:(FRServerRequest *)serverRequest {
[FRServer imageFromURL:serverRequest.url HTTPMethod:serverRequest.HTTPMethod attributes:serverRequest.attributes HTTPHeaderFieldDictionary:serverRequest.HTTPHeaderFields andCallbackBlock:serverRequest.block];
}
+(void) jsonFromServerRequest:(FRServerRequest *)serverRequest {
[FRServer jsonFromURL:serverRequest.url HTTPMethod:serverRequest.HTTPMethod attributes:serverRequest.attributes HTTPHeaderFieldDictionary:serverRequest.HTTPHeaderFields andCallbackBlock:serverRequest.block];
}
@end