Class: NzPostApi::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/nz_post_api/auth.rb

Constant Summary collapse

TOKEN_URL =
"https://oauth.nzpost.co.nz/as/token.oauth2"

Class Method Summary collapse

Class Method Details

.fetch_token(client_id, client_secret) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/nz_post_api/auth.rb', line 11

def self.fetch_token(client_id, client_secret)
  params = {
    client_id: client_id,
    client_secret: client_secret,
    grant_type: "client_credentials"
  }
  url_with_params = "#{TOKEN_URL}?#{URI.encode_www_form(params)}"
  response = Faraday.post(url_with_params, nil)

  if response.success?
    JSON.parse(response.body)
  else
    raise NzPostApi::Error.new(
      "Failed to fetch token: #{response.status} - #{response.body}",
      response_http_code: response.status,
      response_body: response.body
    )
  end
end