Class: Gusto::Client

Inherits:
BaseClient show all
Defined in:
lib/fern_gusto.rb

Defined Under Namespace

Classes: Options

Instance Attribute Summary

Attributes inherited from BaseClient

#ach_transactions, #companies, #contractors, #employees, #events, #flows, #generated_documents, #holiday_pay_policies, #invoices, #notifications, #payroll, #recovery_cases, #reports, #time_off_policies, #token, #webhooks, #wire_in_requests

Instance Method Summary collapse

Constructor Details

#initialize(auth:, environment: Gusto::Environment::DEMO, base_url: nil, max_retries: nil, timeout_in_seconds: nil, gusto_api_version: nil) ⇒ Gusto::Client

Instantiate a new Gusto API client

Examples:

Create client with system access token auth

auth = Gusto::SystemAccessTokenAuth.new(
  type: :system_access_token,
  client_id: 'client_id',
  client_secret: 'client_secret'
)
client = Gusto::Client.new(auth: auth, environment: :production)

Create client with company access token auth

auth = Gusto::CompanyAccessTokenAuth.new(
  type: :company_access_token,
  client_id: 'client_id',
  client_secret: 'client_secret',
  redirect_uri: 'https://example.com/callback',
  refresh_token: 'refresh_token',
  access_token: 'access_token'
)
client = Gusto::Client.new(auth: auth, environment: :production)

Create client with token auth

auth = Gusto::TokenAuth.new(
  type: :token,
  token: 'token'
)
client = Gusto::Client.new(auth: auth, environment: :production)

Parameters:

  • auth (SystemAccessTokenAuth, CompanyAccessTokenAuth, TokenAuth)

    Authentication configuration

  • environment (Gusto::Environment) (defaults to: Gusto::Environment::DEMO)

    The Gusto environment to connect to

  • base_url (String) (defaults to: nil)

    The base URL for API requests

  • max_retries (Long) (defaults to: nil)

    The number of times to retry a failed request, defaults to 2

  • timeout_in_seconds (Long) (defaults to: nil)

    The timeout for API requests in seconds

  • gusto_api_version (String) (defaults to: nil)

    Determines the date-based API version associated with your API call



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/fern_gusto.rb', line 122

def initialize(auth:, environment: Gusto::Environment::DEMO, base_url: nil, max_retries: nil, timeout_in_seconds: nil, gusto_api_version: nil)
  token_supplier = case auth
  when SystemAccessTokenAuth
    refresher = SystemAccessTokenRefresher.new(
      auth.client_id,
      auth.client_secret
    )
    -> { refresher.get_token }
  when CompanyAccessTokenAuth
    refresher = CompanyAccessTokenRefresher.new(
      auth.client_id,
      auth.client_secret,
      auth.redirect_uri,
      auth.refresh_token
    )
    -> { refresher.get_token }
  when TokenAuth
    -> { auth.token }
  end

  @request_client = Gusto::RequestClient.new(
    base_url: base_url,
    environment: environment,
    max_retries: max_retries,
    timeout_in_seconds: timeout_in_seconds,
    token: token_supplier,
    gusto_api_version: gusto_api_version
  )

  super(
    environment: environment,
    token: token_supplier.call,
  )
end