Skip to content

Commit

Permalink
Change sales response to data (#103)
Browse files Browse the repository at this point in the history
* downloadSalesAndTrendsReports T ==  Void -> Data

* Return Data if T == Data in APIProvider mapResponse

* downloadFinanceReports APIEndpoint T == Void -> Data

* Add test for request with data in API Provider

* Reformat mapResponse if let data
  • Loading branch information
DechengMa authored Jun 11, 2020
1 parent 00f3f33 commit 2abaffe
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Sources/APIProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ private extension APIProvider {
return .failure(Error.requestFailure(response.statusCode, response.data))
}

if let data = data as? T {
return .success(data)
}

do {
let decodedValue = try Self.jsonDecoder.decode(T.self, from: data)
return .success(decodedValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Created by Pascal Edmond on 30/11/2018.
//

extension APIEndpoint where T == Void {
import Foundation

extension APIEndpoint where T == Data {

/// Download finance reports filtered by your specified criteria.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Created by Pascal Edmond on 30/11/2018.
//

extension APIEndpoint where T == Void {
import Foundation

extension APIEndpoint where T == Data {

/// Download sales and trends reports filtered by your specified criteria.
///
Expand Down
14 changes: 14 additions & 0 deletions Tests/APIProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,18 @@ final class APIProviderTests: XCTestCase {
XCTAssertTrue(result.isFailure)
}
}

func testRequestWithDataResultSuccess() {
let response = Response(statusCode: 200, data: Data(base64Encoded: ""))
let mockRequestExecutor = MockRequestExecutor(expectedResponse: Result.success(response))

let apiProvider = APIProvider(configuration: configuration, requestExecutor: mockRequestExecutor)
let resourceLink = ResourceLinks<Data>(self: URL(string: "https://api.appstoreconnect.com?cursor=FIRST")!)
apiProvider.request(resourceLink) { result in
// using the mock request executor the block is called sync
XCTAssertTrue(result.isSuccess)
XCTAssertEqual(result.value!, Data(base64Encoded: ""))
XCTAssertNotEqual(result.value!, Data(base64Encoded: "foo"))
}
}
}

0 comments on commit 2abaffe

Please sign in to comment.