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

Definition

() async throws -> AccessTokenDetails

Response

TypeDescription
AccessTokenDetailsA credential representing the authorization granted by the resource owner (user) to the ReadyRemit SDK

Sample Implementation

struct CreateAccessTokenDetailsResponse: AccessTokenDetails {
    var accessToken: String
    var expiresIn: Int
    var scope: String
    var tokenType: String
}

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:
        return try JSONDecoder().decode(CreateAccessTokenDetailsResponse.self, from: data)
    default:
        throw CustomError.unsuccessfulResponse
    }
}