Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub response caching #203

Merged
merged 4 commits into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions BuildaGitServer/GitHubServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class GitHubServer : GitServer {

public let endpoints: GitHubEndpoints
public var latestRateLimitInfo: GitHubRateLimit?

let cache = InMemoryURLCache()

public init(endpoints: GitHubEndpoints, http: HTTP? = nil) {

Expand All @@ -23,11 +25,9 @@ public class GitHubServer : GitServer {

//TODO: from each of these calls, return a "cancellable" object which can be used for cancelling

//FYI - GitHub API has a rate limit of 5,000 requests per hour. should be more than enough, but keep it in mind
//when calling the API frequently.
extension GitHubServer {

private func sendRequestWithPossiblePagination(request: NSURLRequest, accumulatedResponseBody: NSArray, completion: HTTP.Completion) {
private func sendRequestWithPossiblePagination(request: NSMutableURLRequest, accumulatedResponseBody: NSArray, completion: HTTP.Completion) {

self.sendRequest(request) {
(response, body, error) -> () in
Expand Down Expand Up @@ -115,15 +115,20 @@ extension GitHubServer {
return linkDict
}

private func sendRequest(request: NSURLRequest, completion: HTTP.Completion) {
private func sendRequest(request: NSMutableURLRequest, completion: HTTP.Completion) {

let cachedInfo = self.cache.getCachedInfoForRequest(request)
if let etag = cachedInfo.etag {
request.setValue(etag, forHTTPHeaderField: "If-None-Match")
}

self.http.sendRequest(request, completion: { (response, body, error) -> () in

if let error = error {
completion(response: response, body: body, error: error)
return
}

if response == nil {
completion(response: nil, body: body, error: Error.withInfo("Nil response"))
return
Expand Down Expand Up @@ -158,7 +163,13 @@ extension GitHubServer {
//error out on special HTTP status codes
let statusCode = response!.statusCode
switch statusCode {

case 200...299: //good response, cache the returned data
let responseInfo = ResponseInfo(response: response!, body: body)
cachedInfo.update(responseInfo)
case 304: //not modified, return the cached response
let responseInfo = cachedInfo.responseInfo!
completion(response: responseInfo.response, body: responseInfo.body, error: nil)
return
case 400 ... 500:
let message = (body as? NSDictionary)?["message"] as? String ?? "Unknown error"
let resultString = "\(statusCode): \(message)"
Expand Down
4 changes: 3 additions & 1 deletion BuildaGitServerTests/GitHubServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ class GitHubSourceTests: XCTestCase {
XCTAssertEqual(prbranch.repo.name, "AsyncDisplayKit")
}


func testResponseCaching() {
//TODO
}


}
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use_frameworks!
inhibit_all_warnings!

def pods_for_errbody
pod 'BuildaUtils', '~> 0.2.6'
pod 'BuildaUtils', '~> 0.2.7'
end

def rac
Expand Down
9 changes: 6 additions & 3 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
PODS:
- Alamofire (2.0.0-beta.3)
- BuildaUtils (0.2.6)
- BuildaUtils (0.2.7):
- SwiftSafe (~> 0.1)
- CryptoSwift (0.2.2)
- ekgclient (0.3.0):
- Alamofire (= 2.0.0-beta.3)
Expand All @@ -22,11 +23,12 @@ PODS:
- ReactiveCocoa/Core
- Result (~> 1.0)
- Result (1.0.1)
- SwiftSafe (0.1)
- XcodeServerSDK (0.5.4):
- BuildaUtils (~> 0.2.3)

DEPENDENCIES:
- BuildaUtils (~> 0.2.6)
- BuildaUtils (~> 0.2.7)
- CryptoSwift
- ekgclient (~> 0.3.0)
- Ji (~> 1.2.0)
Expand All @@ -36,13 +38,14 @@ DEPENDENCIES:

SPEC CHECKSUMS:
Alamofire: 39dddb7d3725d1771b1d2f7099c8bd45bd83ffbb
BuildaUtils: 9fbc3dfa77e778cb937292572eccef47dc97e102
BuildaUtils: dacc580246b52c7b5029c993ff4732e025c633a8
CryptoSwift: d382228d6301c09474132417878a741c2a2e68cd
ekgclient: 40f5d347e2ede450b3e50ac7c6bd84d96e7b84ad
Ji: ddebb22f9ac445db6e884b66f78ea74fb135fdb7
Nimble: 4c353d43735b38b545cbb4cb91504588eb5de926
ReactiveCocoa: 2b0f654beb7642b82cfd3fdb845205ae9889422c
Result: caef80340451e1f07492fa1e89117f83613bce24
SwiftSafe: 77ffd12b02678790bec1ef56a2d14ec5036f1fd6
XcodeServerSDK: 38c2bbdbc8e1387480adde299c0dc4455f0af11b

COCOAPODS: 0.39.0