Class: BunnyApp::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/bunny_app/client.rb

Constant Summary collapse

USER_AGENT =
"BunnyApp Ruby v#{BunnyApp::VERSION}".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bunny_app/client.rb', line 12

def initialize
  self.class.base_uri BunnyApp.base_uri
  self.class.default_options.update(verify: verify_ssl)
  self.class.default_timeout 3 # seconds

  BunnyApp.access_token ||= fetch_access_token
  @headers = {
    'User-Agent' => USER_AGENT,
    'Content-Type' => 'application/json',
    'Authorization' => "Bearer #{BunnyApp.access_token}"
  }

  @headers['host'] = host_header unless host_header.nil?
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



8
9
10
# File 'lib/bunny_app/client.rb', line 8

def headers
  @headers
end

Instance Method Details

#fetch_access_tokenObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bunny_app/client.rb', line 27

def fetch_access_token
  body = URI.encode_www_form({
                               grant_type: 'client_credentials',
                               client_id: BunnyApp.client_id,
                               client_secret: BunnyApp.client_secret,
                               scope: BunnyApp.scope
                             })

  headers = {
    'Content-Type' => 'application/x-www-form-urlencoded'
  }

  res = self.class.post('/oauth/token', headers:, body:)

  case res.code.to_s
  when /2[0-9][0-9]/ # HTTP 2xx
    BunnyApp.retryable = true
    res['access_token']
  when /40[0-9]/
    raise AuthorizationError, res.parsed_response['error_description']
  else
    raise ResponseError, res.body
  end
end

#host_headerObject



99
100
101
# File 'lib/bunny_app/client.rb', line 99

def host_header
  ENV.fetch('BUNNY_HOST_HEADER', nil)
end

#query(query, variables, retries = 0) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bunny_app/client.rb', line 52

def query(query, variables, retries = 0)
  body = {
    query:,
    variables:
  }.to_json

  @headers['Authorization'] = "Bearer #{BunnyApp.access_token}"

  res = self.class.post('/graphql', headers: @headers, body:)

  case res.code.to_s
  when /2[0-9][0-9]/ # HTTP 2xx
    response_body = JSON.parse(res.body)
    raise ResponseError, (response_body['errors'].map { |error| error['message'] }) if response_body['errors']

    response_body

  when /401/ # Access Token Expired
    raise AuthorizationError, 'Invalid access token' unless BunnyApp.retryable
    raise AuthorizationError, 'Invalid api credentials' if retries >= 1

    BunnyApp.access_token = fetch_access_token
    retries += 1
    query(query, variables, retries)

  when /403/ # HTTP 403
    raise AuthorizationError, res.parsed_response['error_decscription']
  else
    # HTTP 400, 500 etc
    puts "Bunny SDK Response Error Headers: #{res.headers}"
    puts "Bunny SDK Response Error Body: #{res.body}"
    raise ResponseError, res.body
  end
end

#query_async(query, variables) ⇒ Object

Performs the api request within a new Thread, so it is non blocking. Consider it fire and forget



89
90
91
92
93
# File 'lib/bunny_app/client.rb', line 89

def query_async(query, variables)
  Thread.new do
    query(query, variables)
  end
end

#verify_sslObject



95
96
97
# File 'lib/bunny_app/client.rb', line 95

def verify_ssl
  true unless ENV['IGNORE_SSL']
end