From f958aca52af6348414841d30df7e4f7113ed9d60 Mon Sep 17 00:00:00 2001 From: Andrew Edwards Date: Thu, 20 Feb 2020 19:29:45 -0500 Subject: [PATCH 1/3] Added convenient event loop hopping to avoid recreating a new StripeClient with a different EventLoop. --- Sources/StripeKit/StripeClient.swift | 11 ++++++++++- Sources/StripeKit/StripeRequest.swift | 4 ++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Sources/StripeKit/StripeClient.swift b/Sources/StripeKit/StripeClient.swift index 0c402e49..93f18515 100644 --- a/Sources/StripeKit/StripeClient.swift +++ b/Sources/StripeKit/StripeClient.swift @@ -97,12 +97,14 @@ public final class StripeClient { // MARK: - WEBHOOKS public var webhookEndpoints: WebhookEndpointRoutes + var handler: StripeAPIHandler + /// Returns a StripeClient used to interact with the Stripe APIs. /// - Parameter httpClient: An `HTTPClient`used to communicate wiith the Stripe API /// - Parameter eventLoop: An `EventLoop` used to return an `EventLoopFuture` on. /// - Parameter apiKey: A Stripe API key. public init(httpClient: HTTPClient, eventLoop: EventLoop, apiKey: String) { - let handler = StripeDefaultAPIHandler(httpClient: httpClient, eventLoop: eventLoop, apiKey: apiKey) + handler = StripeDefaultAPIHandler(httpClient: httpClient, eventLoop: eventLoop, apiKey: apiKey) balances = StripeBalanceRoutes(apiHandler: handler) balanceTransactions = StripeBalanceTransactionRoutes(apiHandler: handler) @@ -180,4 +182,11 @@ public final class StripeClient { webhookEndpoints = StripeWebhookEndpointRoutes(apiHandler: handler) } + + /// Hop to a new eventloop to execute requests on. + /// - Parameter eventLoop: The eventloop to execute requests on. + public func hopped(to eventLoop: EventLoop) -> StripeClient { + handler.eventLoop = eventLoop + return self + } } diff --git a/Sources/StripeKit/StripeRequest.swift b/Sources/StripeKit/StripeRequest.swift index 70128300..a23507f0 100644 --- a/Sources/StripeKit/StripeRequest.swift +++ b/Sources/StripeKit/StripeRequest.swift @@ -38,11 +38,11 @@ extension StripeAPIHandler { } } -public struct StripeDefaultAPIHandler: StripeAPIHandler { +struct StripeDefaultAPIHandler: StripeAPIHandler { private let httpClient: HTTPClient private let apiKey: String private let decoder = JSONDecoder() - private let eventLoop: EventLoop + var eventLoop: EventLoop init(httpClient: HTTPClient, eventLoop: EventLoop, apiKey: String) { self.httpClient = httpClient From 9ce3d51614120c943c9bea4cd6e59ddd1b2125e5 Mon Sep 17 00:00:00 2001 From: Andrew Edwards Date: Thu, 20 Feb 2020 19:32:51 -0500 Subject: [PATCH 2/3] Set correct type for api handler. --- Sources/StripeKit/StripeClient.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/StripeKit/StripeClient.swift b/Sources/StripeKit/StripeClient.swift index 93f18515..47ab56b7 100644 --- a/Sources/StripeKit/StripeClient.swift +++ b/Sources/StripeKit/StripeClient.swift @@ -97,7 +97,7 @@ public final class StripeClient { // MARK: - WEBHOOKS public var webhookEndpoints: WebhookEndpointRoutes - var handler: StripeAPIHandler + var handler: StripeDefaultAPIHandler /// Returns a StripeClient used to interact with the Stripe APIs. /// - Parameter httpClient: An `HTTPClient`used to communicate wiith the Stripe API From 000b97cf2a5c2862b6121741886bc07c0007445d Mon Sep 17 00:00:00 2001 From: Andrew Edwards Date: Fri, 21 Feb 2020 07:31:35 -0500 Subject: [PATCH 3/3] Updated README. --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1948294f..68e1533c 100644 --- a/README.md +++ b/README.md @@ -112,8 +112,18 @@ None of the API calls throw errors. Instead each route returns a successful `Eve To use StripeKit with Vapor 4.x, add a simple extension on `Request`. ~~~swift extension Request { + private struct StripeKey: StorageKey { + typealias Value = StripeClient + } + public var stripe: StripeClient { - return StripeClient(httpClient: self.application.client.http, eventLoop: self.eventLoop, apiKey: "STRIPE_API_KEY") + if let existing = application.storage[StripeKey.self] { + return existing.hopped(to: self.eventLoop) + } else { + let new = StripeClient(httpClient: self.application.client.http, eventLoop: self.eventLoop, apiKey: "STRIPE_API_KEY") + self.application.storage[StripeKey.self] = new + return new + } } }