Skip to content

Commit

Permalink
Remove Environment (#1)
Browse files Browse the repository at this point in the history
* Removed Environment enum and its usage from RazorpayAPIHandler

* Remove baseUrl parameter from RazorpayClient routes

* Remove baseUrl parameter from RazorpayKitTests

* Update async-http-client dependency to version 1.21.2
  • Loading branch information
vamsii777 authored Jul 31, 2024
1 parent 0ed08b1 commit 7a4671c
Show file tree
Hide file tree
Showing 25 changed files with 52 additions and 112 deletions.
6 changes: 2 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ let package = Package(
],
products: [.library(name: "RazorpayKit", targets: ["RazorpayKit"])],
dependencies: [
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.20.1"),
.package(url: "https://github.com/apple/swift-crypto.git", from: "3.1.0")
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.21.2")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(name: "RazorpayKit", dependencies: [
.product(name: "AsyncHTTPClient", package: "async-http-client"),
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "AsyncHTTPClient", package: "async-http-client")
]),
.testTarget(
name: "RazorpayKitTests",
Expand Down
48 changes: 24 additions & 24 deletions Sources/RazorpayKit/RazorpayClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@ public final class RazorpayClient {
public var transfer: RazorpayTransferRoutes
public var virtualAccount: RazorpayVirtualAccountRoutes
public var webhook: RazorpayWebhookRoutes

var handler: RazorpayAPIHandler

public init(httpClient: HTTPClient, key: String, secret: String, environment: Environment) {
self.handler = RazorpayAPIHandler(httpClient: httpClient, key: key, secret: secret, environment: environment)
account = RazorpayAccountRoutes(client: handler, baseUrl: environment.baseUrl)
addon = RazorpayAddonRoutes(client: handler, baseUrl: environment.baseUrl)
card = RazorpayCardRoutes(client: handler, baseUrl: environment.baseUrl)
customer = RazorpayCustomerRoutes(client: handler, baseUrl: environment.baseUrl)
fundAccount = RazorpayFundAccountRoutes(client: handler, baseUrl: environment.baseUrl)
iin = RazorpayIINRoutes(client: handler, baseUrl: environment.baseUrl)
invoice = RazorpayInvoiceRoutes(client: handler, baseUrl: environment.baseUrl)
item = RazorpayItemRoutes(client: handler, baseUrl: environment.baseUrl)
order = RazorpayOrderRoutes(client: handler, baseUrl: environment.baseUrl)
payment = RazorpayPaymentRoutes(client: handler, baseUrl: environment.baseUrl)
paymentLink = RazorpayPaymentLinkRoutes(client: handler, baseUrl: environment.baseUrl)
product = RazorpayProductRoutes(client: handler, baseUrl: environment.baseUrl)
qrCode = RazorpayQRCodeRoutes(client: handler, baseUrl: environment.baseUrl)
refund = RazorpayRefundRoutes(client: handler, baseUrl: environment.baseUrl)
settlement = RazorpaySettlementRoutes(client: handler, baseUrl: environment.baseUrl)
stakeholder = RazorpayStakeholderRoutes(client: handler, baseUrl: environment.baseUrl)
subscription = RazorpaySubscriptionRoutes(client: handler, baseUrl: environment.baseUrl)
token = RazorpayTokenRoutes(client: handler, baseUrl: environment.baseUrl)
transfer = RazorpayTransferRoutes(client: handler, baseUrl: environment.baseUrl)
virtualAccount = RazorpayVirtualAccountRoutes(client: handler, baseUrl: environment.baseUrl)
webhook = RazorpayWebhookRoutes(client: handler, baseUrl: environment.baseUrl)
public init(httpClient: HTTPClient, key: String, secret: String) {
self.handler = RazorpayAPIHandler(httpClient: httpClient, key: key, secret: secret)
account = RazorpayAccountRoutes(client: handler)
addon = RazorpayAddonRoutes(client: handler)
card = RazorpayCardRoutes(client: handler)
customer = RazorpayCustomerRoutes(client: handler)
fundAccount = RazorpayFundAccountRoutes(client: handler)
iin = RazorpayIINRoutes(client: handler)
invoice = RazorpayInvoiceRoutes(client: handler)
item = RazorpayItemRoutes(client: handler)
order = RazorpayOrderRoutes(client: handler)
payment = RazorpayPaymentRoutes(client: handler)
paymentLink = RazorpayPaymentLinkRoutes(client: handler)
product = RazorpayProductRoutes(client: handler)
qrCode = RazorpayQRCodeRoutes(client: handler)
refund = RazorpayRefundRoutes(client: handler)
settlement = RazorpaySettlementRoutes(client: handler)
stakeholder = RazorpayStakeholderRoutes(client: handler)
subscription = RazorpaySubscriptionRoutes(client: handler)
token = RazorpayTokenRoutes(client: handler)
transfer = RazorpayTransferRoutes(client: handler)
virtualAccount = RazorpayVirtualAccountRoutes(client: handler)
webhook = RazorpayWebhookRoutes(client: handler)
}
}
20 changes: 2 additions & 18 deletions Sources/RazorpayKit/RazorpayRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@ import NIOFoundationCompat
import NIOHTTP1
import AsyncHTTPClient

public enum Environment {
case production
case sandbox

var baseUrl: String {
switch self {
case .production:
return APIConstants.baseURL
case .sandbox:
return APIConstants.baseURL
}
}
}

extension HTTPClientRequest.Body {
public static func string(_ string: String) -> Self {
.bytes(.init(string: string))
Expand All @@ -34,15 +20,13 @@ extension HTTPClientRequest.Body {

struct RazorpayAPIHandler {
private let httpClient: HTTPClient
private let environment: Environment
private let key: String
private let secret: String

public init(httpClient: HTTPClient, key: String, secret: String, environment: Environment) {
public init(httpClient: HTTPClient, key: String, secret: String) {
self.httpClient = httpClient
self.key = key
self.secret = secret
self.environment = environment
}

private func authorizationHeader() -> String {
Expand All @@ -57,7 +41,7 @@ struct RazorpayAPIHandler {
headers: HTTPHeaders) async throws -> HTTPClientResponse {

let queryString = RZPRUTL.convertToQueryString(queryParams)
let url = environment.baseUrl + path + queryString
let url = APIConstants.baseURL + path + queryString

var requestHeaders: HTTPHeaders = ["Authorization": authorizationHeader(), "Content-Type": "application/json",
"Accept": "application/json"]
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ public protocol AccountRoutes: RazorpayAPIRoute {
public struct RazorpayAccountRoutes: AccountRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func create(data: [String: Any], extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/Addon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ public protocol AddonRoutes: RazorpayAPIRoute {
public struct RazorpayAddonRoutes: AddonRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func fetch(addonID: String, queryParams: [String: String]? = nil, extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/Card.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ public protocol CardRoutes: RazorpayAPIRoute {
public struct RazorpayCardRoutes: CardRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func fetch(cardID: String, queryParams: [String: String]? = nil, extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/Customer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ public protocol CustomerRoutes: RazorpayAPIRoute {
public struct RazorpayCustomerRoutes: CustomerRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func fetch(customerID: String, queryParams: [String: String]? = nil, extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/FundAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ public protocol FundAccountRoutes: RazorpayAPIRoute {
public struct RazorpayFundAccountRoutes: FundAccountRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func all(queryParams: [String: String]? = nil, extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/IIN.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ public protocol IINRoutes: RazorpayAPIRoute {
public struct RazorpayIINRoutes: IINRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func fetch(tokenIin: String, queryParams: [String: String]? = nil, extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/Invoice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ public protocol InvoiceRoutes: RazorpayAPIRoute {
public struct RazorpayInvoiceRoutes: InvoiceRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func all(queryParams: [String: String]? = nil, extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ public protocol ItemRoutes: RazorpayAPIRoute {
public struct RazorpayItemRoutes: ItemRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func all(queryParams: [String: String]? = nil, extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/Order.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ public protocol OrderRoutes: RazorpayAPIRoute {
public struct RazorpayOrderRoutes: OrderRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

// Fetches multiple orders for the given query parameters.
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/Payment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,9 @@ public protocol PaymentRoutes: RazorpayAPIRoute {
public struct RazorpayPaymentRoutes: PaymentRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func all(queryParams: [String: String]? = nil, extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/PaymentLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ public protocol PaymentLinkRoutes: RazorpayAPIRoute {
public struct RazorpayPaymentLinkRoutes: PaymentLinkRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func all(queryParams: [String: String]? = nil, extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/Product.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ public protocol ProductRoutes: RazorpayAPIRoute {
public struct RazorpayProductRoutes: ProductRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func requestProductConfiguration(accountId: String, data: [String: Any], extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/QrCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ public protocol QRCodeRoutes: RazorpayAPIRoute {
public struct RazorpayQRCodeRoutes: QRCodeRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func create(data: [String: Any], extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/Refund.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ public protocol RefundRoutes: RazorpayAPIRoute {
public struct RazorpayRefundRoutes: RefundRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func all(queryParams: [String: String]? = nil, extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/Settlement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ public protocol SettlementRoutes: RazorpayAPIRoute {
public struct RazorpaySettlementRoutes: SettlementRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func all(queryParams: [String: String]? = nil, extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
6 changes: 2 additions & 4 deletions Sources/RazorpayKit/Resources/Stakeholder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ public protocol StakeholderRoutes: RazorpayAPIRoute {
public struct RazorpayStakeholderRoutes: StakeholderRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func create(accountId: String, data: [String: Any], extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down Expand Up @@ -74,4 +72,4 @@ public struct RazorpayStakeholderRoutes: StakeholderRoutes {
let response = try await client.sendRequest(method: .GET, path: url, queryParams: queryParams, headers: RZPRUTL.convertToHTTPHeaders(extraHeaders))
return try await RZPRUTL.processResponse(response)
}
}
}
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/Subscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ public protocol SubscriptionRoutes: RazorpayAPIRoute {
public struct RazorpaySubscriptionRoutes: SubscriptionRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func all(queryParams: [String: String]? = nil, extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/Token.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ public protocol TokenRoutes: RazorpayAPIRoute {
public struct RazorpayTokenRoutes: TokenRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func create(data: [String: Any], extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/Transfer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ public protocol TransferRoutes: RazorpayAPIRoute {
public struct RazorpayTransferRoutes: TransferRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func all(queryParams: [String: String]? = nil, extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
4 changes: 1 addition & 3 deletions Sources/RazorpayKit/Resources/VirtualAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ public protocol VirtualAccountRoutes: RazorpayAPIRoute {
public struct RazorpayVirtualAccountRoutes: VirtualAccountRoutes {
public var headers: HTTPHeaders = [:]
private let client: RazorpayAPIHandler
private let baseUrl: String

init(client: RazorpayAPIHandler, baseUrl: String) {
init(client: RazorpayAPIHandler) {
self.client = client
self.baseUrl = baseUrl
}

public func all(queryParams: [String: String]? = nil, extraHeaders: [String: String]? = nil) async throws -> [String: Any] {
Expand Down
Loading

0 comments on commit 7a4671c

Please sign in to comment.