Skip to content

Commit

Permalink
Merge pull request #175 from judev-forks/feature/request-headers
Browse files Browse the repository at this point in the history
add headerFields dict property to NXOAuth2Request
  • Loading branch information
toto committed Oct 15, 2015
2 parents 4a71e8a + 4d14a02 commit 1b536fc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Sources/OAuth2Client/NXOAuth2Request.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler
responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler;

+ (void)performMethod:(NSString *)method
onResource:(NSURL *)resource
usingParameters:(NSDictionary *)parameters
usingHeaders:(NSDictionary *)headers
withAccount:(NXOAuth2Account *)account
sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler
responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler;


#pragma mark Lifecycle

Expand All @@ -53,6 +61,7 @@
@property (nonatomic, strong, readwrite) NSURL *resource;
@property (nonatomic, strong, readwrite) NSDictionary *parameters;

@property (nonatomic, strong, readwrite) NSDictionary *headerFields;

#pragma mark Signed NSURLRequest

Expand Down
31 changes: 31 additions & 0 deletions Sources/OAuth2Client/NXOAuth2Request.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ @interface NXOAuth2Request () <NXOAuth2ConnectionDelegate>
@property (nonatomic, strong, readwrite) NXOAuth2Request *me;
#pragma mark Apply Parameters
- (void)applyParameters:(NSDictionary *)someParameters onRequest:(NSMutableURLRequest *)aRequest;
- (void)applyHeaders:(NSDictionary *)someHeaders onRequest:(NSMutableURLRequest *)aRequest;
@end


Expand All @@ -40,11 +41,29 @@ + (void)performMethod:(NSString *)aMethod
withAccount:(NXOAuth2Account *)anAccount
sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler
responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler;
{
[self performMethod:aMethod
onResource:aResource
usingParameters:someParameters
usingHeaders:nil
withAccount:anAccount
sendProgressHandler:progressHandler
responseHandler:responseHandler];
}

+ (void)performMethod:(NSString *)aMethod
onResource:(NSURL *)aResource
usingParameters:(NSDictionary *)someParameters
usingHeaders:(NSDictionary *)someHeaders
withAccount:(NXOAuth2Account *)anAccount
sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler
responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler
{
NXOAuth2Request *request = [[NXOAuth2Request alloc] initWithResource:aResource
method:aMethod
parameters:someParameters];
request.account = anAccount;
request.headerFields = someHeaders;
[request performRequestWithSendingProgressHandler:progressHandler responseHandler:responseHandler];
}

Expand Down Expand Up @@ -81,6 +100,7 @@ - (NSURLRequest *)signedURLRequest;

[request setHTTPMethod:self.requestMethod];

[self applyHeaders:self.headerFields onRequest:request];
[self applyParameters:self.parameters onRequest:request];

if (self.account.oauthClient.userAgent && ![request valueForHTTPHeaderField:@"User-Agent"]) {
Expand All @@ -105,6 +125,7 @@ - (void)performRequestWithSendingProgressHandler:(NXOAuth2ConnectionSendingProgr

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.resource];
[request setHTTPMethod:self.requestMethod];
[self applyHeaders:self.headerFields onRequest:request];
self.connection = [[NXOAuth2Connection alloc] initWithRequest:request
requestParameters:self.parameters
oauthClient:self.account.oauthClient
Expand Down Expand Up @@ -171,4 +192,14 @@ - (void)applyParameters:(NSDictionary *)someParameters onRequest:(NSMutableURLRe
}
}

- (void)applyHeaders:(NSDictionary *)someHeaders onRequest:(NSMutableURLRequest *)aRequest;
{
for (id key in someHeaders) {
id value = someHeaders[key];
NSAssert1([key isKindOfClass:[NSString class]], @"Header name should be a string. name=%@", key);
NSAssert2([value isKindOfClass:[NSString class]], @"Header value should be a string. name=%@, value=%@", key, value);
[aRequest setValue:value forHTTPHeaderField:key];
}
}

@end

0 comments on commit 1b536fc

Please sign in to comment.