-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathYoAPIClient.m
173 lines (145 loc) · 5.91 KB
/
YoAPIClient.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
//
// YoAPIClient.m
// Yo
//
// Created by Or Arbel on 8/21/14.
//
//
#import "YoAPIClient.h"
#import "YOUDID.h"
#ifdef DEBUG
//#define YO_BASE_URL_PROD @"http://yo.ngrok.io"
#define YO_BASE_URL_PROD @"https://api.justyo.co"
#else
#define YO_BASE_URL_PROD @"https://api.justyo.co"
#endif
#define YO_BASE_URL_STAGE @"http://api-dev.herokuapp.com"
//#define YO_BASE_URL_DEV @"http://0.0.0.0:5001"
#define YO_BASE_URL_DEV @"http://yo.ngrok.io"
#define YoJavascriptWebToken @"JWT"
@implementation YoAPIClient
typedef void (^YoAPIResponseBlock)(AFHTTPRequestOperation *operation, id responseObject);
typedef void (^YoAPIErrorBlock)(AFHTTPRequestOperation *operation, NSError *error);
- (instancetype)initWithAccessToken:(NSString *)accessToken
{
self = [self initWithBaseURL:[NSURL URLWithString:YO_BASE_URL_PROD]];
if (self != nil) {
// setup
self.accessToken = accessToken;
}
return self;
}
- (instancetype)init
{
return [self initWithBaseURL:[NSURL URLWithString:YO_BASE_URL_PROD]];
}
- (instancetype)initWithBaseURL:(NSURL *)url
{
self = [super initWithBaseURL:url];
if (self != nil) {
self.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
self.securityPolicy.allowInvalidCertificates = YES;
self.securityPolicy.validatesDomainName = NO;
AFJSONResponseSerializer *YoAFJSONResponseSerializer= [AFJSONResponseSerializer serializer];
YoAFJSONResponseSerializer.removesKeysWithNullValues = YES;
self.responseSerializer = YoAFJSONResponseSerializer;
self.requestSerializer = [AFJSONRequestSerializer serializer];
[self.requestSerializer setValue:[YOUDID value]
forHTTPHeaderField:@"X-RPC-UDID"];
[self.requestSerializer setValue:@"http://jobs.justyo.co"
forHTTPHeaderField:@"X-HACKER-JOBS"];
}
return self;
}
/**
Must call this before makeing any request against the server.
*/
- (void)updateHeaders
{
[self.requestSerializer clearAuthorizationHeader];
if (self.accessToken != nil) {
[self.requestSerializer setValue:MakeString(@"Bearer %@", self.accessToken)
forHTTPHeaderField:@"Authorization"];
}
}
#pragma mark Intercepting
- (AFHTTPRequestOperation *)POST:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
YoAPIResponseBlock interceptedSuccessBlock = [self interceptBlockResponseObject:success];
YoAPIErrorBlock interceptedFailureBlock = [self interceptBlockWithError:failure];
[self updateHeaders];
return [super POST:URLString
parameters:parameters
success:interceptedSuccessBlock
failure:interceptedFailureBlock];
}
- (AFHTTPRequestOperation *)POST:(NSString *)URLString
parameters:(id)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
YoAPIResponseBlock interceptedSuccessBlock = [self interceptBlockResponseObject:success];
YoAPIErrorBlock interceptedFailureBlock = [self interceptBlockWithError:failure];
[self updateHeaders];
return [super POST:URLString
parameters:parameters
constructingBodyWithBlock:block
success:interceptedSuccessBlock
failure:interceptedFailureBlock];
}
#pragma mark - Interception
- (YoAPIResponseBlock)interceptBlockResponseObject:(YoAPIResponseBlock)block {
__weak YoAPIClient *weakSelf = self;
YoAPIResponseBlock interceptedBlock = ^(AFHTTPRequestOperation *operation, id responseObject)
{
[weakSelf serverDidSendResponse:responseObject
withStatusCode:operation.response.statusCode];
if (block) {
block(operation, responseObject);
}
};
return interceptedBlock;
}
- (YoAPIErrorBlock)interceptBlockWithError:(YoAPIErrorBlock)block {
__weak YoAPIClient *weakSelf = self;
YoAPIErrorBlock interceptedBlock = ^(AFHTTPRequestOperation *operation, NSError *error)
{
[weakSelf serverDidSendResponse:operation.responseObject
withStatusCode:operation.response.statusCode];
if (block) {
block(operation, error);
}
};
return interceptedBlock;
}
- (void)serverDidSendResponse:(id)responseObject withStatusCode:(NSInteger)statusCode {
[self processResponseObject:responseObject];
[self processStatusCode:statusCode];
}
- (void)processResponseObject:(id)responseObject {
if ([responseObject respondsToSelector:@selector(valueForKey:)]) {
// JWT
NSString *updatedToken = [responseObject valueForKey:YoJavascriptWebToken];
if (updatedToken != nil && updatedToken.length > 0) {
[YoApp currentSession].accessToken = updatedToken;
}
}
}
- (void)processStatusCode:(NSInteger)statusCode {
if (statusCode == 401 && [[YoApp currentSession] isLoggedIn]) {
[[YoApp currentSession] logout];
#ifndef IS_APP_EXTENSION
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
YoAlert *yoAlert = [[YoAlert alloc] initWithTitle:NSLocalizedString(@"Session Ended", nil)
desciption:NSLocalizedString(@"Please login again", nil)];
[yoAlert addAction:[[YoAlertAction alloc] initWithTitle:NSLocalizedString(@"Ok", nil).uppercaseString tapBlock:nil]];
[[YoAlertManager sharedInstance] showAlert:yoAlert];
});
#endif
}
}
@end