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

BitBucket pagination #224

Merged
merged 1 commit into from
Jan 28, 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
5 changes: 2 additions & 3 deletions BuildaGitServer/BitBucket/BitBucketEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ class BitBucketEntity : BitBucketType {
}

//parse an array of dictionaries into an array of parsed entities
func BitBucketArray<T where T: BitBucketType>(jsonDict: NSDictionary) -> [T] {
func BitBucketArray<T where T: BitBucketType>(jsonArray: [NSDictionary]) -> [T] {

let array = jsonDict["values"] as! [NSDictionary]!
let parsed = array.map {
let parsed = jsonArray.map {
(json: NSDictionary) -> (T) in
return T(json: json)
}
Expand Down
45 changes: 42 additions & 3 deletions BuildaGitServer/BitBucket/BitBucketServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extension BitBucketServer: SourceServerType {
return
}

if let body = body as? NSDictionary {
if let body = body as? [NSDictionary] {
let prs: [BitBucketPullRequest] = BitBucketArray(body)
completion(prs: prs.map { $0 as PullRequestType }, error: nil)
} else {
Expand Down Expand Up @@ -206,7 +206,7 @@ extension BitBucketServer: SourceServerType {
return
}

if let body = body as? NSDictionary {
if let body = body as? [NSDictionary] {
let comments: [BitBucketComment] = BitBucketArray(body)
completion(comments: comments.map { $0 as CommentType }, error: nil)
} else {
Expand Down Expand Up @@ -321,9 +321,48 @@ extension BitBucketServer {

do {
let request = try self.endpoints.createRequest(method, endpoint: endpoint, params: allParams, query: query, body: body)
self._sendRequest(request, completion: completion)
self._sendRequestWithPossiblePagination(request, accumulatedResponseBody: NSArray(), completion: completion)
} catch {
completion(response: nil, body: nil, error: Error.withInfo("Couldn't create Request, error \(error)"))
}
}

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

self._sendRequest(request) {
(response, body, error) -> () in

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

guard let dictBody = body as? NSDictionary else {
completion(response: response, body: body, error: error)
return
}

//pull out the values
guard let arrayBody = dictBody["values"] as? [AnyObject] else {
completion(response: response, body: dictBody, error: error)
return
}

//we do have more, let's fetch it
let newBody = accumulatedResponseBody.arrayByAddingObjectsFromArray(arrayBody)

guard let nextLink = dictBody.optionalStringForKey("next") else {

//is array, but we don't have any more data
completion(response: response, body: newBody, error: error)
return
}

let newRequest = request.mutableCopy() as! NSMutableURLRequest
newRequest.URL = NSURL(string: nextLink)!
self._sendRequestWithPossiblePagination(newRequest, accumulatedResponseBody: newBody, completion: completion)
return
}
}

}