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

Included in:
AccessControl::Runners::AccessControl, Account::Runners::Account, Aggregate::Runners::Aggregate, Approval::Runners::Approval, Asset::Runners::Asset, Attachment::Runners::Attachment, Audit::Runners::Audit, BusinessRule::Runners::BusinessRule, Calendar::Runners::Calendar, CatalogTask::Runners::CatalogTask, CatalogVariable::Runners::CatalogVariable, Change::Runners::Change, CiRelationship::Runners::CiRelationship, Client, Cmdb::Instance::Runners::Instance, Cmdb::Meta::Runners::Meta, CmdbHealth::Runners::CmdbHealth, Company::Runners::Company, Contract::Runners::Contract, CostCenter::Runners::CostCenter, Currency::Runners::Currency, Department::Runners::Department, DeprecationLog::Runners::DeprecationLog, Discovery::Runners::Discovery, EmailLog::Runners::EmailLog, Event::Runners::Event, Flow::Runners::Flow, Legion::Extensions::ServiceNow::HrCase::Runners::HrCase, ImportSet::Runners::ImportSet, Incident::Runners::Incident, Knowledge::Runners::Knowledge, KnowledgeBase::Runners::KnowledgeBase, KnowledgeFeedback::Runners::KnowledgeFeedback, License::Runners::License, Location::Runners::Location, Metric::Runners::Metric, MidServer::Runners::MidServer, Notification::Runners::Notification, OnCall::Runners::OnCall, PerformanceAnalytics::Runners::PerformanceAnalytics, Problem::Runners::Problem, Project::Runners::Project, Release::Runners::Release, Request::Runners::Request, ScheduledJob::Runners::ScheduledJob, ScriptAction::Runners::ScriptAction, ScriptInclude::Runners::ScriptInclude, SecurityIncident::Runners::SecurityIncident, ServiceCatalog::Runners::ServiceCatalog, Sla::Runners::Sla, Survey::Runners::Survey, SystemProperty::Runners::SystemProperty, Table::Runners::Table, Tag::Runners::Tag, Task::Runners::Task, UiAction::Runners::UiAction, UiPolicy::Runners::UiPolicy, UpdateSet::Runners::UpdateSet, User::Runners::User, UserGroup::Runners::UserGroup, Vendor::Runners::Vendor, WorkOrder::Runners::WorkOrder, Workflow::Runners::Workflow
Defined in:
lib/legion/extensions/service_now/helpers/client.rb

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

#handle_response(resp) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/legion/extensions/service_now/helpers/client.rb', line 42

def handle_response(resp)
  case resp.status
  when 200, 201, 202, 204
    resp
  when 401
    raise Errors::AuthenticationError.new(error_message(resp), status: 401, detail: resp.body)
  when 403
    raise Errors::AuthorizationError.new(error_message(resp), status: 403, detail: resp.body)
  when 404
    raise Errors::NotFoundError.new(error_message(resp), status: 404, detail: resp.body)
  when 422
    raise Errors::UnprocessableError.new(error_message(resp), status: 422, detail: resp.body)
  when 429
    raise Errors::RateLimitError.new(error_message(resp), status: 429, detail: resp.body)
  when 500..599
    raise Errors::ServerError.new(error_message(resp), status: resp.status, detail: resp.body)
  else
    raise Errors::ServiceNowError.new(error_message(resp), status: resp.status, detail: resp.body)
  end
end