REST Client based on Alamofire 5 and RxSwift library. Supports OAuth2 and Basic authentication interceptor

iOS – Swift – Alamofire 5 – REST Client example

Example of simple OAuth2 authentication using Alamofire 5 and RxSwift library.

Supported grant types:

  • password
  • refresh_token
  • client_credentials

Supported interceptors:

  • Client credentials grant type authentication interceptor
  • Password grant type authentication and refresh token interceptor with request synchronization

Example call

extension Api {
    
    enum OAuth2Service {
                
        static func getUserAccessToken(username: String, password: String) -> AuthorizedEndpoint<Scope.Guest, UserAccessToken> {
            return AuthorizedEndpoint(
                raw: Endpoint(
                    authType: .basic,
                    method: .post,
                    path: "oauth/token",
                    parameters: [
                        "username": username,
                        "password": password,
                        "grant_type": GrantType.password
                    ]
                )
            )
        }
       ...
    }
}

 
 let client = AuthorizedClient<Scope.Guest>()
 
 client
    .request(Api.OAuth2Service.getUserAccessToken(username: username, password: password))
    .asObservable()
    .subscribe(
        onNext: { accessToken in
            DispatchQueue.main.async {
                // Store access token
            }
        },
        onError: { error in
            DispatchQueue.main.async {
                // Handle api error
                guard let errorResponse = error.asApiError?.response else {  
                    ...
                    return
                }
                ...
            }
        }
    )
    .disposed(by: disposeBag)

GitHub

View Github