Skip to content
Open
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
3 changes: 3 additions & 0 deletions Classes/FlickrKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,13 @@ FOUNDATION_EXPORT const unsigned char FlickrKitVersionString[];
- (FKDUNetworkOperation *) beginAuthWithCallbackURL:(NSURL *)url permission:(FKPermission)permission completion:(FKAPIAuthBeginCompletion)completion;
// 2. After they login and authorize the app, need to get an auth token - this will happen via your URL scheme - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
- (FKDUNetworkOperation *) completeAuthWithURL:(NSURL *)url completion:(FKAPIAuthCompletion)completion;
- (FKDUNetworkOperation *) completeAuthWithURL:(NSURL *)url saveAuthTokenToUserDefaults:(BOOL)persistAuthToken completion:(FKAPIAuthCompletion)completion;
// 3. On returning to the app, you want to re-log them in automatically - do it here
- (FKFlickrNetworkOperation *) checkAuthorizationOnCompletion:(FKAPIAuthCompletion)completion;
// 4. Logout - just removes all the stored keys
- (void) logout;
// 5. Programatic Login - allow logging in with supplied tokens
- (void) loginWithAuthToken:(NSString *)authToken authSecret:(NSString *)authSecret;

@end

Expand Down
48 changes: 32 additions & 16 deletions Classes/FlickrKit/FlickrKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ - (FKFlickrNetworkOperation *) call:(NSString *)apiMethod args:(NSDictionary *)r
}

- (FKFlickrNetworkOperation *) call:(NSString *)apiMethod args:(NSDictionary *)requestArgs maxCacheAge:(FKDUMaxAge)maxAge completion:(FKAPIRequestCompletion)completion {
NSAssert([FlickrKit sharedFlickrKit].apiKey, @"You must pass an apiKey to initializeWithAPIKey");
NSAssert(self.apiKey, @"You must pass an apiKey to initializeWithAPIKey");
NSAssert(apiMethod, @"You must pass an apiMethod");
NSAssert(completion, @"You must pass a completion block");

if ([FKDUReachability isOffline]) {
if ([FKDUReachability isOffline]) {
if (completion) {
completion(nil, [FKDUReachability buildOfflineErrorMessage]);
}
Expand All @@ -74,7 +74,7 @@ - (FKFlickrNetworkOperation *) call:(NSString *)apiMethod args:(NSDictionary *)r
self.diskCache = [FKDUDefaultDiskCache sharedDiskCache];
}

FKFlickrNetworkOperation *op = [[FKFlickrNetworkOperation alloc] initWithAPIMethod:apiMethod arguments:requestArgs maxAgeMinutes:maxAge diskCache:self.diskCache completion:completion];
FKFlickrNetworkOperation *op = [[FKFlickrNetworkOperation alloc] initWithFlickrKit:self APIMethod:apiMethod arguments:requestArgs maxAgeMinutes:maxAge completion:completion];

[[FKDUNetworkController sharedController] execute:op];
return op;
Expand All @@ -87,12 +87,12 @@ - (FKFlickrNetworkOperation *) call:(id<FKFlickrAPIMethod>)method completion:(FK
}

- (FKFlickrNetworkOperation *) call:(id<FKFlickrAPIMethod>)method maxCacheAge:(FKDUMaxAge)maxAge completion:(FKAPIRequestCompletion)completion {
NSAssert([FlickrKit sharedFlickrKit].apiKey, @"You must pass an apiKey to initializeWithAPIKey");
NSAssert(self.apiKey, @"You must pass an apiKey to initializeWithAPIKey");
NSAssert(method, @"You must pass a method");

// Check if this method needs auth
if ([method needsLogin]) {
if (![FlickrKit sharedFlickrKit].isAuthorized) {
if (!self.isAuthorized) {
NSString *errorDescription = @"You need to login to call this method";
NSDictionary *userInfo = @{NSLocalizedDescriptionKey: errorDescription};
NSError *error = [NSError errorWithDomain:FKFlickrAPIErrorDomain code:FKErrorNotAuthorized userInfo:userInfo];
Expand All @@ -101,7 +101,7 @@ - (FKFlickrNetworkOperation *) call:(id<FKFlickrAPIMethod>)method maxCacheAge:(F
} else {
// Check method permission
FKPermission permissionRequired = [method requiredPerms];
FKPermission grantedPermission = [FlickrKit sharedFlickrKit].permissionGranted;
FKPermission grantedPermission = self.permissionGranted;
if (permissionRequired > grantedPermission) {
NSString *requiredString = FKPermissionStringForPermission(permissionRequired);
NSString *grantedString = FKPermissionStringForPermission(grantedPermission);
Expand All @@ -126,7 +126,7 @@ - (FKFlickrNetworkOperation *) call:(id<FKFlickrAPIMethod>)method maxCacheAge:(F
self.diskCache = [FKDUDefaultDiskCache sharedDiskCache];
}

FKFlickrNetworkOperation *op = [[FKFlickrNetworkOperation alloc] initWithAPIMethod:method maxAgeMinutes:maxAge diskCache:self.diskCache completion:completion];
FKFlickrNetworkOperation *op = [[FKFlickrNetworkOperation alloc] initWithFlickrKit:self APIMethod:method maxAgeMinutes:maxAge completion:completion];

[[FKDUNetworkController sharedController] execute:op];
return op;
Expand Down Expand Up @@ -198,7 +198,7 @@ - (FKDUNetworkOperation *) beginAuthWithCallbackURL:(NSURL *)url permission:(FKP
}

NSDictionary *paramsDictionary = @{@"oauth_callback": [url absoluteString]};
FKURLBuilder *urlBuilder = [[FKURLBuilder alloc] init];
FKURLBuilder *urlBuilder = [[FKURLBuilder alloc] initWithFlickrKit:self];
NSURL *requestURL = [urlBuilder oauthURLFromBaseURL:[NSURL URLWithString:@"https://www.flickr.com/services/oauth/request_token"] method:FKHttpMethodGET params:paramsDictionary];

FKDUNetworkOperation *op = [[FKDUNetworkOperation alloc] initWithURL:requestURL];
Expand Down Expand Up @@ -235,6 +235,11 @@ - (FKDUNetworkOperation *) beginAuthWithCallbackURL:(NSURL *)url permission:(FKP
}

- (FKDUNetworkOperation *) completeAuthWithURL:(NSURL *)url completion:(FKAPIAuthCompletion)completion {
// By default persist token to user defaults
return [self completeAuthWithURL:url saveAuthTokenToUserDefaults:YES completion:completion];
}

- (FKDUNetworkOperation *) completeAuthWithURL:(NSURL *)url saveAuthTokenToUserDefaults:(BOOL)persistAuthToken completion:(FKAPIAuthCompletion)completion {

if ([FKDUReachability isOffline]) {
if (completion) {
Expand All @@ -258,7 +263,7 @@ - (FKDUNetworkOperation *) completeAuthWithURL:(NSURL *)url completion:(FKAPIAut
}

NSDictionary *paramsDictionary = @{@"oauth_token": token, @"oauth_verifier": verifier};
FKURLBuilder *urlBuilder = [[FKURLBuilder alloc] init];
FKURLBuilder *urlBuilder = [[FKURLBuilder alloc] initWithFlickrKit:self];
NSURL *requestURL = [urlBuilder oauthURLFromBaseURL:[NSURL URLWithString:@"https://www.flickr.com/services/oauth/access_token"] method:FKHttpMethodGET params:paramsDictionary];

FKDUNetworkOperation *op = [[FKDUNetworkOperation alloc] initWithURL:requestURL];
Expand Down Expand Up @@ -294,9 +299,11 @@ - (FKDUNetworkOperation *) completeAuthWithURL:(NSURL *)url completion:(FKAPIAut
completion(nil, nil, nil, error);
}
} else {
[[NSUserDefaults standardUserDefaults] setValue:oat forKey:kFKStoredTokenKey];
[[NSUserDefaults standardUserDefaults] setValue:oats forKey:kFKStoredTokenSecret];
[[NSUserDefaults standardUserDefaults] synchronize];
if (persistAuthToken) {
[[NSUserDefaults standardUserDefaults] setValue:oat forKey:kFKStoredTokenKey];
[[NSUserDefaults standardUserDefaults] setValue:oats forKey:kFKStoredTokenSecret];
[[NSUserDefaults standardUserDefaults] synchronize];
}
self.authorized = YES;
self.authToken = oat;
self.authSecret = oats;
Expand Down Expand Up @@ -384,6 +391,14 @@ - (void) logout {
self.beginAuthURL = nil;
}

#pragma mark - 5. Programatic Login - allow logging in with supplied tokens

- (void) loginWithAuthToken:(NSString *)authToken authSecret:(NSString *)authSecret {
self.authorized = YES;
self.authToken = authToken;
self.authSecret = authSecret;
}

@end


Expand Down Expand Up @@ -452,16 +467,17 @@ - (NSURL *) photoURLForSize:(FKPhotoSize)size photoID:(NSString *)photoID server

@implementation FlickrKit (PhotoUpload)
- (FKImageUploadNetworkOperation *) uploadImage:(DUImage *)image args:(NSDictionary *)args completion:(FKAPIImageUploadCompletion)completion {
FKImageUploadNetworkOperation *imageUpload = [[FKImageUploadNetworkOperation alloc] initWithImage:image arguments:args completion:completion];
FKImageUploadNetworkOperation *imageUpload = [[FKImageUploadNetworkOperation alloc] initWithFlickrKit:self image:image arguments:args completion:completion];
[[FKDUNetworkController sharedController] execute:imageUpload];
return imageUpload;
}

#if TARGET_OS_IOS
- (FKImageUploadNetworkOperation *) uploadAssetURL:(NSURL *)assetURL args:(NSDictionary *)args completion:(FKAPIImageUploadCompletion)completion {
FKImageUploadNetworkOperation *imageUpload = [[FKImageUploadNetworkOperation alloc] initWithAssetURL:assetURL
arguments:args
completion:completion];
FKImageUploadNetworkOperation *imageUpload = [[FKImageUploadNetworkOperation alloc] initWithFlickrKit:self
assetURL:assetURL
arguments:args
completion:completion];
[[FKDUNetworkController sharedController] execute:imageUpload];
return imageUpload;
}
Expand Down
7 changes: 4 additions & 3 deletions Classes/Network/FKFlickrNetworkOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// Copyright (c) 2013 DevedUp Ltd. All rights reserved. http://www.devedup.com
//

@class FlickrKit;

#import "FKDataTypes.h"
#import "FKDUConcurrentOperation.h"
#import "FKDUDiskCache.h"
Expand All @@ -14,8 +16,7 @@

@interface FKFlickrNetworkOperation : FKDUNetworkOperation

- (id) initWithAPIMethod:(NSString *)api arguments:(NSDictionary *)args maxAgeMinutes:(FKDUMaxAge)maxAge diskCache:(id<FKDUDiskCache>)diskCache completion:(FKAPIRequestCompletion)completion;

- (id) initWithAPIMethod:(id<FKFlickrAPIMethod>)method maxAgeMinutes:(FKDUMaxAge)maxAge diskCache:(id<FKDUDiskCache>)diskCache completion:(FKAPIRequestCompletion)completion;
- (id) initWithFlickrKit:(FlickrKit *)flickrKit APIMethod:(NSString *)api arguments:(NSDictionary *)args maxAgeMinutes:(FKDUMaxAge)maxAge completion:(FKAPIRequestCompletion)completion;
- (id) initWithFlickrKit:(FlickrKit *)flickrKit APIMethod:(id<FKFlickrAPIMethod>)method maxAgeMinutes:(FKDUMaxAge)maxAge completion:(FKAPIRequestCompletion)completion;

@end
20 changes: 10 additions & 10 deletions Classes/Network/FKFlickrNetworkOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#import "FKDUNetworkController.h"

@interface FKFlickrNetworkOperation ()
@property (nonatomic, strong) FlickrKit *flickrKit;
@property (nonatomic, strong) NSString *apiMethod;
@property (nonatomic, strong) NSDictionary *args;
@property (nonatomic, copy) FKAPIRequestCompletion completion;
@property (nonatomic, strong) id<FKDUDiskCache> diskCache;
@property (nonatomic, assign) NSInteger maxAgeMinutes;
@property (nonatomic, strong) NSString *cacheKey;
@property (nonatomic, retain) id<FKFlickrAPIMethod> method;
Expand All @@ -28,11 +28,11 @@ @implementation FKFlickrNetworkOperation

#pragma mark - Init

- (id) initWithAPIMethod:(NSString *)api arguments:(NSDictionary *)args maxAgeMinutes:(FKDUMaxAge)maxAge diskCache:(id<FKDUDiskCache>)diskCache completion:(FKAPIRequestCompletion)completion {
- (id) initWithFlickrKit:(FlickrKit *)flickrKit APIMethod:(NSString *)api arguments:(NSDictionary *)args maxAgeMinutes:(FKDUMaxAge)maxAge completion:(FKAPIRequestCompletion)completion {
self = [super init];
if (self) {
self.flickrKit = flickrKit;
self.maxAgeMinutes = maxAge;
self.diskCache = diskCache;
self.apiMethod = api;
self.args = args;
self.completion = completion;
Expand All @@ -42,10 +42,10 @@ - (id) initWithAPIMethod:(NSString *)api arguments:(NSDictionary *)args maxAgeMi
return self;
}

- (id) initWithAPIMethod:(id<FKFlickrAPIMethod>)method maxAgeMinutes:(FKDUMaxAge)maxAge diskCache:(id<FKDUDiskCache>)diskCache completion:(FKAPIRequestCompletion)completion {
- (id) initWithFlickrKit:(FlickrKit *)flickrKit APIMethod:(id<FKFlickrAPIMethod>)method maxAgeMinutes:(FKDUMaxAge)maxAge completion:(FKAPIRequestCompletion)completion {
NSString *api = [method name];
NSDictionary *args = [method args];
return [self initWithAPIMethod:api arguments:args maxAgeMinutes:maxAge diskCache:diskCache completion:completion];
return [self initWithFlickrKit:flickrKit APIMethod:api arguments:args maxAgeMinutes:maxAge completion:completion];
}

#pragma mark - DUOperation Methods
Expand All @@ -71,9 +71,9 @@ - (BOOL) startRequest:(NSError **)error {

NSData *cachedData = nil;
if (0 == self.maxAgeMinutes) {
[self.diskCache removeDataForKey:self.cacheKey];
[self.flickrKit.diskCache removeDataForKey:self.cacheKey];
} else {
cachedData = [self.diskCache dataForKey:self.cacheKey maxAgeMinutes:self.maxAgeMinutes];
cachedData = [self.flickrKit.diskCache dataForKey:self.cacheKey maxAgeMinutes:self.maxAgeMinutes];
}

if (cachedData) {
Expand Down Expand Up @@ -133,7 +133,7 @@ - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
//Cache the response
if (data && data.length > 0) {
if (0 != self.maxAgeMinutes) {
[self.diskCache storeData:subData forKey:self.cacheKey];
[self.flickrKit.diskCache storeData:subData forKey:self.cacheKey];
}
[self processResponseData:subData];
} else {
Expand All @@ -156,10 +156,10 @@ - (NSMutableURLRequest *) createRequest:(NSError **)error {
newArgs[@"method"] = self.apiMethod;
newArgs[@"format"] = @"json";

FKURLBuilder *urlBuilder = [[FKURLBuilder alloc] init];
FKURLBuilder *urlBuilder = [[FKURLBuilder alloc] initWithFlickrKit:self.flickrKit];

NSURL *url = nil;
if ([FlickrKit sharedFlickrKit].isAuthorized) {
if (self.flickrKit.isAuthorized) {
url = [urlBuilder oauthURLFromBaseURL:[NSURL URLWithString:FKFlickrRESTAPI] method:FKHttpMethodGET params:newArgs];
} else {
NSString *query = [urlBuilder signedQueryStringFromParameters:newArgs];
Expand Down
6 changes: 4 additions & 2 deletions Classes/Network/FKImageUploadNetworkOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
#import "FKDUNetworkOperation.h"
#import "FKDataTypes.h"

@class FlickrKit;

@interface FKImageUploadNetworkOperation : FKDUNetworkOperation
@property (nonatomic, assign, readonly) CGFloat uploadProgress;

- (id) initWithImage:(DUImage *)image arguments:(NSDictionary *)args completion:(FKAPIImageUploadCompletion)completion;
- (id) initWithFlickrKit:(FlickrKit *)flickrKit image:(DUImage *)image arguments:(NSDictionary *)args completion:(FKAPIImageUploadCompletion)completion;

#if TARGET_OS_IOS
- (id) initWithAssetURL:(NSURL *)assetURL arguments:(NSDictionary *)args completion:(FKAPIImageUploadCompletion)completion;
- (id) initWithFlickrKit:(FlickrKit *)flickrKit assetURL:(NSURL *)assetURL arguments:(NSDictionary *)args completion:(FKAPIImageUploadCompletion)completion;
#endif

@end
Expand Down
9 changes: 6 additions & 3 deletions Classes/Network/FKImageUploadNetworkOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

@interface FKImageUploadNetworkOperation ()

@property (nonatomic, strong) FlickrKit *flickrKit;
@property (nonatomic, strong) DUImage *image;
@property (nonatomic, retain) NSString *tempFile;
@property (nonatomic, copy) FKAPIImageUploadCompletion completion;
Expand All @@ -28,9 +29,10 @@ @interface FKImageUploadNetworkOperation ()

@implementation FKImageUploadNetworkOperation

- (id) initWithImage:(DUImage *)image arguments:(NSDictionary *)args completion:(FKAPIImageUploadCompletion)completion; {
- (id) initWithFlickrKit:(FlickrKit *)flickrKit image:(DUImage *)image arguments:(NSDictionary *)args completion:(FKAPIImageUploadCompletion)completion; {
self = [super init];
if (self) {
self.flickrKit = flickrKit;
self.image = image;
self.args = args;
self.completion = completion;
Expand All @@ -39,9 +41,10 @@ - (id) initWithImage:(DUImage *)image arguments:(NSDictionary *)args completion:
}

#if TARGET_OS_IOS
- (id) initWithAssetURL:(NSURL *)assetURL arguments:(NSDictionary *)args completion:(FKAPIImageUploadCompletion)completion; {
- (id) initWithFlickrKit:(FlickrKit *)flickrKit assetURL:(NSURL *)assetURL arguments:(NSDictionary *)args completion:(FKAPIImageUploadCompletion)completion; {
self = [super init];
if (self) {
self.flickrKit = flickrKit;
self.image = nil;
self.assetURL = assetURL;
self.args = args;
Expand Down Expand Up @@ -93,7 +96,7 @@ - (NSMutableURLRequest *) createRequest:(NSError **)error {
//#endif

// Build a URL to the upload service
FKURLBuilder *urlBuilder = [[FKURLBuilder alloc] init];
FKURLBuilder *urlBuilder = [[FKURLBuilder alloc] initWithFlickrKit:self.flickrKit];
NSDictionary *args = [urlBuilder signedArgsFromParameters:newArgs method:FKHttpMethodPOST url:[NSURL URLWithString:@"https://api.flickr.com/services/upload/"]];

// Form multipart needs a boundary
Expand Down
4 changes: 4 additions & 0 deletions Classes/Network/FKURLBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ typedef enum {
FKHttpMethodPOST
} FKHttpMethod;

@class FlickrKit;

@interface FKURLBuilder : NSObject

- (id)initWithFlickrKit:(FlickrKit *)flickrKit;

#pragma mark - URL Encryption

- (NSURL *) oauthURLFromBaseURL:(NSURL *)inURL method:(FKHttpMethod)method params:(NSDictionary *)params;
Expand Down
40 changes: 30 additions & 10 deletions Classes/Network/FKURLBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,28 @@
#import "FlickrKit.h"
#import "FKUtilities.h"

@interface FKURLBuilder ()
@property (nonatomic, strong) FlickrKit *flickrKit;
@end

@implementation FKURLBuilder

- (id)initWithFlickrKit:(FlickrKit *)flickrKit {
self = [super init];
if (self) {
self.flickrKit = flickrKit;
}
return self;
}

- (id)init {
self = [super init];
if (self) {
NSAssert(false, @"FKURLBuilder must be initialised with a FlickrKit instance");
}
return self;
}

#pragma mark - URL Encryption

- (NSURL *) oauthURLFromBaseURL:(NSURL *)inURL method:(FKHttpMethod)method params:(NSDictionary *)params {
Expand Down Expand Up @@ -50,17 +70,17 @@ - (NSDictionary *) signedOAuthHTTPQueryParameters:(NSDictionary *)params baseURL
newArgs[@"oauth_timestamp"] = [NSString stringWithFormat:@"%f", time];
newArgs[@"oauth_version"] = @"1.0";
newArgs[@"oauth_signature_method"] = @"HMAC-SHA1";
newArgs[@"oauth_consumer_key"] = [FlickrKit sharedFlickrKit].apiKey;
newArgs[@"oauth_consumer_key"] = self.flickrKit.apiKey;

if (!params[@"oauth_token"] && [FlickrKit sharedFlickrKit].authToken) {
newArgs[@"oauth_token"] = [FlickrKit sharedFlickrKit].authToken;
if (!params[@"oauth_token"] && self.flickrKit.authToken) {
newArgs[@"oauth_token"] = self.flickrKit.authToken;
}

NSString *signatureKey = nil;
if ([FlickrKit sharedFlickrKit].authSecret) {
signatureKey = [NSString stringWithFormat:@"%@&%@", [FlickrKit sharedFlickrKit].secret, [FlickrKit sharedFlickrKit].authSecret];
if (self.flickrKit.authSecret) {
signatureKey = [NSString stringWithFormat:@"%@&%@", self.flickrKit.secret, self.flickrKit.authSecret];
} else {
signatureKey = [NSString stringWithFormat:@"%@&", [FlickrKit sharedFlickrKit].secret];
signatureKey = [NSString stringWithFormat:@"%@&", self.flickrKit.secret];
}

NSMutableString *baseString = [NSMutableString string];
Expand Down Expand Up @@ -100,12 +120,12 @@ - (NSString *) signedQueryStringFromParameters:(NSDictionary *)params {
//private
- (NSArray *) signedArgumentComponentsFromParameters:(NSDictionary *)params {
NSMutableDictionary *args = params ? [params mutableCopy] : [NSMutableDictionary dictionary];
if ([FlickrKit sharedFlickrKit].apiKey) {
args[@"api_key"] = [FlickrKit sharedFlickrKit].apiKey;
if (self.flickrKit.apiKey) {
args[@"api_key"] = self.flickrKit.apiKey;
}

NSMutableArray *argArray = [NSMutableArray array];
NSMutableString *sigString = [NSMutableString stringWithString:[FlickrKit sharedFlickrKit].secret ? [FlickrKit sharedFlickrKit].secret : @""];
NSMutableString *sigString = [NSMutableString stringWithString:self.flickrKit.secret ? self.flickrKit.secret : @""];
NSArray *sortedKeys = [[args allKeys] sortedArrayUsingSelector:@selector(compare:)];

for (NSString *key in sortedKeys) {
Expand All @@ -123,7 +143,7 @@ - (NSArray *) signedArgumentComponentsFromParameters:(NSDictionary *)params {
#pragma mark - Args as array

- (NSDictionary *) signedArgsFromParameters:(NSDictionary *)params method:(FKHttpMethod)method url:(NSURL *)url {
if ([FlickrKit sharedFlickrKit].isAuthorized) {
if (self.flickrKit.isAuthorized) {
return [self signedOAuthHTTPQueryParameters:params baseURL:url method:method];
} else {
NSMutableDictionary *returnDict = [NSMutableDictionary dictionary];
Expand Down
Loading