Skip to content

Commit

Permalink
Update endpoint paths (#60)
Browse files Browse the repository at this point in the history
* Update endpoint path

* Remove idempotency token from header (put it in params) (#58)

* Update errors (#57)
  • Loading branch information
mederic-p committed Jun 18, 2018
1 parent f0cb1c3 commit bce01d8
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 83 deletions.
10 changes: 5 additions & 5 deletions OmiseGOTests/APITests/APIErrorTests.swift
Expand Up @@ -18,7 +18,7 @@ class APIErrorTests: XCTestCase {

func testIsAuthorizationError() {
XCTAssertTrue(APIError.init(code: .accessTokenExpired, description: "").isAuthorizationError())
XCTAssertTrue(APIError.init(code: .accessTokenNotFound, description: "").isAuthorizationError())
XCTAssertTrue(APIError.init(code: .authenticationTokenNotFound, description: "").isAuthorizationError())
XCTAssertTrue(APIError.init(code: .invalidAPIKey, description: "").isAuthorizationError())
XCTAssertFalse(APIError.init(code: .unknownServerError, description: "").isAuthorizationError())
}
Expand All @@ -43,16 +43,14 @@ class APIErrorCodeTests: XCTestCase {
APIErrorCode.endPointNotFound)
XCTAssertEqual(APIErrorCode(rawValue: "client:invalid_api_key"),
APIErrorCode.invalidAPIKey)
XCTAssertEqual(APIErrorCode(rawValue: "client:no_idempotency_token_provided"),
APIErrorCode.missingIdempotencyToken)

XCTAssertEqual(APIErrorCode(rawValue: "server:internal_server_error"),
APIErrorCode.internalServerError)
XCTAssertEqual(APIErrorCode(rawValue: "server:unknown_error"),
APIErrorCode.unknownServerError)

XCTAssertEqual(APIErrorCode(rawValue: "user:access_token_not_found"),
APIErrorCode.accessTokenNotFound)
XCTAssertEqual(APIErrorCode(rawValue: "user:auth_token_not_found"),
APIErrorCode.authenticationTokenNotFound)
XCTAssertEqual(APIErrorCode(rawValue: "user:access_token_expired"),
APIErrorCode.accessTokenExpired)
XCTAssertEqual(APIErrorCode(rawValue: "user:from_address_not_found"),
Expand Down Expand Up @@ -87,6 +85,8 @@ class APIErrorCodeTests: XCTestCase {
APIErrorCode.channelNotFound)
XCTAssertEqual(APIErrorCode(rawValue: "websocket:connect_error"),
APIErrorCode.websocketError)
XCTAssertEqual(APIErrorCode(rawValue: "db:inserted_transaction_could_not_be_loaded"),
APIErrorCode.transactionCouldNotBeLoaded)
XCTAssertEqual(APIErrorCode(rawValue: "an other code"),
APIErrorCode.other("an other code"))
}
Expand Down
18 changes: 0 additions & 18 deletions OmiseGOTests/APITests/RequestBuilderTests.swift
Expand Up @@ -42,24 +42,6 @@ class RequestBuilderTests: XCTestCase {
}
}

func testBuildRequestWithAdditionalHeaderFromParams() {
let transactionConsumptionParams = StubGenerator.transactionConsumptionParams()
let endpoint = APIEndpoint.transactionRequestConsume(params: transactionConsumptionParams)
do {
let urlRequest = try self.requestBuilder.buildHTTPURLRequest(withEndpoint: endpoint)
XCTAssertEqual(urlRequest.allHTTPHeaderFields!["Idempotency-Token"],
transactionConsumptionParams.idempotencyToken)
XCTAssertEqual(urlRequest.httpMethod, "POST")
XCTAssertEqual(urlRequest.cachePolicy, .useProtocolCachePolicy)
XCTAssertEqual(urlRequest.timeoutInterval, 6.0)
XCTAssertNotNil(urlRequest.allHTTPHeaderFields!["Authorization"])
XCTAssertNotNil(urlRequest.allHTTPHeaderFields!["Accept"])
XCTAssertNotNil(urlRequest.allHTTPHeaderFields!["Content-Type"])
} catch let error {
XCTFail(error.localizedDescription)
}
}

func testBuildRequestWithoutParams() {
let endpoint = APIEndpoint.custom(path: "/test", task: HTTPTask.requestPlain)
do {
Expand Down
1 change: 1 addition & 0 deletions OmiseGOTests/CodingTests/EncodeTests.swift
Expand Up @@ -285,6 +285,7 @@ class EncodeTests: XCTestCase {
"correlation_id":"321",
"formatted_transaction_request_id":"|0a8a4a98-794b-419e-b92d-514e83657e75",
"address":"456",
"idempotency_token":"123",
"encrypted_metadata":{},
"metadata":{},
"token_id":"BTC:123"
Expand Down
6 changes: 3 additions & 3 deletions OmiseGOTests/HTTPTests/APIEndpointTest.swift
Expand Up @@ -18,17 +18,17 @@ class APIEndpointTest: XCTestCase {

func testPath() {
XCTAssertEqual(APIEndpoint.getCurrentUser.path, "/me.get")
XCTAssertEqual(APIEndpoint.getWallets.path, "/me.list_wallets")
XCTAssertEqual(APIEndpoint.getWallets.path, "/me.get_wallets")
XCTAssertEqual(APIEndpoint.getSettings.path, "/me.get_settings")
XCTAssertEqual(APIEndpoint.getTransactions(params: self.validTransactionListParams).path,
"/me.list_transactions")
"/me.get_transactions")
XCTAssertEqual(APIEndpoint.transactionRequestCreate(params: self.validTransactionCreateParams).path,
"/me.create_transaction_request")
XCTAssertEqual(APIEndpoint.transactionRequestGet(params: self.validTransactionGetParams).path,
"/me.get_transaction_request")
XCTAssertEqual(APIEndpoint.transactionRequestConsume(params: self.validTransactionConsumptionParams).path,
"/me.consume_transaction_request")
XCTAssertEqual(APIEndpoint.logout.path, "/logout")
XCTAssertEqual(APIEndpoint.logout.path, "/me.logout")
}

func testTask() {
Expand Down
2 changes: 1 addition & 1 deletion OmiseGOTests/LiveTests/AuthenticationLiveTest.swift
Expand Up @@ -25,7 +25,7 @@ class AuthenticationLiveTest: LiveTestCase {
case .fail(error: let error):
switch error {
case .api(apiError: let apiError) where apiError.isAuthorizationError():
XCTAssertEqual(apiError.code, .accessTokenNotFound)
XCTAssertEqual(apiError.code, .authenticationTokenNotFound)
default:
XCTFail("Error should be an authorization error")
}
Expand Down
13 changes: 0 additions & 13 deletions OmiseGOTests/ModelTests/TransactionConsumptionParamsTest.swift
Expand Up @@ -99,17 +99,4 @@ class TransactionConsumptionParamsTest: XCTestCase {
metadata: [:])!
XCTAssertEqual(params.amount, 3000)
}

func testIdempotencyTokenExists() {
let transactionRequest = StubGenerator.transactionRequest(amount: 1337)
let params = TransactionConsumptionParams(transactionRequest: transactionRequest,
address: nil,
tokenId: nil,
amount: 3000,
idempotencyToken: "123",
correlationId: nil,
metadata: [:])!
XCTAssertNotNil(params.getIdempotencyToken())
XCTAssertEqual(params.getIdempotencyToken()!, "123")
}
}
6 changes: 3 additions & 3 deletions Source/API/APIEndpoint.swift
Expand Up @@ -27,11 +27,11 @@ enum APIEndpoint {
case .getCurrentUser:
return "/me.get"
case .getWallets:
return "/me.list_wallets"
return "/me.get_wallets"
case .getSettings:
return "/me.get_settings"
case .getTransactions:
return "/me.list_transactions"
return "/me.get_transactions"
case .createTransaction:
return "/me.transfer"
case .transactionRequestCreate:
Expand All @@ -45,7 +45,7 @@ enum APIEndpoint {
case .transactionConsumptionReject:
return "/me.reject_transaction_consumption"
case .logout:
return "/logout"
return "/me.logout"
case .custom(let path, _):
return path
}
Expand Down
23 changes: 12 additions & 11 deletions Source/API/APIError.swift
Expand Up @@ -21,7 +21,7 @@ public struct APIError {
/// Indicate if the error is an authorization error in which case you may want to refresh the authentication token
public func isAuthorizationError() -> Bool {
switch self.code {
case .accessTokenExpired, .accessTokenNotFound, .invalidAPIKey: return true
case .accessTokenExpired, .authenticationTokenNotFound, .invalidAPIKey: return true
default: return false
}
}
Expand Down Expand Up @@ -70,12 +70,11 @@ public enum APIErrorCode: Decodable {
case permissionError
case endPointNotFound
case invalidAPIKey
case missingIdempotencyToken
// Server
case internalServerError
case unknownServerError
// User
case accessTokenNotFound
case authenticationTokenNotFound
case accessTokenExpired
case fromAddressNotFound
case fromAddressMismatch
Expand All @@ -95,6 +94,8 @@ public enum APIErrorCode: Decodable {
case forbiddenChannel
case channelNotFound
case websocketError
// DB
case transactionCouldNotBeLoaded
case other(String)

public init(from decoder: Decoder) throws {
Expand All @@ -121,14 +122,12 @@ extension APIErrorCode: RawRepresentable {
self = .endPointNotFound
case "client:invalid_api_key":
self = .invalidAPIKey
case "client:no_idempotency_token_provided":
self = .missingIdempotencyToken
case "server:internal_server_error":
self = .internalServerError
case "server:unknown_error":
self = .unknownServerError
case "user:access_token_not_found":
self = .accessTokenNotFound
case "user:auth_token_not_found":
self = .authenticationTokenNotFound
case "user:access_token_expired":
self = .accessTokenExpired
case "user:from_address_not_found":
Expand Down Expand Up @@ -159,6 +158,8 @@ extension APIErrorCode: RawRepresentable {
self = .channelNotFound
case "websocket:connect_error":
self = .websocketError
case "db:inserted_transaction_could_not_be_loaded":
self = .transactionCouldNotBeLoaded
case let code:
self = .other(code)
}
Expand All @@ -176,14 +177,12 @@ extension APIErrorCode: RawRepresentable {
return "client:endpoint_not_found"
case .invalidAPIKey:
return "client:invalid_api_key"
case .missingIdempotencyToken:
return "client:no_idempotency_token_provided"
case .internalServerError:
return "server:internal_server_error"
case .unknownServerError:
return "server:unknown_error"
case .accessTokenNotFound:
return "user:access_token_not_found"
case .authenticationTokenNotFound:
return "user:auth_token_not_found"
case .accessTokenExpired:
return "user:access_token_expired"
case .fromAddressNotFound:
Expand Down Expand Up @@ -214,6 +213,8 @@ extension APIErrorCode: RawRepresentable {
return "websocket:channel_not_found"
case .websocketError:
return "websocket:connect_error"
case .transactionCouldNotBeLoaded:
return "db:inserted_transaction_could_not_be_loaded"
case .other(let code):
return code
}
Expand Down
19 changes: 1 addition & 18 deletions Source/API/HTTPTask.swift
Expand Up @@ -7,19 +7,9 @@
//

/// Protocol for api parameters
public protocol APIParameters: Encodable {

/// Returns idempotency token if parameters have one
///
/// - Returns: idempotency token
func getIdempotencyToken() -> String?
}
public protocol APIParameters: Encodable {}

extension APIParameters {
public func getIdempotencyToken() -> String? {
return nil
}

public func encodedPayload() throws -> Data {
return try serialize(self)
}
Expand All @@ -42,11 +32,4 @@ enum HTTPTask {
return nil
}
}

/// An idempotency token required for task's request
/// nil if tasks do not require a token
public var idempotencyToken: String? {
return self.parameters?.getIdempotencyToken()
}

}
5 changes: 0 additions & 5 deletions Source/API/RequestBuilder.swift
Expand Up @@ -28,11 +28,6 @@ final class RequestBuilder {

try addRequiredHeaders(toRequest: &request)

// Add idempotencyToken if endpoint's task requires
if let token = endpoint.task.idempotencyToken {
request.addValue(token, forHTTPHeaderField: "Idempotency-Token")
}

// Add endpoint's task parameters if necessary
if let parameters = endpoint.task.parameters {
let payload = try parameters.encodedPayload()
Expand Down
8 changes: 2 additions & 6 deletions Source/Models/TransactionConsumptionParams.swift
Expand Up @@ -63,12 +63,6 @@ public struct TransactionConsumptionParams {
}

extension TransactionConsumptionParams: APIParameters {
public func getIdempotencyToken() -> String? {
return self.idempotencyToken
}
}

extension TransactionConsumptionParams {

private enum CodingKeys: String, CodingKey {
case formattedTransactionRequestId = "formatted_transaction_request_id"
Expand All @@ -78,6 +72,7 @@ extension TransactionConsumptionParams {
case metadata
case encryptedMetadata = "encrypted_metadata"
case correlationId = "correlation_id"
case idempotencyToken = "idempotency_token"
}

public func encode(to encoder: Encoder) throws {
Expand All @@ -89,6 +84,7 @@ extension TransactionConsumptionParams {
try container.encode(metadata, forKey: .metadata)
try container.encode(encryptedMetadata, forKey: .encryptedMetadata)
try container.encode(correlationId, forKey: .correlationId)
try container.encode(idempotencyToken, forKey: .idempotencyToken)
}

}
Expand Down

0 comments on commit bce01d8

Please sign in to comment.