Module: Legion::Extensions::ServiceNow::Helpers::Client

Instance Method Summary collapse

Instance Method Details

#connection(url: nil, client_id: nil, client_secret: nil, token: nil, username: nil, password: nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/legion/extensions/service_now/helpers/client.rb', line 10

def connection(url: nil, client_id: nil, client_secret: nil,
               token: nil, username: nil, password: nil, **)
  base_url = url || Legion::Settings[:service_now][:url]
  Faraday.new(url: base_url) do |conn|
    conn.request :json
    conn.response :json, content_type: /\bjson$/
    if client_id && client_secret
      conn.headers['Authorization'] = "Bearer #{fetch_oauth2_token(base_url, client_id, client_secret)}"
    elsif token
      conn.headers['Authorization'] = "Bearer #{token}"
    elsif username && password
      conn.request :authorization, :basic, username, password
    end
    conn.adapter Faraday.default_adapter
  end
end

#fetch_oauth2_token(base_url, client_id, client_secret) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/legion/extensions/service_now/helpers/client.rb', line 27

def fetch_oauth2_token(base_url, client_id, client_secret)
  @fetch_oauth2_token ||= begin
    resp = Faraday.new(url: base_url) do |conn|
      conn.request :url_encoded
      conn.response :json, content_type: /\bjson$/
      conn.adapter Faraday.default_adapter
    end.post('/oauth_token.do', {
               grant_type:    'client_credentials',
               client_id:     client_id,
               client_secret: client_secret
             })
    resp.body['access_token']
  end
end