/
NXOAuth2Request.m
175 lines (132 loc) · 5.75 KB
/
NXOAuth2Request.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
//
// NXOAuth2Request.m
// OAuth2Client
//
// Created by Tobias Kräntzer on 13.07.11.
//
// Copyright 2011 nxtbgthng. All rights reserved.
//
// Licenced under the new BSD-licence.
// See README.md in this repository for
// the full licence.
//
#import "NXOAuth2Connection.h"
#import "NXOAuth2ConnectionDelegate.h"
#import "NXOAuth2AccessToken.h"
#import "NXOAuth2Account.h"
#import "NXOAuth2Client.h"
#import "NXOAuth2PostBodyStream.h"
#import "NSURL+NXOAuth2.h"
#import "NXOAuth2Request.h"
@interface NXOAuth2Request () <NXOAuth2ConnectionDelegate>
@property (nonatomic, strong, readwrite) NXOAuth2Connection *connection;
@property (nonatomic, strong, readwrite) NXOAuth2Request *me;
#pragma mark Apply Parameters
- (void)applyParameters:(NSDictionary *)someParameters onRequest:(NSMutableURLRequest *)aRequest;
@end
@implementation NXOAuth2Request
#pragma mark Class Methods
+ (void)performMethod:(NSString *)aMethod
onResource:(NSURL *)aResource
usingParameters:(NSDictionary *)someParameters
withAccount:(NXOAuth2Account *)anAccount
sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler
responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler;
{
NXOAuth2Request *request = [[NXOAuth2Request alloc] initWithResource:aResource
method:aMethod
parameters:someParameters];
request.account = anAccount;
[request performRequestWithSendingProgressHandler:progressHandler responseHandler:responseHandler];
}
#pragma mark Lifecycle
- (id)initWithResource:(NSURL *)aResource method:(NSString *)aMethod parameters:(NSDictionary *)someParameters;
{
self = [super init];
if (self) {
resource = aResource;
parameters = someParameters;
requestMethod = aMethod;
}
return self;
}
#pragma mark Accessors
@synthesize parameters;
@synthesize resource;
@synthesize requestMethod;
@synthesize account;
@synthesize connection;
@synthesize me;
#pragma mark Signed NSURLRequest
- (NSURLRequest *)signedURLRequest;
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.resource];
[request setHTTPMethod:self.requestMethod];
[self applyParameters:self.parameters onRequest:request];
if (self.account.oauthClient.userAgent && ![request valueForHTTPHeaderField:@"User-Agent"]) {
[request setValue:self.account.oauthClient.userAgent forHTTPHeaderField:@"User-Agent"];
}
if (self.account) {
NSString *oauthAuthorizationHeader = [NSString stringWithFormat:@"%@ %@", self.account.accessToken.tokenType, self.account.accessToken.accessToken];
[request setValue:oauthAuthorizationHeader forHTTPHeaderField:@"Authorization"];
}
return request;
}
#pragma mark Perform Request
- (void)performRequestWithSendingProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler
responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler;
{
NSAssert(self.me == nil, @"This object an only perform one request at the same time.");
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.resource];
[request setHTTPMethod:self.requestMethod];
self.connection = [[NXOAuth2Connection alloc] initWithRequest:request
requestParameters:self.parameters
oauthClient:self.account.oauthClient
sendingProgressHandler:progressHandler
responseHandler:responseHandler];
self.connection.delegate = self;
// Keep request object alive during the request is performing.
self.me = self;
}
#pragma mark Cancel
- (void)cancel;
{
[self.connection cancel];
self.connection = nil;
// Release the referens to self (break cycle) after the current run loop.
__autoreleasing __attribute__((unused)) id runloopMe = self.me;
self.me = nil;
}
#pragma mark NXOAuth2ConnectionDelegate
- (void)oauthConnection:(NXOAuth2Connection *)connection didFinishWithData:(NSData *)data;
{
self.connection = nil;
// Release the referens to self (break cycle) after the current run loop.
__autoreleasing __attribute__((unused)) id runloopMe = self.me;
self.me = nil;
}
- (void)oauthConnection:(NXOAuth2Connection *)connection didFailWithError:(NSError *)error;
{
self.connection = nil;
// Release the reference to self (break cycle) after the current run loop.
__autoreleasing __attribute__((unused)) id runloopMe = self.me;
self.me = nil;
}
#pragma mark Apply Parameters
- (void)applyParameters:(NSDictionary *)someParameters onRequest:(NSMutableURLRequest *)aRequest;
{
if (!someParameters) return;
NSString *httpMethod = [aRequest HTTPMethod];
if ([httpMethod caseInsensitiveCompare:@"POST"] != NSOrderedSame
&& [httpMethod caseInsensitiveCompare:@"PUT"] != NSOrderedSame) {
aRequest.URL = [aRequest.URL nxoauth2_URLByAddingParameters:someParameters];
} else {
NSInputStream *postBodyStream = [[NXOAuth2PostBodyStream alloc] initWithParameters:parameters];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", [(NXOAuth2PostBodyStream *)postBodyStream boundary]];
NSString *contentLength = [NSString stringWithFormat:@"%llu", [(NXOAuth2PostBodyStream *)postBodyStream length]];
[aRequest setValue:contentType forHTTPHeaderField:@"Content-Type"];
[aRequest setValue:contentLength forHTTPHeaderField:@"Content-Length"];
[aRequest setHTTPBodyStream:postBodyStream];
}
}
@end