Skip to content

Commit a5884c6

Browse files
committed
Merge pull request octokit#50 from octokit/fix-304-and-logging
Improve logging, fix handling of 304 Not Modified
2 parents 936f194 + f32fb61 commit a5884c6

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

OctoKit/OCTClient.m

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
NSString * const OCTClientErrorRequestURLKey = @"OCTClientErrorRequestURLKey";
3131
NSString * const OCTClientErrorHTTPStatusCodeKey = @"OCTClientErrorHTTPStatusCodeKey";
3232

33-
static const NSUInteger OCTClientNotModifiedStatusCode = 304;
33+
static const NSInteger OCTClientNotModifiedStatusCode = 304;
3434

3535
@interface OCTClient ()
3636

@@ -131,7 +131,12 @@ - (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)p
131131
NSMutableURLRequest *request = [self requestWithMethod:method path:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:parameters];
132132
if (etag != nil) {
133133
[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;
134138
}
139+
135140
return request;
136141
}
137142

@@ -144,15 +149,15 @@ - (RACSignal *)enqueueRequest:(NSURLRequest *)request resultClass:(Class)resultC
144149
- (RACSignal *)enqueueRequest:(NSURLRequest *)request resultClass:(Class)resultClass fetchAllPages:(BOOL)fetchAllPages {
145150
RACSignal *signal = [RACSignal createSignal:^(id<RACSubscriber> subscriber) {
146151
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+
147156
if (operation.response.statusCode == OCTClientNotModifiedStatusCode) {
148157
// No change in the data.
149158
[subscriber sendCompleted];
150159
return;
151160
}
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-
}
156161

157162
RACSignal *thisPageSignal = [[self parsedResponseOfClass:resultClass fromJSON:responseObject]
158163
map:^(id parsedResult) {
@@ -161,6 +166,16 @@ - (RACSignal *)enqueueRequest:(NSURLRequest *)request resultClass:(Class)resultC
161166

162167
return response;
163168
}];
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+
}
164179

165180
RACSignal *nextPageSignal = [RACSignal empty];
166181
NSURL *nextPageURL = (fetchAllPages ? [self nextPageURLFromOperation:operation] : nil);
@@ -185,18 +200,6 @@ - (RACSignal *)enqueueRequest:(NSURLRequest *)request resultClass:(Class)resultC
185200
[operation cancel];
186201
}];
187202
}];
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-
}
200203

201204
return [[signal
202205
replayLazily]

0 commit comments

Comments
 (0)