fetchAccessTokenDetails

Overview

    +-------------+           +----------+           +----------+
    |  Customer   |----(1)--->| Customer |----(2)--->| PayMitto |
    | Application |<---(4)----|   API    |<---(3)----|    API   |
    +-------------+           +----------+           +----------+
           |
           |                  +----------+
            -----------(5)--->| PayMitto |
                              |    SDK   |
                              +----------+
  1. Customer application makes a network request to the Customer API to initiate the creation
    of an access token for the PayMitto SDK
  2. Customer API makes a network request to the PayMitto API to create an access token for
    the PayMitto SDK
  3. PayMitto 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 PayMitto 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
    }
}