Skip to content

Commit

Permalink
Add Result object
Browse files Browse the repository at this point in the history
  • Loading branch information
gomfucius committed Jan 26, 2017
1 parent 90d7909 commit aa45753
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 28 deletions.
82 changes: 63 additions & 19 deletions Sources/VaporStripe/VaporStripe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(),
Expand All @@ -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 []
}
Expand All @@ -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
}
}
}
12 changes: 3 additions & 9 deletions Tests/VaporStripeTests/VaporStripeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit aa45753

Please sign in to comment.