Skip to content

Commit

Permalink
Merge pull request #35 from vapor-community/feature/hopped-el
Browse files Browse the repository at this point in the history
Add hopped event loop convenience.
  • Loading branch information
Andrewangeta authored Feb 21, 2020
2 parents 4d38da4 + 000b97c commit 7926ae9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion Sources/StripeKit/StripeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ public final class StripeClient {
// MARK: - WEBHOOKS
public var webhookEndpoints: WebhookEndpointRoutes

var handler: StripeDefaultAPIHandler

/// 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)
Expand Down Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions Sources/StripeKit/StripeRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7926ae9

Please sign in to comment.