Skip to content

Commit

Permalink
Adding some broken code for more data calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
kreeger committed Apr 13, 2014
1 parent fd816d9 commit 9aac525
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 24 deletions.
22 changes: 15 additions & 7 deletions Classes/BDKUntappd.h
Expand Up @@ -213,23 +213,21 @@ extern NSString * const BDKUntappdBaseURL;
@discussion See https://untappd.com/api/docs#details
@param checkinID Required; the checkin ID for which to retrieve a information.
@param compact If `YES`, only basic info is returned; if `NO`, a full object including checkins and a beer list will
be returned.
@param completion A block to be called upon completion; will get passed the response body and error if one occurred.
*/
- (void)infoForCheckin:(NSNumber *)checkinID compact:(BOOL)compact completion:(BDKUntappdResultBlock)completion;
- (void)infoForCheckin:(NSNumber *)checkinID completion:(BDKUntappdResultBlock)completion;

/**
Gets information for a given user. If userID is nil, info for the currently-logged-in user will be retrieved.
Gets information for a given user. If username is nil, info for the currently-logged-in user will be retrieved.
@discussion See https://untappd.com/api/docs#user_info
@param userID Required; the user ID for which to retrieve a information.
@param username Required; the username for which to retrieve a information.
@param compact If `YES`, only basic info is returned; if `NO`, a full object including checkins and a beer list will
be returned.
@param completion A block to be called upon completion; will get passed the response body and error if one occurred.
*/
- (void)infoForUser:(NSNumber *)userID compact:(BOOL)compact completion:(BDKUntappdResultBlock)completion;
- (void)infoForUser:(NSString *)username compact:(BOOL)compact completion:(BDKUntappdResultBlock)completion;


#pragma mark - User detail calls
Expand Down Expand Up @@ -282,7 +280,7 @@ extern NSString * const BDKUntappdBaseURL;
*/
- (void)distinctBeersForUser:(NSNumber *)userID compact:(BOOL)compact completion:(BDKUntappdResultBlock)completion;

#pragma mark - Search calls
#pragma mark - Search and trending calls

/**
Searches the Untappd database for a brewery (or breweries) matching a provided search term.
Expand All @@ -306,4 +304,14 @@ extern NSString * const BDKUntappdBaseURL;
*/
- (void)searchForBeer:(NSString *)query sortBy:(BDKUntappdSortType)sortBy completion:(BDKUntappdResultBlock)completion;

/**
Gets a list of trending beers globally.
@discussion See https://untappd.com/api/docs#trending
@param completion A block to be called upon completion; will get passed the response body and error if one occurred.
*/
- (void)trendingBeers:(BDKUntappdResultBlock)completion;


@end
140 changes: 123 additions & 17 deletions Classes/BDKUntappd.m
Expand Up @@ -10,6 +10,8 @@
#import "BDKUntappdModels.h"
#import "BDKUntappdParser.h"

#define BDKStringFromBOOL(val) [NSString stringWithFormat:@"%@", val ? @"true" : @"false"]

NSString * const BDKUntappdBaseURL = @"http://api.untappd.com/v4";
NSString * const BDKUntappdAuthenticateURL = @"https://untappd.com/oauth/authenticate";
NSString * const BDKUntappdAuthorizeURL = @"https://untappd.com/oauth/authorize";
Expand All @@ -32,22 +34,23 @@ - (instancetype)initWithClientId:(NSString *)clientId
redirectUrl:(NSString *)redirectUrl {
self = [super initWithBaseURL:[NSURL URLWithString:BDKUntappdBaseURL]];
if (!self) return nil;

_clientId = clientId;
_clientSecret = clientSecret;
_redirectUrl = redirectUrl;
_parser = [BDKUntappdParser new];

return self;
}


#pragma mark - Authentication

- (NSURLRequest *)authenticationURLRequest {
NSDictionary *params = @{@"client_id": self.clientId,
@"response_type": @"code",
@"redirect_url": self.redirectUrl};

NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET"
URLString:BDKUntappdAuthenticateURL
parameters:params
Expand All @@ -62,7 +65,7 @@ - (void)authorizeForAccessCode:(NSString *)accessCode completion:(BDKUntappdResu
@"redirect_url": self.redirectUrl,
@"response_type": @"code",
@"code": accessCode};

[self GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
if (responseObject[@"response"][@"access_token"]) {
self.accessToken = responseObject[@"response"][@"access_token"];
Expand All @@ -73,14 +76,15 @@ - (void)authorizeForAccessCode:(NSString *)accessCode completion:(BDKUntappdResu
}];
}


#pragma mark - Checkin feeds

- (void)checkinsForFriendsWithMaxID:(NSNumber *)maxID
limit:(NSInteger)limit
completion:(BDKUntappdResultBlock)completion {
NSString *url = @"/v4/checkin/recent";
NSMutableDictionary *params = [self requestParamsWithMinID:nil maxID:maxID limit:limit];

[self GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
completion([self.parser checkinsFromResponseObject:responseObject], nil);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
Expand All @@ -93,10 +97,10 @@ - (void)checkinsForUser:(NSString *)username
limit:(NSInteger)limit
completion:(BDKUntappdResultBlock)completion {
NSAssert(!!username || !!self.accessToken, @"Either username or a saved access token must be supplied.");

NSString *url = [NSString stringWithFormat:@"/v4/user/checkins%@%@", username ? @"/" : @"", username ?: @""];
NSMutableDictionary *params = [self requestParamsWithMinID:nil maxID:maxID limit:limit];

[self GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
completion([self.parser checkinsFromResponseObject:responseObject], nil);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
Expand All @@ -116,7 +120,7 @@ - (void)publicFeedForLatitude:(float)latitude
if (latitude > 0) params[@"lat"] = @(latitude);
if (longitude > 0) params[@"lng"] = @(longitude);
if (radius > 0) params[@"radius"] = @(radius);

[self GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
completion([self.parser checkinsFromResponseObject:responseObject], nil);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
Expand All @@ -130,10 +134,10 @@ - (void)checkinsForVenue:(NSNumber *)venueID
limit:(NSInteger)limit
completion:(BDKUntappdResultBlock)completion {
NSAssert(!!venueID, @"A venue ID must be supplied.");

NSString *url = [NSString stringWithFormat:@"/v4/venue/checkins/%@", venueID];
NSMutableDictionary *params = [self requestParamsWithMinID:minID maxID:maxID limit:limit];

[self GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
completion([self.parser checkinsFromResponseObject:responseObject], nil);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
Expand All @@ -147,10 +151,10 @@ - (void)checkinsForBeer:(NSNumber *)beerID
limit:(NSInteger)limit
completion:(BDKUntappdResultBlock)completion {
NSAssert(!!beerID, @"A beer ID must be supplied.");

NSString *url = [NSString stringWithFormat:@"/v4/beer/checkins/%@", beerID];
NSMutableDictionary *params = [self requestParamsWithMinID:minID maxID:maxID limit:limit];

[self GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
completion([self.parser checkinsFromResponseObject:responseObject], nil);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
Expand All @@ -163,18 +167,120 @@ - (void)checkinsForBrewery:(NSNumber *)breweryID
maxID:(NSNumber *)maxID
limit:(NSInteger)limit
completion:(BDKUntappdResultBlock)completion {
NSAssert(!!breweryID, @"A beer ID must be supplied.");
NSAssert(!!breweryID, @"A brewery ID must be supplied.");

NSString *url = [NSString stringWithFormat:@"/v4/brewery/checkins/%@", breweryID];
NSMutableDictionary *params = [self requestParamsWithMinID:minID maxID:maxID limit:limit];

[self GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
completion([self.parser checkinsFromResponseObject:responseObject], nil);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[self handleError:error forTask:task completion:completion];
}];
}


#pragma mark - Object detail calls

- (void)infoForBrewery:(NSNumber *)breweryID compact:(BOOL)compact completion:(BDKUntappdResultBlock)completion {
NSAssert(!!breweryID, @"A brewery ID must be supplied.");

NSString *url = [NSString stringWithFormat:@"/v4/brewery/info/%@", breweryID];
NSMutableDictionary *params = [self authorizationParamsWithParams:@{@"compact": BDKStringFromBOOL(compact)}];

[self GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
completion([self.parser breweryFromResponseObject:responseObject], nil);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[self handleError:error forTask:task completion:completion];
}];
}

- (void)infoForBeer:(NSNumber *)beerID compact:(BOOL)compact completion:(BDKUntappdResultBlock)completion {
NSAssert(!!beerID, @"A beer ID must be supplied.");

NSString *url = [NSString stringWithFormat:@"/v4/beer/info/%@", beerID];
NSMutableDictionary *params = [self authorizationParamsWithParams:@{@"compact": BDKStringFromBOOL(compact)}];

[self GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
completion([self.parser beerFromResponseObject:responseObject], nil);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[self handleError:error forTask:task completion:completion];
}];
}

- (void)infoForVenue:(NSNumber *)venueID compact:(BOOL)compact completion:(BDKUntappdResultBlock)completion {
NSAssert(!!venueID, @"A venue ID must be supplied.");

NSString *url = [NSString stringWithFormat:@"/v4/venue/info/%@", beerID];
NSMutableDictionary *params = [self authorizationParamsWithParams:@{@"compact": BDKStringFromBOOL(compact)}];

[self GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
completion([self.parser venueFromResponseObject:responseObject], nil);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[self handleError:error forTask:task completion:completion];
}];
}

- (void)infoForCheckin:(NSNumber *)checkinID completion:(BDKUntappdResultBlock)completion {
NSAssert(!!checkinID, @"A checkin ID must be supplied.");

NSString *url = [NSString stringWithFormat:@"/v4/checkin/view/%@", checkinID];
NSMutableDictionary *params = [self authorizationParamsWithParams:nil];

[self GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
completion([self.parser checkinFromResponseObject:responseObject], nil);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[self handleError:error forTask:task completion:completion];
}];
}

- (void)infoForUser:(NSNumber *)username compact:(BOOL)compact completion:(BDKUntappdResultBlock)completion {
NSAssert(!!username || !!self.accessToken, @"Either username or a saved access token must be supplied.");

NSString *url = [NSString stringWithFormat:@"/v4/user/info%@%@", username ? @"/" : @"", username ?: @""];
NSMutableDictionary *params = [self authorizationParamsWithParams:nil];

[self GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
completion([self.parser userFromResponseObject:responseObject], nil);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[self handleError:error forTask:task completion:completion];
}];
}


#pragma mark - User detail calls

- (void)badgesForUser:(NSNumber *)userID compact:(BOOL)compact completion:(BDKUntappdResultBlock)completion {

}

- (void)friendsForUser:(NSNumber *)userID compact:(BOOL)compact completion:(BDKUntappdResultBlock)completion {

}

- (void)wishListForUser:(NSNumber *)userID compact:(BOOL)compact completion:(BDKUntappdResultBlock)completion {

}

- (void)distinctBeersForUser:(NSNumber *)userID compact:(BOOL)compact completion:(BDKUntappdResultBlock)completion {

}


#pragma mark - Search and trending calls

- (void)searchForBrewery:(NSString *)query completion:(BDKUntappdResultBlock)completion {

}

- (void)searchForBeer:(NSString *)query sortBy:(BDKUntappdSortType)sortBy completion:(BDKUntappdResultBlock)completion {

}

- (void)trendingBeers:(BDKUntappdResultBlock)completion {

}

#pragma mark - Private methods

- (NSDictionary *)authorizationParams {
Expand All @@ -187,7 +293,7 @@ - (NSDictionary *)authorizationParams {

- (NSMutableDictionary *)authorizationParamsWithParams:(NSDictionary *)params {
if ([params count] == 0) return [[self authorizationParams] mutableCopy];

NSMutableDictionary *merge = [[self authorizationParams] mutableCopy];
[params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
merge[key] = obj;
Expand All @@ -206,7 +312,7 @@ - (NSMutableDictionary *)requestParamsWithMinID:(NSNumber *)minID maxID:(NSNumbe

- (void)handleError:(NSError *)error forTask:(NSURLSessionDataTask *)task completion:(BDKUntappdResultBlock)completion {
NSLog(@"API ERROR. %@", [error localizedDescription]);

if (completion) {
completion(nil, error);
}
Expand Down

0 comments on commit 9aac525

Please sign in to comment.