fetchAccessTokenDetails

A closure to retrieve credentials to interface with the ReadyRemit API on behalf of the sender

Overview

    +-------------+           +----------+           +------------+
    |  Customer   |----(1)--->| Customer |----(2)--->| ReadyRemit |
    | Application |<---(4)----|   API    |<---(3)----|    API     |
    +-------------+           +----------+           +------------+
           |
           |                  +------------+
            -----------(5)--->| ReadyRemit |
                              |    SDK     |
                              +------------+
  1. Customer application makes a network request to the Customer API to initiate the creation of an access token for the ReadyRemit SDK
  2. Customer API makes a network request to the ReadyRemit API to create an access token for the ReadyRemit SDK
  3. ReadyRemit API returns the access token details or an error
  4. Customer API returns the access token details or an error
  5. Customer application returns the access token details or throws an error

Return Type

public protocol AccessTokenDetails: Sendable {
    var accessToken: String { get }
    var expiresIn: Int { get }
    var scope: String { get }
    var tokenType: String { get }
}

Sample Implementation

private func fetchAccessTokenDetails() async throws -> AccessTokenDetails {
    guard let url = URL(string: "https://example.com/v1/oauth/token") else {
        throw URLError(.badURL)
    }
    var urlRequest = URLRequest(url: url)
    urlRequest.httpMethod = "POST"
    let (data, urlResponse) = try await URLSession.shared.data(for: urlRequest)
    guard let httpURLResponse = urlResponse as? HTTPURLResponse else {
        throw URLError(.badServerResponse)
    }
    switch httpURLResponse.statusCode {
    case 200...299:
        // CreateAccessTokenDetailsResponse must conform to AccessTokenDetails
        return try JSONDecoder().decode(CreateAccessTokenDetailsResponse.self, from: data)
    default:
        throw CustomError.unsuccessfulResponse
    }
}