Last active
December 18, 2015 10:39
-
-
Save Brayden/5769659 to your computer and use it in GitHub Desktop.
BB Standard iOS App API Wrapper. Direct questions to Brayden.
This file contains hidden or 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> | |
typedef enum { | |
GET, | |
POST, | |
PUT, | |
DELETE | |
} kAPIMethod; | |
typedef enum { | |
kEndpointCategories, // /categories GET / / / | |
kEndpointSubcategory, // /categories/{id} GET / / / | |
kEndpointCategoryProducts, // /categories/{id}/products GET / / / | |
kEndpointProduct, // /products/{id} GET / / / | |
kEndpointProductSearch, // /products{?q} GET / / / | |
kEndpointCart, // /checkout/cart GET / POST / PUT / DELETE | |
kEndpointPromo, // /checkout/promo / POST / / | |
kEndpointCheckoutPaypal, // /checkout/paypal / POST / / | |
kEndpointCheckoutGoogle, // /checkout/google / POST / / | |
kEndpointCheckoutCC, // /checkout/cc / POST / / | |
kEndpointCheckoutConfirm, // /checkout/confirm GET / POST / / | |
kEndpointCheckoutReceipt, // /checkout/receipt GET / / / | |
kEndpointCheckoutShipping, // /checkout/shipping_methods GET / / PUT / | |
kEndpointCheckoutDestinations, // /checkout/destinations GET / / PUT / | |
kEndpointSessionNew, // /session/new / POST / / | |
kEndpointSessionDestroy, // /session/destroy / / / DELETE | |
kEndpointSessionStatus, // /session/status GET / / / | |
kEndpointAccountNew, // /account/new / POST / / | |
kEndpointAccountOrders, // /account/orders GET / / / | |
kEndpointAccountWishlist, // /account/wishlist GET / POST / PUT / DELETE | |
kEndpointShoprunnerNew, // /shoprunner/new / POST / / | |
kEndpointShoprunnerDestroy, // /shoprunner/destroy / / / DELETE | |
kEndpointAccountLoyalty, // /account/loyalty/show GET / / / | |
kEndpointAccountLoyaltyStatements, // /account/loyalty/statements GET / / / | |
kEndpointAccountLoyaltyTransaction, // /account/loyalty/transactions GET / / / | |
kEndpointGiftcards, // /giftcards GET / / / | |
kEndpointCheckoutGiftcard, // /checkout/giftcard / POST / / | |
kEndpointAccountAddressBilling, // /account/addresses/billing GET / POST / PUT / DELETE | |
kEndpointAccountAddressShipping, // /account/addresses/shipping GET / POST / PUT / DELETE | |
} kAPIEndpoint; | |
@interface BBStandardAPI : NSObject | |
+ (BBStandardAPI *)sharedManager; | |
@property (nonatomic, strong) NSOperationQueue *queue; | |
@property (nonatomic, strong) NSString *baseHost; | |
@property (nonatomic, strong) NSArray *categories; | |
+ (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParamaters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error))completion; | |
@end | |
#import "BBStandardAPI.h" | |
@implementation BBStandardAPI | |
#pragma mark - Singleton - | |
+ (BBStandardAPI *)sharedManager { | |
static BBStandardAPI *sharedManager; | |
static dispatch_once_t token = 0; | |
dispatch_once(&token, ^{ | |
sharedManager = [[BBStandardAPI alloc] init]; | |
}); | |
return sharedManager; | |
} | |
- (id)init { | |
if (self = [super init]) { | |
self.queue = [[NSOperationQueue alloc] init]; | |
self.queue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount; | |
self.queue.name = @"com.brandingbrand.api"; | |
} | |
return self; | |
} | |
#pragma mark - Wrapper Methods - | |
+ (NSMutableURLRequest *)newRequestWithMethod:(kAPIMethod)method endpoint:(NSString *)endpoint andOptions:(NSDictionary *)options { | |
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", [[BBStandardAPI sharedManager] baseHost], endpoint]]]; | |
if(method == GET) [request setHTTPMethod:@"GET"]; | |
else if(method == POST) [request setHTTPMethod:@"POST"]; | |
else if(method == PUT) [request setHTTPMethod:@"PUT"]; | |
else if(method == DELETE) [request setHTTPMethod:@"DELETE"]; | |
if(options) { | |
NSError *err; | |
NSData *data = [NSJSONSerialization dataWithJSONObject:options options:NSJSONWritingPrettyPrinted error:&err]; | |
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | |
[request setValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"]; | |
[request setHTTPBody:data]; | |
} else { | |
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; | |
[request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"]; | |
} | |
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; | |
[request setTimeoutInterval:20.0f]; | |
return request; | |
} | |
+ (NSString *)getEndpoint:(kAPIEndpoint)endpoint { | |
if(endpoint == kEndpointCategories) return @"/categories"; | |
else if(endpoint == kEndpointSubcategory) return @"/categories/%@"; | |
else if(endpoint == kEndpointCategoryProducts) return @"/categories/%@/products"; | |
else if(endpoint == kEndpointProduct) return @"/products/%@"; | |
else if(endpoint == kEndpointProductSearch) return @"/products?%@"; | |
else if(endpoint == kEndpointCart) return @"/checkout/cart"; | |
else if(endpoint == kEndpointPromo) return @"/checkout/promo"; | |
else if(endpoint == kEndpointCheckoutPaypal) return @"/checkout/paypal"; | |
else if(endpoint == kEndpointCheckoutGoogle) return @"/checkout/google"; | |
else if(endpoint == kEndpointCheckoutCC) return @"/checkout/cc"; | |
else if(endpoint == kEndpointCheckoutConfirm) return @"/checkout/confirm"; | |
else if(endpoint == kEndpointCheckoutReceipt) return @"/checkout/receipt"; | |
else if(endpoint == kEndpointCheckoutShipping) return @"/checkout/shipping_methods"; | |
else if(endpoint == kEndpointCheckoutDestinations) return @"/checkout/destinations"; | |
else if(endpoint == kEndpointSessionNew) return @"/session/new"; | |
else if(endpoint == kEndpointSessionDestroy) return @"/session/destroy"; | |
else if(endpoint == kEndpointSessionStatus) return @"/session/status"; | |
else if(endpoint == kEndpointAccountNew) return @"/account/new"; | |
else if(endpoint == kEndpointAccountOrders) return @"/account/orders"; | |
else if(endpoint == kEndpointAccountWishlist) return @"/account/wishlist"; | |
else if(endpoint == kEndpointShoprunnerNew) return @"/shoprunner/new"; | |
else if(endpoint == kEndpointShoprunnerDestroy) return @"/shoprunner/destroy"; | |
else if(endpoint == kEndpointAccountLoyalty) return @"/account/loyalty/show"; | |
else if(endpoint == kEndpointAccountLoyaltyStatements) return @"/account/loyalty/statements"; | |
else if(endpoint == kEndpointAccountLoyaltyTransaction) return @"/account/loyalty/transactions"; | |
else if(endpoint == kEndpointGiftcards) return @"/giftcards"; | |
else if(endpoint == kEndpointCheckoutGiftcard) return @"/checkout/giftcard"; | |
else if(endpoint == kEndpointAccountAddressBilling) return @"/account/addresses/billing"; | |
else if(endpoint == kEndpointAccountAddressShipping) return @"/account/addresses/shipping"; | |
return @""; | |
} | |
+ (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParamaters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error))completion { | |
NSMutableString *parameter = [[NSMutableString alloc] initWithString:[BBStandardAPI getEndpoint:endpoint]]; | |
NSArray *parts = [parameter componentsSeparatedByString:@"%@"]; | |
if(parts.count > 1) { | |
parameter = [@"" mutableCopy]; | |
for(int index = 0; index < parts.count - 1; index++) { | |
[parameter appendFormat:@"%@%@", parts[index], params[index]]; | |
} | |
[parameter appendFormat:@"%@", parts[parts.count - 1]]; | |
} | |
[NSURLConnection sendAsynchronousRequest:[BBStandardAPI newRequestWithMethod:method endpoint:parameter andOptions:options] queue:[[BBStandardAPI sharedManager] queue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { | |
if(data != nil) { | |
NSError *err; | |
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&err]; | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
completion(result, nil); | |
}); | |
} else | |
NSLog(@"-- BBStandardAPI -- Error: %@", error.description); | |
}]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment