Skip to content

Commit

Permalink
Issue jeyben#66: modified HTTP Client to be compatible with AFNetwork…
Browse files Browse the repository at this point in the history
…ing 3
  • Loading branch information
kimjune01 committed Jan 7, 2016
1 parent 7411b0b commit c5eaf4a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 54 deletions.
2 changes: 1 addition & 1 deletion IOSLinkedInAPI.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'IOSLinkedInAPI'
s.version = '2.0.0'
s.version = '3.0.0'
s.license = 'MIT'
s.summary = 'IOS LinkedIn API capable of accessing LinkedIn using oauth2. Using a UIWebView to fetch the authorization code.'
s.homepage = 'https://github.com/jeyben/IOSLinkedInAPI'
Expand Down
48 changes: 2 additions & 46 deletions IOSLinkedInAPI/LIALinkedInHttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,65 +20,21 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "AFHTTPRequestOperationManager.h"
#import <AFNetworking/AFHTTPSessionManager.h>

@class LIALinkedInApplication;

/**
* A LinkedIn client is created using a `LIALinkedInApplication` and is the network instance that will perform all requests to the LinkedIn API.
**/
@interface LIALinkedInHttpClient : AFHTTPRequestOperationManager
@interface LIALinkedInHttpClient : AFHTTPSessionManager

/** ************************************************************************************************ **
* @name Initializers
** ************************************************************************************************ **/

/**
* A LinkedIn client is created using a `LIALinkedInApplication` and is the network instance that will perform all requests to the LinkedIn API.
* @param application A `LIALinkedInApplication` configured instance.
* @discussion This method calls `+clientForApplication:presentingViewController:` with presenting view controller as nil.
**/
+ (LIALinkedInHttpClient *)clientForApplication:(LIALinkedInApplication *)application;

/**
* A LinkedIn client is created using a `LIALinkedInApplication` and is the network instance that will perform all requests to the LinkedIn API.
* @param application A `LIALinkedInApplication` configured instance.
* @param viewController The view controller that the UIWebView will be modally presented from. Passing nil assumes the root view controller.
**/
+ (LIALinkedInHttpClient *)clientForApplication:(LIALinkedInApplication *)application presentingViewController:viewController;

/** ************************************************************************************************ **
* @name Methods
** ************************************************************************************************ **/

/**
* Returns YES if the current cached token is valid and not expired, NO otherwise.
* @return The validity of the cached token.
* @discussion When getting the token via the method `-getAccessToken:success:failure:`, the library is caching the token for further use.
**/
- (BOOL)validToken;

/**
* Returns the previsouldy cached LinkedIn access token.
* @return The access token.
* @discussion When getting the token via the method `-getAccessToken:success:failure:`, the library is caching the token for further use.
**/
- (NSString *)accessToken;

/**
* Retrieves the access token from a valid authhorization code.
* @param authorizationCode The authorization code.
* @param success A success block. The success block contains a dictoinary containing the access token keyed by the string "access_token".
* @param failure A failure block containing the error.
**/
- (void)getAccessToken:(NSString *)authorizationCode success:(void (^)(NSDictionary *))success failure:(void (^)(NSError *))failure;

/**
* Retrieves an authorization code.
* @param success A success block.
* @param cancel A cancel block. This block is called when the user cancels the linkedin authentication flow.
* @param failure A failure block containing the error.
**/
- (void)getAuthorizationCode:(void (^)(NSString *))success cancel:(void (^)(void))cancel failure:(void (^)(NSError *))failure;

@end
14 changes: 7 additions & 7 deletions IOSLinkedInAPI/LIALinkedInHttpClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,24 @@ - (NSString *)accessToken {
- (void)getAccessToken:(NSString *)authorizationCode success:(void (^)(NSDictionary *))success failure:(void (^)(NSError *))failure {
NSString *accessTokenUrl = @"/uas/oauth2/accessToken?grant_type=authorization_code&code=%@&redirect_uri=%@&client_id=%@&client_secret=%@";
NSString *url = [NSString stringWithFormat:accessTokenUrl, authorizationCode, [self.application.redirectURL LIAEncode], self.application.clientId, self.application.clientSecret];

[self POST:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

//using AFNetworking 3
[self POST:url parameters:nil constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *accessToken = [responseObject objectForKey:@"access_token"];
NSTimeInterval expiration = [[responseObject objectForKey:@"expires_in"] doubleValue];

// store credentials
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults setObject:accessToken forKey:LINKEDIN_TOKEN_KEY];
[userDefaults setDouble:expiration forKey:LINKEDIN_EXPIRATION_KEY];
[userDefaults setDouble:[[NSDate date] timeIntervalSince1970] forKey:LINKEDIN_CREATION_KEY];
[userDefaults synchronize];

success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error);
}];

}

- (void)getAuthorizationCode:(void (^)(NSString *))success cancel:(void (^)(void))cancel failure:(void (^)(NSError *))failure {
Expand Down

0 comments on commit c5eaf4a

Please sign in to comment.