Class: MixinBot::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mixin_bot/client.rb,
lib/mixin_bot/client/error_mapper.rb

Overview

HTTP client for making requests to the Mixin Network API.

Defined Under Namespace

Modules: ErrorMapper

Constant Summary collapse

SERVER_SCHEME =
'https'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mixin_bot/client.rb', line 14

def initialize(config)
  @config = config || MixinBot.config
  @conn = Faraday.new(
    url: "#{SERVER_SCHEME}://#{config.api_host}",
    headers: {
      'Content-Type' => 'application/json',
      'User-Agent' => "mixin_bot/#{MixinBot::VERSION}"
    }
  ) do |f|
    f.request :json
    f.request :retry, max: 2, interval: 0.5, interval_randomness: 0.5, backoff_factor: 2,
                      exceptions: [Faraday::ConnectionFailed, Faraday::TimeoutError]
    f.response :json
    f.response :logger if config.debug
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/mixin_bot/client.rb', line 12

def config
  @config
end

#connObject (readonly)

Returns the value of attribute conn.



12
13
14
# File 'lib/mixin_bot/client.rb', line 12

def conn
  @conn
end

Instance Method Details

#fetch_get(path, query: nil, access_token: nil, exp_in: 600, scp: 'FULL') ⇒ Object

Explicit query-string GET (preferred for new code).



75
76
77
78
79
80
81
82
83
# File 'lib/mixin_bot/client.rb', line 75

def fetch_get(path, query: nil, access_token: nil, exp_in: 600, scp: 'FULL')
  q = (query || {}).dup
  q.compact!
  body = ''
  full_path = q.empty? ? path : "#{path}?#{URI.encode_www_form(q.sort_by { |k, _| k.to_s })}"
  token = access_token.presence || sign_token('GET', full_path, body, exp_in:, scp:)
  response = @conn.get(full_path, nil, authorization_headers(token))
  parse_response!(verb: 'GET', path: full_path, body:, response:)
end

#fetch_post(path, body:, access_token: nil, exp_in: 600, scp: 'FULL') ⇒ Object

Explicit JSON-object POST (preferred for new code).



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

def fetch_post(path, body:, access_token: nil, exp_in: 600, scp: 'FULL')
  payload = body.is_a?(String) ? body : body.compact.to_json
  token = access_token.presence || sign_token('POST', path, payload, exp_in:, scp:)
  response = @conn.post(path, payload, authorization_headers(token))
  parse_response!(verb: 'POST', path:, body: payload, response:)
end

#fetch_post_array(path, array_body, access_token: nil, exp_in: 600, scp: 'FULL') ⇒ Object

Explicit JSON-array POST (e.g. /users/fetch, /safe/keys).



98
99
100
101
102
103
# File 'lib/mixin_bot/client.rb', line 98

def fetch_post_array(path, array_body, access_token: nil, exp_in: 600, scp: 'FULL')
  payload = array_body.to_json
  token = access_token.presence || sign_token('POST', path, payload, exp_in:, scp:)
  response = @conn.post(path, payload, authorization_headers(token))
  parse_response!(verb: 'POST', path:, body: payload, response:)
end

#get(path, **kwargs) ⇒ MixinBot::Models::ApiEnvelope

GET request. Remaining keyword arguments are treated as query-string parameters.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mixin_bot/client.rb', line 36

def get(path, **kwargs)
  access_token = kwargs.delete(:access_token)
  exp_in = kwargs.delete(:exp_in) || 600
  scp = kwargs.delete(:scp) || 'FULL'

  kwargs.compact!
  body = ''
  full_path = kwargs.empty? ? path : "#{path}?#{URI.encode_www_form(kwargs.sort_by { |k, _| k.to_s })}"

  token = access_token.presence || sign_token('GET', full_path, body, exp_in:, scp:)
  response = @conn.get(full_path, nil, authorization_headers(token))
  parse_response!(verb: 'GET', path: full_path, body:, response:)
end

#post(path, *args, **kwargs) ⇒ MixinBot::Models::ApiEnvelope

POST with a Hash body (+**kwargs+ merged into JSON object) or an Array body (+*args+).



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mixin_bot/client.rb', line 55

def post(path, *args, **kwargs)
  access_token = kwargs.delete(:access_token)
  exp_in = kwargs.delete(:exp_in) || 600
  scp = kwargs.delete(:scp) || 'FULL'

  body =
    if args.present?
      args.to_json
    else
      kwargs.compact.to_json
    end

  token = access_token.presence || sign_token('POST', path, body, exp_in:, scp:)
  response = @conn.post(path, body, authorization_headers(token))
  parse_response!(verb: 'POST', path:, body:, response:)
end