Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add headerFields dict property to NXOAuth2Request #175

Merged
merged 2 commits into from
Oct 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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