-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIQWebService.m
178 lines (142 loc) · 7.76 KB
/
IQWebService.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// Created by Mustafa Murabbi on 5/21/14.
// Copyright (c) 2014 Mustafa Murabbi. All rights reserved.
//
#import "IQWebService.h"
#import "IQURLConnection.h"
@implementation IQWebService
@synthesize logEnabled = _logEnabled;
+(instancetype)service
{
static IQWebService *sharedService;
if (sharedService == nil)
{
sharedService = [[self alloc] init];
}
return sharedService;
}
-(BOOL)isLogEnabled
{
return _logEnabled;
}
+(NSString*)httpParameterString:(NSDictionary*)dictionary
{
NSMutableString *parameterString = [[NSMutableString alloc] init];
for (NSString *aKey in [dictionary allKeys])
{
[parameterString appendFormat:@"%@=%@&",aKey,[dictionary objectForKey:aKey]];
}
if ([parameterString length])
{
[parameterString replaceCharactersInRange:NSMakeRange(parameterString.length-2, 1) withString:@""];
}
return parameterString;
}
+(NSMutableURLRequest*)requestWithURL:(NSURL*)url httpMethod:(NSString*)httpMethod contentType:(NSString*)contentType body:(NSData*)body
{
if (url != nil)
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:300];
if ([httpMethod length]) [request setHTTPMethod:httpMethod];
if ([contentType length]) [request addValue:contentType forHTTPHeaderField:kIQContentType];
if ([body length])
{
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%d",[body length]] forHTTPHeaderField:kIQContentLength];
}
return request;
}
else
{
return nil;
}
}
//Simple request
-(void)requestWithPath:(NSString*)path httpMethod:(NSString*)method parameter:(NSDictionary*)parameter completionHandler:(IQDictionaryCompletionBlock)completionHandler
{
[self requestWithPath:path httpMethod:method parameter:parameter responseBlock:nil uploadProgressBlock:nil downloadProgressBlock:nil completionHandler:completionHandler];
}
//Download Upload Progress request
-(void)requestWithPath:(NSString*)path httpMethod:(NSString*)method parameter:(NSDictionary*)parameter downloadProgressBlock:(IQProgressBlock)downloadProgress completionHandler:(IQDictionaryCompletionBlock)completionHandler;
{
[self requestWithPath:path httpMethod:method parameter:parameter responseBlock:nil uploadProgressBlock:nil downloadProgressBlock:downloadProgress completionHandler:completionHandler];
}
-(void)requestWithPath:(NSString*)path httpMethod:(NSString*)method parameter:(NSDictionary*)parameter uploadProgressBlock:(IQProgressBlock)uploadProgress completionHandler:(IQDictionaryCompletionBlock)completionHandler;
{
[self requestWithPath:path httpMethod:method parameter:parameter responseBlock:nil uploadProgressBlock:uploadProgress downloadProgressBlock:nil completionHandler:completionHandler];
}
-(void)requestWithPath:(NSString*)path httpMethod:(NSString*)method parameter:(NSDictionary*)parameter uploadProgressBlock:(IQProgressBlock)uploadProgress downloadProgressBlock:(IQProgressBlock)downloadProgress completionHandler:(IQDictionaryCompletionBlock)completionHandler;
{
[self requestWithPath:path httpMethod:method parameter:parameter responseBlock:nil uploadProgressBlock:uploadProgress downloadProgressBlock:downloadProgress completionHandler:completionHandler];
}
//Simple request + HTTPResponse
-(void)requestWithPath:(NSString*)path httpMethod:(NSString*)method parameter:(NSDictionary*)parameter responseBlock:(IQResponseBlock)response completionHandler:(IQDictionaryCompletionBlock)completionHandler;
{
[self requestWithPath:path httpMethod:method parameter:parameter responseBlock:response uploadProgressBlock:nil downloadProgressBlock:nil completionHandler:completionHandler];
}
//Download Upload Progress request + HTTPResponse
-(void)requestWithPath:(NSString*)path httpMethod:(NSString*)method parameter:(NSDictionary*)parameter responseBlock:(IQResponseBlock)response downloadProgressBlock:(IQProgressBlock)downloadProgress completionHandler:(IQDictionaryCompletionBlock)completionHandler;
{
[self requestWithPath:path httpMethod:method parameter:parameter responseBlock:response uploadProgressBlock:downloadProgress downloadProgressBlock:nil completionHandler:completionHandler];
}
-(void)requestWithPath:(NSString*)path httpMethod:(NSString*)method parameter:(NSDictionary*)parameter responseBlock:(IQResponseBlock)response uploadProgressBlock:(IQProgressBlock)uploadProgress completionHandler:(IQDictionaryCompletionBlock)completionHandler;
{
[self requestWithPath:path httpMethod:method parameter:parameter responseBlock:response uploadProgressBlock:uploadProgress downloadProgressBlock:nil completionHandler:completionHandler];
}
-(void)requestWithPath:(NSString*)path httpMethod:(NSString*)method parameter:(NSDictionary*)parameter responseBlock:(IQResponseBlock)response uploadProgressBlock:(IQProgressBlock)uploadProgress downloadProgressBlock:(IQProgressBlock)downloadProgress completionHandler:(IQDictionaryCompletionBlock)completionHandler
{
NSURL *url = nil;
NSData *httpBody = nil;
if ([method isEqualToString:kIQHTTPMethodGET])
{
NSMutableString *parameterString = [[NSMutableString alloc] init];
if ([path hasSuffix:@"?"] == NO) [parameterString appendString:@"?"];
[parameterString appendString:[[self class] httpParameterString:parameter]];
url = [NSURL URLWithString:[[NSString stringWithFormat:@"%@%@%@",self.serverURL,path,parameterString] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
}
else if ([method isEqualToString:kIQHTTPMethodPOST])
{
url = [NSURL URLWithString:[[NSString stringWithFormat:@"%@%@",self.serverURL,path] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSData *originalData = [[NSData alloc] init];
if (self.parameterType == IQRequestParameterTypeJSON)
{
originalData = [NSJSONSerialization dataWithJSONObject:parameter options:0 error:nil];
}
else if (self.parameterType == IQRequestParameterTypeHTTP)
{
NSString *parameterString = [[self class] httpParameterString:parameter];
originalData = [parameterString dataUsingEncoding:NSUTF8StringEncoding];
}
if ([originalData length])
{
NSMutableData *editedData = [[NSMutableData alloc] init];
if (self.startBodyData) [editedData appendData:self.startBodyData];
if (originalData) [editedData appendData:originalData];
if (self.endBodyData) [editedData appendData:self.endBodyData];
httpBody = editedData;
}
}
NSURLRequest *request = [IQWebService requestWithURL:url httpMethod:method contentType:self.defaultContentType body:httpBody];
if (_logEnabled)
{
NSLog(@"RequestURL:- %@",request.URL);
NSLog(@"HTTPHeaderFields:- %@",request.allHTTPHeaderFields);
NSLog(@"Body:- %@",[[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]);
}
[IQURLConnection sendAsynchronousRequest:request responseBlock:response uploadProgressBlock:uploadProgress downloadProgressBlock:downloadProgress completionHandler:^(NSData *result, NSError *error) {
if (_logEnabled)
{
NSLog(@"URL:- %@",request.URL);
NSLog(@"error:- %@",error);
NSLog(@"Response:- %@",[[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]);
}
if (completionHandler != NULL)
{
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:result options:0 error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(response, error);
});
}
}];
}
@end