-
Notifications
You must be signed in to change notification settings - Fork 0
/
CFAPIClient.m
executable file
·195 lines (166 loc) · 9.42 KB
/
CFAPIClient.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// CFAPIClient.m
// ChineseFit
//
//
#import "CFAPIClient.h"
@implementation CFAPIClient
+(CFAPIClient *)sharedInstance {
static CFAPIClient *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:kAPIHost]];
[sharedInstance registerHTTPOperationClass:[AFJSONRequestOperation class]];
// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[sharedInstance setDefaultHeader:@"Accept" value:@"application/json"];
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
});
return sharedInstance;
}
+(CFAPIClient *)sharedInstanceWeiXin {
static CFAPIClient *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:kAPIHost]];
[sharedInstance registerHTTPOperationClass:[AFJSONRequestOperation class]];
// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[sharedInstance setDefaultHeader:@"Accept" value:@"application/json"];
// [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
[AFJSONRequestOperation acceptableContentTypes];
});
return sharedInstance;
}
-(void)requestWexinURL:(NSString *)url andMethod:(NSString *)method withPath:(NSString *)path withParams:(NSMutableDictionary *)params onCompletion:(JSONResponseBlock)completionBlock
{
NSMutableURLRequest *request;
if ([method isEqualToString:HTTPGETMETHOD]) {
request = [self requestWithMethod:method path:path parameters:params];
}else{
// NSData* uploadFile = nil;
// if ([params objectForKey:@"file"]) {
// uploadFile = (NSData*)[params objectForKey:@"file"];
// [params removeObjectForKey:@"file"];
// }
// request = [self multipartFormRequestWithMethod:method path:path parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
// if (uploadFile) {
// [formData appendPartWithFileData:uploadFile name:@"file" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
// }
// }];
NSObject *uploadFile = nil;
if ([params objectForKey:@"file"]) {
uploadFile = [params objectForKey:@"file"];
}
request = [self multipartFormRequestWithMethod:method path:path parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
if (uploadFile) {
if ([uploadFile isKindOfClass:[NSData class]]) {
NSData *realUploadFile = (NSData *)uploadFile;
if (realUploadFile) {
[formData appendPartWithFileData:realUploadFile name:@"file" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
}
}else if([uploadFile isKindOfClass:[NSMutableArray class]] ||[uploadFile isKindOfClass:[NSArray class]]){
NSArray *uploadFileArray = (NSArray *)uploadFile;
if (uploadFileArray && uploadFileArray.count) {
for (NSData *realData in uploadFileArray) {
[formData appendPartWithFileData:realData name:@"file" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
}
}
}
}
}];
}
[request setTimeoutInterval:500];
debugLog(@"request url %@",request.URL);
NSURL *urlurl = [NSURL URLWithString:url];
[request setURL:urlurl];;
debugLog(@"request url %@",request.URL);
AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
completionBlock(responseObject);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];
[operation start];
}
-(void)requestWithMethod:(NSString *)method withPath:(NSString *)path withParams:(NSMutableDictionary *)params onCompletion:(JSONResponseBlock)completionBlock
{
NSMutableURLRequest *request;
if ([method isEqualToString:HTTPGETMETHOD]) {
request = [self requestWithMethod:method path:path parameters:params];
}else{
// NSData* uploadFile = nil;
// if ([params objectForKey:@"file"]) {
// uploadFile = (NSData*)[params objectForKey:@"file"];
// [params removeObjectForKey:@"file"];
// }
// request = [self multipartFormRequestWithMethod:method path:path parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
// if (uploadFile) {
// [formData appendPartWithFileData:uploadFile name:@"file" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
// }
// }];
NSObject *uploadFile = nil;
if ([params objectForKey:@"file"]) {
uploadFile = [params objectForKey:@"file"];
}
request = [self multipartFormRequestWithMethod:method path:path parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
if (uploadFile) {
if ([uploadFile isKindOfClass:[NSData class]]) {
NSData *realUploadFile = (NSData *)uploadFile;
if (realUploadFile) {
[formData appendPartWithFileData:realUploadFile name:@"file" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
}
}else if([uploadFile isKindOfClass:[NSMutableArray class]] ||[uploadFile isKindOfClass:[NSArray class]]){
NSArray *uploadFileArray = (NSArray *)uploadFile;
if (uploadFileArray && uploadFileArray.count) {
for (NSData *realData in uploadFileArray) {
[formData appendPartWithFileData:realData name:@"file" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
}
}
}
}
}];
}
[request setTimeoutInterval:120];
debugLog(@"request url %@",request.URL);
NSLog(@"request=%@",request);
AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
completionBlock(responseObject);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];
[operation start];
}
- (void)downloadImageWithidentifier:(NSString *)identifier onCompletionBlock:(ImageResponseBlock)completionBlock
{
NSString* urlString = [NSString stringWithFormat:@"%@%@%@",kAPIHost,kAPIDownloadImage, identifier];
AFImageRequestOperation* operation = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
completionBlock(image);
}failure:nil];
[operation start];
}
// add by star by 20150506 for promote cover
-(void)downloadImageWithNoDownloadidentifier:(NSString *)identifier onCompletionBlock:(ImageResponseBlock)completionBlock{
NSString* urlString = [NSString stringWithFormat:@"%@%@",kAPIHost, identifier];
AFImageRequestOperation* operation = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
completionBlock(image);
}failure:nil];
[operation start];
}// hsu jay
- (void)downloadImageWithidentifier2:(NSString *)identifier onCompletionBlock:(ImageResponseBlock)completionBlock
{
NSString* urlString = [NSString stringWithFormat:@"%@%@%@",kAPIHost,kWEBGetHealthReport
, identifier];
AFImageRequestOperation* operation = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
completionBlock(image);
}failure:nil];
[operation start];
}
@end