-
Notifications
You must be signed in to change notification settings - Fork 16
/
WPResource.m
177 lines (133 loc) · 5.65 KB
/
WPResource.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// WePay.m
// WePay
//
// Created by WePay on 10/2/13.
// Copyright (c) 2013 WePay. All rights reserved.
//
#import "WPResource.h"
@interface WPResource()
// get api request url
+ (NSURL *) apiUrlWithEndpoint: (NSString *) endpoint;
// convert data from WePay to error object
+ (NSError *) errorFromResponse: (NSDictionary *) dictionary;
// Helper for makeRequestToEndPoint to process API call request response
+ (void) processResponse: (NSURLResponse *) response data: (NSData *) data error: (NSError *)error successBlock: (WPSuccessBlock) successHandler errorHandler: (WPErrorBlock) errorHandler;
@end
@implementation WPResource
/* URL roots to make API calls. */
// prod
static NSString * const prodApiUrlRoot = @"https://wepayapi.com/v2/";
// stage
static NSString * const stageApiUrlRoot = @"https://stage.wepayapi.com/v2/";
// Version number to be appended to URL root.
static NSString * const version = @"v2";
// Returns an NSURL for API Call Endpoint
+ (NSURL *) apiUrlWithEndpoint: (NSString *) endpoint {
[WePay validateCredentials];
NSString * rootUrl;
if ([WePay isProduction]) {
rootUrl = prodApiUrlRoot;
}
else {
rootUrl = stageApiUrlRoot;
}
return [[NSURL URLWithString: [NSString stringWithFormat: @"%@", rootUrl]] URLByAppendingPathComponent: endpoint];
}
# pragma mark API Requests and Handling
// Converts API Call Error into NSError Object
+ (NSError *) errorFromResponse: (NSDictionary *) dictionary {
NSMutableDictionary * details = [NSMutableDictionary dictionary];
NSInteger errorCode;
NSString * errorText;
NSString * errorCategory;
if([dictionary objectForKey: @"error_code"] != (id)[NSNull null]) {
errorCode = [[dictionary objectForKey: @"error_code"] intValue];
}
else {
// This should not happen
errorCode = WPErrorUnknown;
}
if([dictionary objectForKey: @"error_description"] != (id)[NSNull null] &&
[[dictionary objectForKey: @"error_description"] length]) {
errorText = [dictionary objectForKey: @"error_description"];
}
else if(dictionary == nil) {
// This should not happen
errorText = WPNoDataReturnedErrorMessage;
}
else {
// This should not happen
errorText = WPUnexpectedErrorMessage;
}
if([dictionary objectForKey: @"error"] != (id)[NSNull null] &&
[[dictionary objectForKey: @"error"] length]) {
errorCategory = [dictionary objectForKey: @"error"];
}
else {
// This should not happen
errorCategory = WPErrorCategoryNone;
}
[details setValue: errorText forKey: NSLocalizedDescriptionKey];
[details setValue: errorCategory forKey: WPErrorCategoryKey];
return [NSError errorWithDomain: WePayAPIDomain code: errorCode userInfo: details];
}
// Processes Request Response
+ (void) processResponse: (NSURLResponse *) response data: (NSData *) data error: (NSError *) error successBlock: (WPSuccessBlock) successHandler errorHandler: (WPErrorBlock) errorHandler {
NSDictionary * dictionary = nil;
if([data length] >= 1)
{
dictionary = [NSJSONSerialization JSONObjectWithData: data options: kNilOptions error: nil];
}
if(dictionary != nil && error == nil)
{
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if(statusCode == 200) {
successHandler(dictionary);
}
else {
errorHandler([self errorFromResponse: dictionary]);
}
}
else if(dictionary == nil && error == nil) {
errorHandler([self errorFromResponse: dictionary]);
}
else if (error != nil) {
errorHandler(error);
}
}
// Makes API Call Request
+ (void) makeRequestToEndPoint: (NSString *) endpoint values: (NSDictionary *) params accessToken: (NSString *) accessToken successBlock: (WPSuccessBlock) successHandler errorHandler: (WPErrorBlock) errorHandler {
NSURL * callUrl = [self apiUrlWithEndpoint: endpoint];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL: callUrl];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"charset" forHTTPHeaderField:@"utf-8"];
[request setValue: [NSString stringWithFormat: @"WePay IOS SDK %@", version] forHTTPHeaderField:@"User-Agent"];
// Set access token
if(accessToken != nil) {
[request setValue: [NSString stringWithFormat: @"Bearer: %@", accessToken] forHTTPHeaderField:@"Authorization"];
}
// Set Api Version
if([WePay apiVersion]) {
[request setValue: [NSString stringWithFormat: @"%@", [WePay apiVersion]] forHTTPHeaderField:@"Api-Version"];
}
NSError *parseError = nil;
// Get json from nsdictionary parameter
[request setHTTPBody: [NSJSONSerialization dataWithJSONObject: params options: kNilOptions error: &parseError]];
if(parseError) {
errorHandler(parseError);
}
else
{
NSOperationQueue *queue = [NSOperationQueue mainQueue];
[NSURLConnection sendAsynchronousRequest: request
queue: queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError * requestError) {
// Process response from server.
[self processResponse:response data: data error: requestError successBlock:successHandler errorHandler: errorHandler];
}];
}
}
@end