From aa4575310412117fe9419537f63a7942ebf498bb Mon Sep 17 00:00:00 2001 From: Genki Mine Date: Wed, 25 Jan 2017 22:07:36 -0800 Subject: [PATCH] Add Result object --- Sources/VaporStripe/VaporStripe.swift | 82 ++++++++++++++----- Tests/VaporStripeTests/VaporStripeTests.swift | 12 +-- 2 files changed, 66 insertions(+), 28 deletions(-) diff --git a/Sources/VaporStripe/VaporStripe.swift b/Sources/VaporStripe/VaporStripe.swift index 02613d1..21d3251 100644 --- a/Sources/VaporStripe/VaporStripe.swift +++ b/Sources/VaporStripe/VaporStripe.swift @@ -2,8 +2,7 @@ import Foundation import JSON import HTTP -public enum Currency: String -{ +public enum Currency: String { case aud = "aud" case cad = "cad" case dkk = "dkk" @@ -16,28 +15,19 @@ public enum Currency: String case sgd = "sgd" } -public final class VaporStripe -{ +public final class VaporStripe { let apiHostUrl = "https://api.stripe.com" let apiVersion = "v1" let chargePath = "charges" let apiKey: String - let amount: Int - let currency: Currency - let description: String let token: String - public init(apiKey: String, token: String, amount: Int, currency: Currency, description: String) - { + public init(apiKey: String, token: String) { self.apiKey = apiKey - self.amount = amount - self.currency = currency - self.description = description self.token = token } - public func charge() throws -> Response - { + public func charge(amount: Int, currency: Currency, description: String) throws -> Result { let dictionary = [ "amount": try amount.makeNode(), "currency": currency.rawValue.makeNode(), @@ -46,14 +36,21 @@ public final class VaporStripe ] let node = Node(dictionary) - return try BasicClient.post("\(apiHostUrl)/\(apiVersion)/\(chargePath)", headers: ["Authorization": "Bearer \(apiKey)"], body: Body.data(node.vsFormURLEncoded())) + let response = try BasicClient.post("\(apiHostUrl)/\(apiVersion)/\(chargePath)", headers: ["Authorization": "Bearer \(apiKey)"], body: Body.data(node.vsFormURLEncoded())) + + var json: JSON? + if let bytes = response.body.bytes { + json = try JSON(bytes: bytes) + } + + return Result(status: ResponseStatus(statusCode: response.status.statusCode), headers: response.headers, json: json) } } -extension Node -{ - fileprivate func vsFormURLEncoded() throws -> Bytes - { +// Taken from StructuredData+FormURLEncoded.swift. +// TODO: Figure out how to use it without having to `import Vapor` +extension Node { + fileprivate func vsFormURLEncoded() throws -> Bytes { guard case .object(let dict) = self else { return [] } @@ -71,3 +68,50 @@ extension Node return bytes.joined(separator: [Byte.ampersand]).array } } + +public struct Result { + let status: ResponseStatus + let headers: [HTTP.HeaderKey: String] + let json: JSON? + + public init(status: ResponseStatus, headers: [HTTP.HeaderKey: String], json: JSON?) { + self.status = status + self.headers = headers + self.json = json + } +} + +/// https://stripe.com/docs/api/curl#errors +public enum ResponseStatus: Int { + case success + case badRequest + case unauthorized + case requestFailed + case notFound + case conflict + case tooManyRequests + case serverError + + public init(statusCode: Int) { + switch statusCode { + case 200: + self = .success + case 400: + self = .badRequest + case 401: + self = .unauthorized + case 402: + self = .requestFailed + case 404: + self = .notFound + case 409: + self = .conflict + case 429: + self = .tooManyRequests + case 500, 502, 503, 504: + self = .serverError + default: + self = .badRequest + } + } +} diff --git a/Tests/VaporStripeTests/VaporStripeTests.swift b/Tests/VaporStripeTests/VaporStripeTests.swift index c7c5cc4..a4981f3 100755 --- a/Tests/VaporStripeTests/VaporStripeTests.swift +++ b/Tests/VaporStripeTests/VaporStripeTests.swift @@ -7,21 +7,15 @@ import XCTest import JSON import HTTP -class VaporStripeTests: XCTestCase -{ +class VaporStripeTests: XCTestCase { static let allTests = [ ("testInitializer", testInitializer), ] - func testInitializer() - { - let stripe = VaporStripe(apiKey: "apikey", token: "sometoken", amount: 99, currency: .usd, description: "Some description") + func testInitializer() { + let stripe = VaporStripe(apiKey: "apikey", token: "sometoken") XCTAssertEqual(stripe.apiKey, "apikey") XCTAssertEqual(stripe.token, "sometoken") - XCTAssertEqual(stripe.amount, 99) - XCTAssertEqual(stripe.currency, .usd) - XCTAssertEqual(stripe.currency.rawValue, "usd") - XCTAssertEqual(stripe.description, "Some description") } }