Last active
December 18, 2015 10:39
Revisions
-
Brayden revised this gist
Jun 30, 2013 . 2 changed files with 4 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -28,6 +28,7 @@ typedef NS_ENUM(NSUInteger, kAPIEndpoint) { kEndpointSessionStatus, // /session/status GET / / / kEndpointAccountNew, // /account/new / POST / / kEndpointAccountOrders, // /account/orders GET / / / kEndpointAccountOrderHistory, // /account/orders/{id} GET / / / kEndpointAccountWishlist, // /account/wishlist GET / POST / PUT / DELETE kEndpointShoprunnerNew, // /shoprunner/new / POST / / kEndpointShoprunnerDestroy, // /shoprunner/destroy / / / DELETE @@ -38,6 +39,7 @@ typedef NS_ENUM(NSUInteger, kAPIEndpoint) { kEndpointCheckoutGiftcard, // /checkout/giftcard / POST / / kEndpointAccountAddressBilling, // /account/addresses/billing GET / POST / PUT / DELETE kEndpointAccountAddressShipping, // /account/addresses/shipping GET / POST / PUT / DELETE kEndpointAccountCredits, // /account/credits GET / / / }; @interface BBStandardAPI : NSObject 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 charactersOriginal file line number Diff line number Diff line change @@ -77,6 +77,7 @@ + (NSString *)getEndpoint:(kAPIEndpoint)endpoint { else if (endpoint == kEndpointSessionStatus) return @"/session/status"; else if (endpoint == kEndpointAccountNew) return @"/account/new"; else if (endpoint == kEndpointAccountOrders) return @"/account/orders"; else if (endpoint == kEndpointAccountOrderHistory) return @"/account/order/%@"; else if (endpoint == kEndpointAccountWishlist) return @"/account/wishlist"; else if (endpoint == kEndpointShoprunnerNew) return @"/shoprunner/new"; else if (endpoint == kEndpointShoprunnerDestroy) return @"/shoprunner/destroy"; @@ -87,6 +88,7 @@ + (NSString *)getEndpoint:(kAPIEndpoint)endpoint { else if (endpoint == kEndpointCheckoutGiftcard) return @"/checkout/giftcard"; else if (endpoint == kEndpointAccountAddressBilling) return @"/account/addresses/billing"; else if (endpoint == kEndpointAccountAddressShipping) return @"/account/addresses/shipping"; else if (endpoint == kEndpointAccountCredits) return @"/account/credits"; return @""; } -
Brayden revised this gist
Jun 28, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ typedef NS_ENUM(NSUInteger, kAPIMethod) { DELETE }; typedef NS_ENUM(NSUInteger, kAPIEndpoint) { kEndpointCustom, // {path} GET / POST / PUT / DELETE kEndpointCategories, // /categories GET / / / kEndpointSubcategory, // /categories/{id} GET / / / @@ -38,7 +38,7 @@ typedef enum { kEndpointCheckoutGiftcard, // /checkout/giftcard / POST / / kEndpointAccountAddressBilling, // /account/addresses/billing GET / POST / PUT / DELETE kEndpointAccountAddressShipping, // /account/addresses/shipping GET / POST / PUT / DELETE }; @interface BBStandardAPI : NSObject -
Brayden revised this gist
Jun 28, 2013 . 3 changed files with 185 additions and 177 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ @class MBProgressHUD; typedef NS_ENUM(NSUInteger, kAPIMethod) { GET, POST, PUT, DELETE }; typedef enum { kEndpointCustom, // {path} GET / POST / PUT / DELETE 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 @property (nonatomic, strong) NSOperationQueue *queue; @property (nonatomic, strong) NSString *baseHost; @property (nonatomic, strong) NSArray *categories; + (BBStandardAPI *)sharedManager; + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParameters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error, NSInteger statusCode))completion; @end 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,133 @@ #import "BBStandardAPI.h" #import "MBProgressHUD.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 == kEndpointCustom) return @"%@"; else 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 andParameters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error, NSInteger statusCode))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]]; } UIView *view = [[UIApplication sharedApplication] keyWindow]; if (view) { [MBProgressHUD showHUDAddedTo:view animated:YES]; } [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; [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, [(NSHTTPURLResponse*)response statusCode]); }); } else { NSLog(@"-- BBStandardAPI -- Error: %@", error.description); } dispatch_async(dispatch_get_main_queue(), ^{ if (view != nil) { [MBProgressHUD hideHUDForView:view animated:YES]; } [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; }); }]; } @end 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 charactersOriginal file line number Diff line number Diff line change @@ -1,177 +0,0 @@ -
Brayden revised this gist
Jun 20, 2013 . No changes.There are no files selected for viewing
-
Brayden revised this gist
Jun 20, 2013 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -47,7 +47,7 @@ + (BBStandardAPI *)sharedManager; @property (nonatomic, strong) NSString *baseHost; @property (nonatomic, strong) NSArray *categories; + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParameters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error, NSInteger statusCode))completion; @end @@ -150,7 +150,7 @@ + (NSString *)getEndpoint:(kAPIEndpoint)endpoint { return @""; } + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParameters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error, NSInteger statusCode))completion { NSMutableString *parameter = [[NSMutableString alloc] initWithString:[BBStandardAPI getEndpoint:endpoint]]; NSArray *parts = [parameter componentsSeparatedByString:@"%@"]; if(parts.count > 1) { @@ -167,7 +167,7 @@ + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method op NSError *err; NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&err]; dispatch_async(dispatch_get_main_queue(), ^{ completion(result, nil, [(NSHTTPURLResponse*)response statusCode]); }); } else NSLog(@"-- BBStandardAPI -- Error: %@", error.description); -
Brayden revised this gist
Jun 14, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -47,7 +47,7 @@ + (BBStandardAPI *)sharedManager; @property (nonatomic, strong) NSString *baseHost; @property (nonatomic, strong) NSArray *categories; + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParameters:(NSArray *)params completion:(void (^)(NSDictionary *dictionary, NSError *error))completion; @end @@ -150,7 +150,7 @@ + (NSString *)getEndpoint:(kAPIEndpoint)endpoint { return @""; } + (void)queryAPIEndpoint:(kAPIEndpoint)endpoint withMethod:(kAPIMethod)method options:(NSDictionary *)options andParameters:(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) { -
Brayden revised this gist
Jun 14, 2013 . 1 changed file with 137 additions and 47 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,53 @@ #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 @@ -16,72 +59,119 @@ + (APIWrapper *)sharedManager; #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 -
Brayden created this gist
Jun 12, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,87 @@ @interface APIWrapper : NSObject @property (nonatomic, strong) NSArray *categories; @property (nonatomic, assign) NSInteger loadingCount; + (void)apiEndpoint:(NSString *)endpoint completionBlock:(void (^)(NSDictionary *dictionary, NSError *error))completion; + (APIWrapper *)sharedManager; @end #import "APIWrapper.h" @implementation APIWrapper + (void)apiEndpoint:(NSString *)endpoint completionBlock:(void (^)(NSDictionary *dictionary, NSError *error))completion { NSOperationQueue *queue = [[NSOperationQueue alloc] init]; queue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount; [queue setName:@"com.brandingbrand.api"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", BASE_URL, endpoint]]]; [request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"]; [request setTimeoutInterval:15.0f]; [[APIWrapper sharedManager] setLoadingCount:[[APIWrapper sharedManager] loadingCount] + 1]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *resposne, NSData *data, NSError *error) { if(data != nil) { NSError *jsonError; NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError]; dispatch_async(dispatch_get_main_queue(), ^{ completion(dictionary, nil); }); } else { NSLog(@"API Error: %@", error.description); } [[APIWrapper sharedManager] setLoadingCount:[[APIWrapper sharedManager] loadingCount] - 1]; }]; } #pragma mark - Singleton Setup - + (APIWrapper *)sharedManager { static APIWrapper *sharedManager; static dispatch_once_t token = 0; dispatch_once(&token, ^{ sharedManager = [[APIWrapper alloc] init]; }); return sharedManager; } - (id)init { if (self = [super init]) { self.categories = [[NSArray alloc] init]; } return self; } #pragma mark - Setter Methods - - (void)setLoadingCount:(NSInteger)loadingCount { _loadingCount = loadingCount; if(loadingCount > 0) [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; else [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } @end