Skip to content

Instantly share code, notes, and snippets.

@nyaray
Last active December 21, 2022 23:48
Show Gist options
  • Save nyaray/4c9882722b0a116ce7e66244492c5210 to your computer and use it in GitHub Desktop.
Save nyaray/4c9882722b0a116ce7e66244492c5210 to your computer and use it in GitHub Desktop.
Good ideas
// definitions
typealias RequestOptions = (HTTPMethod, Set<HeaderOption>, String, String, Encodable?)
protocol RequestOptionsConvertible {
func asRequestOptions() throws -> RequestOptions
}
// "rigging"
enum PaymentMappings: RequestOptionsConvertible {
case .listAvailablePaymentMethods(PaymentMethodsRequest)
case .disablePaymentMethod(DisablePaymentMethodRequest)
case .makePayment(PaymentRequest)
case .completePayment(PaymentFinalisationRequest)
case .getPaymentStatus(Int)
static let BASE_URL = ...
func asRequestOptions() throws -> RequestOptions {
switch self {
case .listAvailablePaymentMethods(let parameters):
return (.post, [.useApiKey, .useAuthToken], Self.BASE_URL, "/payment-methods", parameters)
case .disablePaymentMethod(let parameters):
return (.post, [.useApiKey, .useAuthToken], Self.BASE_URL, "/payment-methods/disable", parameters)
case .makePayment(let parameters):
return (.post, [.useApiKey, .useAuthToken], Self.BASE_URL, "/payments", parameters)
case .completePayment(let parameters):
return (.post, [.useApiKey, .useAuthToken], Self.BASE_URL, "/payments/details", parameters)
case .getStatus(let orderNumber):
return (.get, [.useApiKey], Self.BASE_URL, "/payments/\(orderNumber)/status", .none)
}
}
}
// usage
let (method, headerOptions, baseURL, path, parameters) = req.asRequestOptions()
// prep request
//...
AF.request(...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment