-
Notifications
You must be signed in to change notification settings - Fork 48
/
YLPClient.m
70 lines (59 loc) · 2.43 KB
/
YLPClient.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// YLPClient.m
// Pods
//
// Created by David Chen on 12/7/15.
//
//
#import <Foundation/Foundation.h>
#import <TDOAuth/TDOAuth.h>
#import "YLPClient.h"
#import "YLPClientPrivate.h"
NSString *const kYLPAPIHost = @"api.yelp.com";
@implementation YLPClient
- (instancetype)initWithConsumerKey:(NSString *)consumerKey
consumerSecret:(NSString *)consumerSecret
token:(NSString *)token
tokenSecret:(NSString *)tokenSecret {
if (self = [super init]) {
_consumerKey = consumerKey;
_consumerSecret = consumerSecret;
_token = token;
_tokenSecret = tokenSecret;
}
return self;
}
- (NSURLRequest *)requestWithPath:(NSString *)path {
return [self requestWithPath:path params:nil];
}
- (NSURLRequest *)requestWithPath:(NSString *)path params:(NSDictionary *)params {
return [TDOAuth URLRequestForPath:path
GETParameters:params
scheme:@"https"
host:kYLPAPIHost
consumerKey:self.consumerKey
consumerSecret:self.consumerSecret
accessToken:self.token
tokenSecret:self.tokenSecret];
}
- (void)queryWithRequest:(NSURLRequest *)request
completionHandler:(void (^)(NSDictionary *jsonResponse, NSError *error))completionHandler {
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *responseJSON;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
// This case handles cases where the request was processed by the API, thus
// resulting in a JSON object being passed back into `data`.
if (!error) {
responseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
}
if (!error && httpResponse.statusCode == 200) {
completionHandler(responseJSON, nil);
} else {
// If a request fails due to systematic errors with the API then an NSError will be returned.
error = error ? error : [NSError errorWithDomain:[[NSBundle mainBundle] bundleIdentifier] code:httpResponse.statusCode userInfo:responseJSON];
completionHandler(nil, error);
}
}] resume];
}
@end