Module: Sourcescrub::Utils::Request

Included in:
Client
Defined in:
lib/sourcescrub/utils/request.rb

Overview

Key Reminder

Class Method Summary collapse

Class Method Details

.authenticateObject

Body:

grant_type=password&username={username}&password={password}&scope=client_api


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sourcescrub/utils/request.rb', line 84

def authenticate
  body = "grant_type=password&username=#{Sourcescrub.account.username}&password=#{Sourcescrub.account.password}&scope=client_api"

  response = Faraday.new(
    url: TOKEN_URL,
    headers: {
      'Content-Type' => 'application/x-www-form-urlencoded',
      'Authorization' => Sourcescrub..basic
    }
  ).post(TOKEN_URI, body)

  raise 'Sourcescrub error: Service Unavailable' unless response.status == 200

  @token = JSON.parse(response.body)['access_token']
end

.get(uri, *args) ⇒ Object

Limit of requests per second: 30

Raises:



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sourcescrub/utils/request.rb', line 22

def get(uri, *args)
  response = Faraday.new(request_options(args)) do |faraday|
    faraday.headers['Content-Type'] = 'application/json-patch+json'
    faraday.adapter Faraday.default_adapter
    faraday.response :logger, ::Logger.new(STDOUT), bodies: true if debug_mode?
  end.get(uri)

  raise Error, response.body unless response.status == 200

  parse_api_response(response.body).merge('headers' => response.headers)
end

.search(uri, args) ⇒ Object

Search endpoints

Raises:



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sourcescrub/utils/request.rb', line 35

def search(uri, args)
  response = Faraday.new(request_options(args)) do |faraday|
    faraday.headers['Content-Type'] = 'application/json-patch+json'
    faraday.adapter Faraday.default_adapter
    faraday.response :logger, ::Logger.new(STDOUT), bodies: true if debug_mode?
  end.post(uri, args.to_json)

  raise Error, response.body unless response.status == 200

  parse_api_response(response.body).merge('headers' => response.headers)
end