This repository has been archived by the owner on Feb 20, 2021. It is now read-only.
/
LCHttpClient.m
146 lines (125 loc) · 5.68 KB
/
LCHttpClient.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
//
// LCHttpClient.m
// Feedback
//
// Created by Feng Junwen on 5/15/15.
// Copyright (c) 2015 LeanCloud. All rights reserved.
//
#import "LCHttpClient.h"
#import "LCUtils.h"
#import "LCRouter.h"
@interface LCHttpClient ()
@property (nonatomic, strong) NSURL *baseURL;
@property (nonatomic, strong) NSOperationQueue *operationQueue;
@end
@implementation LCHttpClient
+ (LCHttpClient*)sharedInstance {
static dispatch_once_t once;
static LCHttpClient * sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
sharedInstance.baseURL = [NSURL URLWithString:@"https://api.leancloud.cn/1.1/"];
sharedInstance.operationQueue = [[NSOperationQueue alloc] init];
});
return sharedInstance;
}
- (NSString *)queryStringFromParameters:(NSDictionary *)parameters {
NSMutableString *queries = [[NSMutableString alloc] init];
NSArray *keys = [parameters allKeys];
for (int i = 0; i < keys.count; i++) {
if (i != 0) {
[queries appendString:@"&"];
}
NSString *value = [parameters valueForKey:keys[i]];
[queries appendFormat:@"%@=%@", keys[i], value];
}
return [queries stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters {
NSURL *url = [NSURL URLWithString:path];
if (!url.scheme.length) {
NSString *URLString = [[LCRouter sharedInstance] URLStringForPath:path];
url = [NSURL URLWithString:URLString];
}
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setValue:[AVOSCloud getApplicationId] forHTTPHeaderField:@"X-LC-Id"];
NSString *timestamp=[NSString stringWithFormat:@"%.0f",1000*[[NSDate date] timeIntervalSince1970]];
NSString *sign=[LCUtils calMD5:[NSString stringWithFormat:@"%@%@",timestamp,[AVOSCloud getClientKey]]];
NSString *headerValue=[NSString stringWithFormat:@"%@,%@",sign,timestamp];
[request setValue:headerValue forHTTPHeaderField:@"X-LC-Sign"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
if ([AVUser currentUser].sessionToken) {
[request setValue:[AVUser currentUser].sessionToken forHTTPHeaderField:@"X-LC-Session"];
}
[request setTimeoutInterval:kAVDefaultNetworkTimeoutInterval];
[request setHTTPMethod:method];
if ([method isEqualToString:@"GET"] || [method isEqualToString:@"DELETE"]) {
url = [NSURL URLWithString:[[url absoluteString] stringByAppendingFormat:@"?%@", [self queryStringFromParameters:parameters]]];
[request setURL:url];
} else {
[request setValue:[NSString stringWithFormat:@"application/json"] forHTTPHeaderField:@"Content-Type"];
NSError *error;
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error]];
if (error) {
FLog(@"%@ error : %@", [self class], error);
}
}
return request;
}
- (void)goRequest:(NSURLRequest *)request block:(AVIdResultBlock)block {
FLog(@"request url : %@", request.URL);
FLog(@"request headers : %@", [request allHTTPHeaderFields]);
FLog(@"request body %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);
[NSURLConnection sendAsynchronousRequest:request queue:self.operationQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
block(nil, connectionError);
} else {
if (response && data) {
NSError *error;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error) {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
FLog(@"reponse : %@", responseString);
block(nil, [LCUtils errorWithText:@"Http request failed, reponse string : %@", responseString]);
} else {
block(dictionary, nil);
}
}
}
}];
}
- (NSDictionary *)whereDictionaryFromConditions:(NSDictionary *)conditions {
NSDictionary *where = [NSDictionary dictionary];
if (conditions.count>0) {
NSData *data = [NSJSONSerialization dataWithJSONObject:conditions options:0 error:nil];
NSString *conditionString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
where = @{@"where":conditionString};
}
return where;
}
-(void)getObject:(NSString *)path
withParameters:(NSDictionary *)parameters
block:(AVIdResultBlock)block {
NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:[self whereDictionaryFromConditions:parameters]];
[self goRequest:request block:block];
}
-(void)postObject:(NSString *)path
withParameters:(NSDictionary *)parameters
block:(AVIdResultBlock)block
{
NSMutableURLRequest *request= [self requestWithMethod:@"POST" path:path parameters:parameters];
[self goRequest:request block:block];
}
-(void)putObject:(NSString *)path
withParameters:(NSDictionary *)parameters
block:(AVIdResultBlock)block {
NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters];
[self goRequest:request block:block];
}
-(void)deleteObject:(NSString *)path
withParameters:(NSDictionary *)parameters
block:(AVIdResultBlock)block {
NSMutableURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters];
[self goRequest:request block:block];
}
@end