Class: Payabli::Token::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/payabli/token/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ void

Parameters:



9
10
11
# File 'lib/payabli/token/client.rb', line 9

def initialize(client:)
  @client = client
end

Instance Method Details

#create_server_side_token(request_options: {}, **params) ⇒ Payabli::Types::PayabliAccessTokenResponse

Exchanges a client ID and client secret for a short-lived Bearer access token using the OAuth2 client-credentials flow. Designed for server-to-server use: the credentials and the returned token stay on your backend. Send the returned access_token in the Authorization header as Bearer <access_token> on subsequent API calls. See the OAuth authentication guide for the full flow.

Examples:

client.token.create_server_side_token(
  client_id: "YOUR_CLIENT_ID",
  client_secret: "YOUR_CLIENT_SECRET"
)

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/payabli/token/client.rb', line 33

def create_server_side_token(request_options: {}, **params)
  params = Payabli::Internal::Types::Utils.normalize_keys(params)
  headers = @client.auth_headers_for_endpoint(security: nil)
  request = Payabli::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "v2/Token/serverside",
    headers: headers,
    body: Payabli::Token::Types::CreateServerSideTokenRequest.new(params).to_h,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Payabli::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Payabli::Types::PayabliAccessTokenResponse.load(response.body)
  else
    error_class = Payabli::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end