Created
December 15, 2015 05:55
-
-
Save hlung/2d3a48ae26f633ab680d to your computer and use it in GitHub Desktop.
BDBOAuth1SessionManager+KBExtension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import "BDBOAuth1SessionManager.h" | |
#if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000) || (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090) | |
@interface BDBOAuth1SessionManager (KBExtension) | |
/** | |
* Sends a generic OAuth signed request (using "Authentication" header) for JSON object. | |
* You should have successfully fetched access token first before using this method. | |
* | |
* @param accessPath An endpoint that requires an OAuth signed request. | |
* @param method HTTP method for the request. | |
* @param parameters The parameters to be either set as a query string for `GET` requests, or the request HTTP body. | |
* @param success Completion block performed upon successful request. | |
* @param failure Completion block performed if the request failed. | |
*/ | |
- (void)jsonObjectWithPath:(NSString *)accessPath | |
method:(NSString *)method | |
parameters:(NSDictionary *)parameters | |
success:(void (^)(NSURLResponse *response, id jsonObject))success | |
failure:(void (^)(NSURLResponse *response, NSError *error))failure; | |
@end | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "BDBOAuth1SessionManager+KBExtension.h" | |
#if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000) || (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090) | |
@implementation BDBOAuth1SessionManager (KBExtension) | |
- (void)jsonObjectWithPath:(NSString *)accessPath | |
method:(NSString *)method | |
parameters:(NSDictionary *)parameters | |
success:(void (^)(NSURLResponse *response, id jsonObject))success | |
failure:(void (^)(NSURLResponse *response, NSError *error))failure { | |
AFHTTPResponseSerializer *defaultSerializer = self.responseSerializer; // save old serializer for restore after request is completed | |
self.responseSerializer = [AFJSONResponseSerializer serializer]; // override serializer | |
NSString *URLString = [[NSURL URLWithString:accessPath relativeToURL:self.baseURL] absoluteString]; | |
NSError *error; | |
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:URLString parameters:parameters error:&error]; | |
if (error) { | |
failure(nil, error); | |
return; | |
} | |
void (^completionBlock)(NSURLResponse * __unused, id, NSError *) = ^(NSURLResponse * __unused response, id responseObject, NSError *completionError) { | |
self.responseSerializer = defaultSerializer; // restore the old serializer | |
if (completionError) { | |
failure(response, completionError); | |
return; | |
} | |
success(response, responseObject); | |
}; | |
NSURLSessionDataTask *task = [self dataTaskWithRequest:request completionHandler:completionBlock]; | |
[task resume]; | |
} | |
@end | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment