Class: AtProto::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/atproto_client/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token, refresh_token, dpop_handler = nil) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
# File 'lib/atproto_client/client.rb', line 5

def initialize(access_token, refresh_token, dpop_handler = nil)
  @access_token = access_token
  @refresh_token = refresh_token
  @dpop_handler = dpop_handler || DpopHandler.new
  @token_mutex = Mutex.new
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



3
4
5
# File 'lib/atproto_client/client.rb', line 3

def access_token
  @access_token
end

#dpop_handlerObject (readonly)

Returns the value of attribute dpop_handler.



3
4
5
# File 'lib/atproto_client/client.rb', line 3

def dpop_handler
  @dpop_handler
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



3
4
5
# File 'lib/atproto_client/client.rb', line 3

def refresh_token
  @refresh_token
end

Instance Method Details

#make_api_request(method, url, params: {}, body: nil) ⇒ Object



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

def make_api_request(method, url, params: {}, body: nil)
  retries = 0
  begin
    uri = URI(url)
    uri.query = URI.encode_www_form(params) if params.any?
    @dpop_handler.make_request(
      uri.to_s,
      method,
      headers: { 'Authorization' => "Bearer #{@access_token}" },
      body: body
    )
  rescue TokenExpiredError => e
    raise e unless retries.zero? && @refresh_token

    retries += 1
    refresh_access_token!
    retry
  end
end