class Oauth2ApiClient
The Oauth2ApiClient
class is a client wrapped around the oauth2 and http-rb gem to interact with APIs using oauth2 for authentication with automatic token caching and renewal.
Constants
- VERSION
Public Class Methods
new(base_url:, base_request: HTTP, token:)
click to toggle source
Creates a new Oauth2ApiClient
@param base_url [String] The base url of the API to interact with @param token [String, Oauth2ApiClient::TokenProvider] Allows to pass an
existing token received via external sources or an instance of `Oauth2ApiClient::TokenProvider` which is capable of generating tokens when client id, client secret, etc. is given
@example
client = Oauth2ApiClient.new( base_url: "https://api.example.com", token: "the api token" ) client.post("/orders", json: { address: "..." }).status.success? client.headers("User-Agent" => "API Client").timeout(read: 5, write: 5).get("/orders").parse
@example
client = Oauth2ApiClient.new( base_url: "https://api.example.com", token: Oauth2ApiClient::TokenProvider.new( client_id: "the client id", client_secret: "the client secret", oauth_token_url: "https://auth.example.com/oauth2/token", cache: Rails.cache ) )
# File lib/oauth2_api_client.rb, line 44 def initialize(base_url:, base_request: HTTP, token:) @base_url = base_url @token = token @request = base_request end
Public Instance Methods
token()
click to toggle source
Returns a oauth2 token to use for authentication
@return [String] The token
# File lib/oauth2_api_client.rb, line 54 def token @token.respond_to?(:to_str) ? @token.to_str : @token.token end
Private Instance Methods
execute(verb, path, options = {})
click to toggle source
# File lib/oauth2_api_client.rb, line 76 def execute(verb, path, options = {}) with_retry do response = @request.auth("Bearer #{token}").send(verb, "#{@base_url}#{path}", options) return response if response.status.success? raise ResponseError.for(response) end end
with_retry() { || ... }
click to toggle source
# File lib/oauth2_api_client.rb, line 86 def with_retry retried = false begin yield rescue ResponseError => e if !retried && e.response.status.unauthorized? && @token.respond_to?(:invalidate_token) @token.invalidate_token retried = true retry end raise end end