This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
/
YOSRequestClient.m
259 lines (203 loc) · 8.58 KB
/
YOSRequestClient.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//
// YOSRequestClient.m
// YOSSocial
//
// Created by Zach Graves on 2/19/09.
// Copyright (c) 2009 Yahoo! Inc. All rights reserved.
//
// The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license.
//
#import "YOSRequestClient.h"
#import "NSDictionary+QueryString.h"
#define OAUTH_PARAMS_IN_HTTP_BODY @"OAUTH_PARAMS_IN_HTTP_BODY"
#define OAUTH_PARAMS_IN_AUTH_HEADER @"OAUTH_PARAMS_IN_AUTH_HEADER"
#define OAUTH_PARAMS_IN_QUERY_STRING @"OAUTH_PARAMS_IN_QUERY_STRING"
static NSString *const kYOSUserAgentPrefix = @"YosCocoaSdk/0.4";
@implementation YOSRequestClient
@synthesize consumer;
@synthesize token;
@synthesize HTTPMethod;
@synthesize requestUrl;
@synthesize requestParameters;
@synthesize requestHeaders;
@synthesize HTTPBody;
@synthesize oauthParamsLocation;
@synthesize timeoutInterval;
@synthesize userAgentHeaderValue;
@synthesize requestDelegate;
@synthesize response;
@synthesize responseData;
@synthesize URLConnection;
#pragma mark init
- (id)initWithConsumer:(YOAuthConsumer *)aConsumer andToken:(YOAuthToken *)aToken
{
if(self = [self init]) {
[self setConsumer:aConsumer];
[self setToken:aToken];
[self setOauthParamsLocation:OAUTH_PARAMS_IN_AUTH_HEADER];
[self setTimeoutInterval:20.0]; // TODO: need to switch for EDGE/3G v. wi-fi
[self setUserAgentHeaderValue:[self buildUserAgentHeaderValue]];
}
return self;
}
#pragma mark -
#pragma mark Public
- (YOSResponseData *)sendSynchronousRequest
{
NSMutableURLRequest *urlRequest = [self buildUrlRequest];
NSError *rspError = nil;
NSURLResponse *urlResponse = nil;
NSData *connectionResponseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&urlResponse error:&rspError];
[rspError autorelease];
[connectionResponseData autorelease];
YOSResponseData *serviceResponseData = [YOSResponseData responseWithData:connectionResponseData
andURLResponse:urlResponse];
return serviceResponseData;
}
- (BOOL)sendAsyncRequestWithDelegate:(id)delegate
{
self.responseData = [[NSMutableData data] retain];
[self setRequestDelegate:delegate];
NSMutableURLRequest *urlRequest = [self buildUrlRequest];
// self.URLConnection = [[NSURLConnection connectionWithRequest:urlRequest delegate:self] retain];
[self setURLConnection:[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES]];
BOOL connectionCreated = (self.URLConnection != nil);
return connectionCreated;
}
#pragma mark -
#pragma mark Private
- (NSMutableURLRequest *)buildUrlRequest
{
YOAuthRequest *oauthRequest = [self buildOAuthRequest];
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] init];
[urlRequest setHTTPMethod:self.HTTPMethod];
[urlRequest setTimeoutInterval:self.timeoutInterval];
[urlRequest setValue:self.userAgentHeaderValue forHTTPHeaderField:@"User-Agent"];
if([self.HTTPMethod isEqualToString:@"POST"])
{
NSString *requestAbsoluteURLString = [self.requestUrl absoluteString];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", requestAbsoluteURLString]];
[urlRequest setURL:url];
NSString *postDataQueryString = (oauthRequest)
? [[oauthRequest allRequestParametersAsDictionary] QueryString]
: [requestParameters QueryString];
NSData *postData = [postDataQueryString dataUsingEncoding: NSASCIIStringEncoding];
[urlRequest setHTTPBody:postData];
}
else {
NSDictionary *queryParameters = (oauthRequest && [oauthParamsLocation isEqualToString:OAUTH_PARAMS_IN_QUERY_STRING])
? [oauthRequest allRequestParametersAsDictionary]
: requestParameters;
NSString *requestAbsoluteURLString = [NSString stringWithFormat:@"%@?%@", [self.requestUrl absoluteString], [queryParameters QueryString]];
NSURL *url = [NSURL URLWithString:requestAbsoluteURLString];
[urlRequest setURL:url];
if ([self.HTTPMethod isEqualToString:@"PUT"]) {
[urlRequest setHTTPBody:self.HTTPBody];
}
}
if(oauthRequest) {
if([oauthParamsLocation isEqualToString:OAUTH_PARAMS_IN_AUTH_HEADER]) {
[requestHeaders setObject:[oauthRequest buildAuthorizationHeaderValue] forKey:@"Authorization"];
}
else if([oauthParamsLocation isEqualToString:OAUTH_PARAMS_IN_HTTP_BODY]) {
NSString *requestQueryString = [[oauthRequest allOAuthRequestParametersAsDictionary] QueryString];
[urlRequest setHTTPBody:[requestQueryString dataUsingEncoding: NSASCIIStringEncoding]];
}
}
if(self.requestHeaders && [self.requestHeaders count]) {
for (NSString *headerKey in [self.requestHeaders allKeys]) {
NSString *headerValue = [self.requestHeaders objectForKey:headerKey];
[urlRequest setValue:headerValue forHTTPHeaderField:headerKey];
}
}
return [urlRequest autorelease];
}
- (YOAuthRequest *)buildOAuthRequest
{
YOAuthRequest *oauthRequest = nil;
if(self.consumer) {
oauthRequest = [[YOAuthRequest alloc] initWithConsumer:self.consumer
andUrl:self.requestUrl
andHTTPMethod:self.HTTPMethod
andSignatureMethod:@"HMAC-SHA1"];
if(self.token)
[oauthRequest setToken:self.token];
if(self.requestParameters && [self.requestParameters count])
[oauthRequest setRequestParams:requestParameters];
[oauthRequest prepareRequest];
}
return [oauthRequest autorelease];
}
- (NSString *)buildUserAgentHeaderValue
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
// iPhone apps
// Examples:
// YosCocoaSdk/0.1 (iPhone/2.2.1; iPhone OS; en_US;)
// YosCocoaSdk/0.1 (iPhone Simulator/2.2.1; iPhone OS; en_US;)
// YosCocoaSdk/0.1 (iPod Touch/2.2.1; iPhone OS; en_US;)
UIDevice *currentDevice = [UIDevice currentDevice];
NSString *currentLocaleIdentifier = [[NSLocale currentLocale] localeIdentifier];
NSString *currentDeviceInfo = [NSString stringWithFormat:@"%@/%@; %@; %@;",[currentDevice model],[currentDevice systemVersion],[currentDevice systemName],currentLocaleIdentifier];
NSString *clientUrlRequestUserAgent = [NSString stringWithFormat:@"%@ (%@)",kYOSUserAgentPrefix,currentDeviceInfo];
return clientUrlRequestUserAgent;
#endif
#if (TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR))
// Mac apps
// Examples:
// YosCocoaSdk/0.1 (Mac OS X/10.5.6; en_US;)
NSDictionary *systemVersionDictionary = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"];
NSString *systemProductName = [systemVersionDictionary objectForKey:@"ProductName"];
NSString *systemProductVersion = [systemVersionDictionary objectForKey:@"ProductVersion"];
NSString *currentLocaleIdentifier = [[NSLocale currentLocale] localeIdentifier];
NSString *currentDeviceInfo = [NSString stringWithFormat:@"%@/%@; %@;", systemProductName, systemProductVersion, currentLocaleIdentifier];
NSString *clientUrlRequestUserAgent = [NSString stringWithFormat:@"%@ (%@)",kYOSUserAgentPrefix,currentDeviceInfo];
return clientUrlRequestUserAgent;
#endif
// return default user-agent in case target OS is not available.
return kYOSUserAgentPrefix;
}
#pragma mark -
#pragma mark NSURLConnection delegate methods
- (void)cancel
{
[self.URLConnection cancel];
if ([self.requestDelegate respondsToSelector:@selector(connectionWasCancelled:)]) {
[self.requestDelegate connectionWasCancelled];
}
}
- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)aResponse
{
if(self.response) [self.response release];
self.response = [aResponse retain];
[self.responseData setLength:0];
if ([self.requestDelegate respondsToSelector:@selector(request:didReceiveResponse:)]) {
[self.requestDelegate request:self didReceiveResponse:self.response];
}
}
- (void)connection:(NSURLConnection *)aConnection didReceiveData:(NSData *)data
{
[self.responseData appendData:data];
if ([self.requestDelegate respondsToSelector:@selector(request:didReceiveData:)]) {
[self.requestDelegate request:self didReceiveData:self.responseData];
}
}
- (void)connection:(NSURLConnection *)aConnection didFailWithError:(NSError *)error
{
YOSResponseData *serviceResponseData = [[YOSResponseData alloc] init];
[serviceResponseData autorelease];
[serviceResponseData setError:error];
[serviceResponseData setDidSucceed:NO];
if ([self.requestDelegate respondsToSelector:@selector(requestDidFailWithError:)]) {
[self.requestDelegate requestDidFailWithError:serviceResponseData];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
{
YOSResponseData *serviceResponseData = [YOSResponseData responseWithData:self.responseData
andURLResponse:self.response];
if ([self.requestDelegate respondsToSelector:@selector(requestDidFinishLoading:)]) {
[self.requestDelegate requestDidFinishLoading:serviceResponseData];
}
}
@end