Skip to content

Commit

Permalink
Merge pull request #4 from nerdishbynature/repositories_pagination
Browse files Browse the repository at this point in the history
Add pagination for repositories
  • Loading branch information
pietbrauer committed Nov 6, 2015
2 parents 06b6789 + 1f01799 commit cb81e04
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
14 changes: 7 additions & 7 deletions TrashCanKit/Repositories.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ import RequestKit
// MARK: request

public extension TrashCanKit {
public func repositories(userName: String, completion: (response: Response<[BitbucketRepository]>) -> Void) {
let router = RepositoryRouter.ReadRepositories(configuration, userName)
public func repositories(userName: String, page: String = "1", completion: (response: Response<[BitbucketRepository]>) -> Void) {
let router = RepositoryRouter.ReadRepositories(configuration, userName, page)
router.loadJSON([String: AnyObject].self) { json, error in
if let error = error {
completion(response: Response.Failure(error))
Expand All @@ -66,11 +66,11 @@ public extension TrashCanKit {
// MARK: Router

public enum RepositoryRouter: Router {
case ReadRepositories(Configuration, String)
case ReadRepositories(Configuration, String, String)

public var configuration: Configuration {
switch self {
case .ReadRepositories(let config, _): return config
case .ReadRepositories(let config, _, _): return config
}
}

Expand All @@ -84,14 +84,14 @@ public enum RepositoryRouter: Router {

public var params: [String: String] {
switch self {
case .ReadRepositories(_):
return [:]
case .ReadRepositories(_, _, let page):
return ["page": page]
}
}

public var path: String {
switch self {
case .ReadRepositories(_, let userName):
case .ReadRepositories(_, let userName, _):
return "/repositories/\(userName)"
}
}
Expand Down
23 changes: 21 additions & 2 deletions TrashCanKitTests/RepositoriesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class RepositoriesTests: XCTestCase {

func testGetRepositories() {
let tokenConfig = TokenConfiguration("123456", refreshToken: "7890")
stubRequest("GET", "https://bitbucket.org/api/2.0/repositories/bitbucketCat?access_token=123456").andReturn(200).withBody(TestHelper.loadJSONString("Repositories"))
stubRequest("GET", "https://bitbucket.org/api/2.0/repositories/bitbucketCat?access_token=123456&page=1").andReturn(200).withBody(TestHelper.loadJSONString("Repositories"))
let expectation = expectationWithDescription("get_repos")
TrashCanKit(tokenConfig).repositories("bitbucketCat") { response in
switch response {
Expand All @@ -54,9 +54,28 @@ class RepositoriesTests: XCTestCase {
}
}

func testGetSecondPageRepositories() {
let tokenConfig = TokenConfiguration("123456", refreshToken: "7890")
stubRequest("GET", "https://bitbucket.org/api/2.0/repositories/bitbucketCat?access_token=123456&page=2").andReturn(200).withBody(TestHelper.loadJSONString("Repositories"))
let expectation = expectationWithDescription("get_repos")
TrashCanKit(tokenConfig).repositories("bitbucketCat", page: "2") { response in
switch response {
case .Success(let repos):
XCTAssertEqual(repos.count, 1)
expectation.fulfill()
case .Failure:
XCTAssertFalse(true)
expectation.fulfill()
}
}
waitForExpectationsWithTimeout(1) { error in
XCTAssertNil(error)
}
}

func testFailToGetRepositories() {
let tokenConfig = TokenConfiguration("123456", refreshToken: "7890")
stubRequest("GET", "https://bitbucket.org/api/2.0/repositories/bitbucketCat?access_token=123456").andReturn(401).withBody(TestHelper.loadJSONString("refresh_token_error"))
stubRequest("GET", "https://bitbucket.org/api/2.0/repositories/bitbucketCat?access_token=123456&page=1").andReturn(401).withBody(TestHelper.loadJSONString("refresh_token_error"))
let expectation = expectationWithDescription("get_repos")
TrashCanKit(tokenConfig).repositories("bitbucketCat") { response in
switch response {
Expand Down

0 comments on commit cb81e04

Please sign in to comment.