Skip to content

Commit

Permalink
Merge pull request #45 from Sorix/addApiVersion
Browse files Browse the repository at this point in the history
Possibility to set custom API version in EphemeralKeyRoutes
  • Loading branch information
Andrewangeta authored Jun 8, 2018
2 parents f4b81a7 + e71d88c commit a08d75e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 10 additions & 4 deletions Sources/Stripe/API/Routes/EphemeralKeyRoutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Vapor
public protocol EphemeralKeyRoutes {
associatedtype EK: EphemeralKey

func create(customer: String) throws -> Future<EK>
func create(customer: String, apiVersion: String?) throws -> Future<EK>
func delete(ephemeralKey: String) throws -> Future<EK>
}

Expand All @@ -22,9 +22,15 @@ public struct StripeEphemeralKeyRoutes: EphemeralKeyRoutes {
self.request = request
}

public func create(customer: String) throws -> Future<StripeEphemeralKey> {
let body = ["customer": customer]
return try request.send(method: .POST, path: StripeAPIEndpoint.ephemeralKeys.endpoint, body: body.queryParameters)
public func create(customer: String, apiVersion: String? = nil) throws -> Future<StripeEphemeralKey> {
var headers: HTTPHeaders = [:]

if let otherApiVersion = apiVersion {
headers.replaceOrAdd(name: .stripeVersion, value: otherApiVersion)
}

let body = ["customer": customer]
return try request.send(method: .POST, path: StripeAPIEndpoint.ephemeralKeys.endpoint, body: body.queryParameters, headers: headers)
}

public func delete(ephemeralKey: String) throws -> Future<StripeEphemeralKey> {
Expand Down
4 changes: 3 additions & 1 deletion Sources/Stripe/API/StripeRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public class StripeAPIRequest: StripeRequest {
public func send<SM: StripeModel>(method: HTTPMethod, path: String, query: String, body: String, headers: HTTPHeaders) throws -> Future<SM> {
var finalHeaders: HTTPHeaders = .stripeDefault

headers.forEach { finalHeaders.add(name: $0.name, value: $0.value) }
headers.forEach {
finalHeaders.replaceOrAdd(name: $0.name, value: $0.value)
}

// Get the appropiate API key based on the environment and if the test key is present
let apiKey = self.httpClient.container.environment == .development ? (self.testApiKey ?? self.apiKey) : self.apiKey
Expand Down

0 comments on commit a08d75e

Please sign in to comment.