Class: AtProto::Client
- Inherits:
-
Object
- Object
- AtProto::Client
- Defined in:
- lib/atproto_client/client.rb
Overview
The Client class handles authenticated HTTP requests to the AT Protocol services with DPoP token support and automatic token refresh capabilities.
Instance Attribute Summary collapse
-
#access_token ⇒ String
readonly
The current access token for authentication.
-
#dpop_handler ⇒ DpopHandler
readonly
The handler for DPoP token operations.
-
#refresh_token ⇒ String
readonly
The current refresh token for renewing access.
Instance Method Summary collapse
-
#initialize(access_token:, refresh_token:, private_key:, refresh_token_url: 'https://bsky.social') ⇒ Client
constructor
Initializes a new AT Protocol client.
-
#private_key=(private_key) ⇒ Object
Sets a new private key for DPoP token signing.
-
#request(method, url, params: {}, body: nil) ⇒ Net::HTTPResponse
Makes an authenticated HTTP request with automatic token refresh.
Constructor Details
#initialize(access_token:, refresh_token:, private_key:, refresh_token_url: 'https://bsky.social') ⇒ Client
Initializes a new AT Protocol client
19 20 21 22 23 24 25 |
# File 'lib/atproto_client/client.rb', line 19 def initialize(access_token:, refresh_token:, private_key:, refresh_token_url: 'https://bsky.social') @access_token = access_token @refresh_token = refresh_token @refresh_token_url = refresh_token_url @dpop_handler = DpopHandler.new(private_key, access_token) @token_mutex = Mutex.new end |
Instance Attribute Details
#access_token ⇒ String (readonly)
The current access token for authentication
8 9 10 |
# File 'lib/atproto_client/client.rb', line 8 def access_token @access_token end |
#dpop_handler ⇒ DpopHandler (readonly)
The handler for DPoP token operations
8 9 10 |
# File 'lib/atproto_client/client.rb', line 8 def dpop_handler @dpop_handler end |
#refresh_token ⇒ String (readonly)
The current refresh token for renewing access
8 9 10 |
# File 'lib/atproto_client/client.rb', line 8 def refresh_token @refresh_token end |
Instance Method Details
#private_key=(private_key) ⇒ Object
Sets a new private key for DPoP token signing
31 32 33 |
# File 'lib/atproto_client/client.rb', line 31 def private_key=(private_key) @dpop_handler = @dpop_handler.new(private_key, @access_token) end |
#request(method, url, params: {}, body: nil) ⇒ Net::HTTPResponse
Makes an authenticated HTTP request with automatic token refresh
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/atproto_client/client.rb', line 46 def 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' => "DPoP #{@access_token}" }, body: body ) rescue TokenExpiredError => e raise e unless retries.zero? && @refresh_token retries += 1 refresh_access_token! retry end end |