30
30
NSString * const OCTClientErrorRequestURLKey = @" OCTClientErrorRequestURLKey" ;
31
31
NSString * const OCTClientErrorHTTPStatusCodeKey = @" OCTClientErrorHTTPStatusCodeKey" ;
32
32
33
- static const NSUInteger OCTClientNotModifiedStatusCode = 304 ;
33
+ static const NSInteger OCTClientNotModifiedStatusCode = 304 ;
34
34
35
35
@interface OCTClient ()
36
36
@@ -131,7 +131,12 @@ - (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)p
131
131
NSMutableURLRequest *request = [self requestWithMethod: method path: [path stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding] parameters: parameters];
132
132
if (etag != nil ) {
133
133
[request setValue: etag forHTTPHeaderField: @" If-None-Match" ];
134
+
135
+ // If an etag is specified, we want 304 responses to be treated as 304s,
136
+ // not served from NSURLCache with a status of 200.
137
+ request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
134
138
}
139
+
135
140
return request;
136
141
}
137
142
@@ -144,15 +149,15 @@ - (RACSignal *)enqueueRequest:(NSURLRequest *)request resultClass:(Class)resultC
144
149
- (RACSignal *)enqueueRequest : (NSURLRequest *)request resultClass : (Class )resultClass fetchAllPages : (BOOL )fetchAllPages {
145
150
RACSignal *signal = [RACSignal createSignal: ^(id <RACSubscriber> subscriber) {
146
151
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest: request success: ^(AFHTTPRequestOperation *operation, id responseObject) {
152
+ if (getenv (" LOG_API_RESPONSES" ) != NULL ) {
153
+ NSLog (@" %@ %@ %@ => %li %@ :\n %@ " , request.HTTPMethod , request.URL , request.allHTTPHeaderFields , (long )operation.response .statusCode , operation.response .allHeaderFields , responseObject);
154
+ }
155
+
147
156
if (operation.response .statusCode == OCTClientNotModifiedStatusCode) {
148
157
// No change in the data.
149
158
[subscriber sendCompleted ];
150
159
return ;
151
160
}
152
-
153
- if (getenv (" LOG_API_RESPONSES" ) != NULL ) {
154
- NSLog (@" %@ %@ => %li %@ :\n %@ " , request.HTTPMethod , request.URL , (long )operation.response .statusCode , operation.response .allHeaderFields , responseObject);
155
- }
156
161
157
162
RACSignal *thisPageSignal = [[self parsedResponseOfClass: resultClass fromJSON: responseObject]
158
163
map: ^(id parsedResult) {
@@ -161,6 +166,16 @@ - (RACSignal *)enqueueRequest:(NSURLRequest *)request resultClass:(Class)resultC
161
166
162
167
return response;
163
168
}];
169
+
170
+ if (getenv (" LOG_REMAINING_API_CALLS" ) != NULL ) {
171
+ __block BOOL loggedRemaining = NO ;
172
+ thisPageSignal = [thisPageSignal doNext: ^(OCTResponse *response) {
173
+ if (loggedRemaining) return ;
174
+
175
+ NSLog (@" Remaining API calls: %li /%li " , (long )response.remainingRequests , (long )response.maximumRequestsPerHour );
176
+ loggedRemaining = YES ;
177
+ }];
178
+ }
164
179
165
180
RACSignal *nextPageSignal = [RACSignal empty ];
166
181
NSURL *nextPageURL = (fetchAllPages ? [self nextPageURLFromOperation: operation] : nil );
@@ -185,18 +200,6 @@ - (RACSignal *)enqueueRequest:(NSURLRequest *)request resultClass:(Class)resultC
185
200
[operation cancel ];
186
201
}];
187
202
}];
188
-
189
- if (getenv (" LOG_REMAINING_API_CALLS" ) != NULL ) {
190
- // Avoid infinite recursion.
191
- if (![request.URL.path isEqualToString: @" rate_limit" ]) {
192
- signal = [signal doCompleted: ^{
193
- NSURLRequest *request = [self requestWithMethod: @" GET" path: @" rate_limit" parameters: nil notMatchingEtag: nil ];
194
- [[self enqueueRequest: request resultClass: nil ] subscribeNext: ^(NSDictionary *dict) {
195
- NSLog (@" Remaining API calls: %@ " , dict[@" rate" ][@" remaining" ]);
196
- }];
197
- }];
198
- }
199
- }
200
203
201
204
return [[signal
202
205
replayLazily ]
0 commit comments