1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
module ATS
module Shiro
class API
HEADERS = {
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'User-Agent' => "RubyGems/ATS #{ATS::VERSION}",
}.freeze
attr_reader :http, :port, :host, :scheme, :bearer_token
def initialize(configuration:, debug: false)
@http = Net::Hippie::Client.new(headers: HEADERS, verify_mode: debug ? OpenSSL::SSL::VERIFY_NONE : nil)
@configuration = configuration
@port = configuration[:port]
@scheme = configuration[:scheme]
@host = configuration[:host]
@bearer_token = configuration[:bearer_token]
end
def tokens
ATS::Shiro::Tokens.new(self)
end
def version
get("version")
end
def get(url, params: {}, version: 1)
http.get(build_uri(url, version: version), headers: headers, body: params) do |request, response|
JSON.parse(response.body, symbolize_names: true)
end
end
def post(url, params: {}, version: 1)
http.post(build_uri(url, version: version), headers: headers, body: params) do |request, response|
JSON.parse(response.body, symbolize_names: true)
end
end
private
def build_uri(relative_url, version:)
URI::Generic.build(host: host, port: port, scheme: scheme, path: "/api/v#{version}/#{relative_url}")
end
def headers
{ AUTHORIZATION: "Bearer #{bearer_token}" }
end
end
end
end
|