Class: AtProto::Request

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

Overview

Handles HTTP requests for the AT Protocol client Throw the errors needed for the flow (Dpop and TokenInvalid)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, uri, headers = {}, body = nil) ⇒ Request

Creates a new Request instance

Parameters:

  • method (Symbol)

    The HTTP method (:get, :post, :put, :delete)

  • uri (String)

    The URI for the request

  • headers (Hash) (defaults to: {})

    Optional headers to include

  • body (String, nil) (defaults to: nil)

    Optional request body



20
21
22
23
24
25
# File 'lib/atproto_client/request.rb', line 20

def initialize(method, uri, headers = {}, body = nil)
  @uri = URI(uri)
  @method = method
  @headers = headers
  @body = body
end

Instance Attribute Details

#bodyURI, ...

Returns:

  • (URI)

    The URI for the request

  • (Symbol)

    The HTTP method to use

  • (Hash)

    Headers to be sent with the request

  • (String, nil)

    The request body



11
12
13
# File 'lib/atproto_client/request.rb', line 11

def body
  @body
end

#headersURI, ...

Returns:

  • (URI)

    The URI for the request

  • (Symbol)

    The HTTP method to use

  • (Hash)

    Headers to be sent with the request

  • (String, nil)

    The request body



11
12
13
# File 'lib/atproto_client/request.rb', line 11

def headers
  @headers
end

#methodURI, ...

Returns:

  • (URI)

    The URI for the request

  • (Symbol)

    The HTTP method to use

  • (Hash)

    Headers to be sent with the request

  • (String, nil)

    The request body



11
12
13
# File 'lib/atproto_client/request.rb', line 11

def method
  @method
end

#uriURI, ...

Returns:

  • (URI)

    The URI for the request

  • (Symbol)

    The HTTP method to use

  • (Hash)

    Headers to be sent with the request

  • (String, nil)

    The request body



11
12
13
# File 'lib/atproto_client/request.rb', line 11

def uri
  @uri
end

Instance Method Details

#runHash

Executes the HTTP request

Makes the HTTP request with configured parameters and handles the response. Automatically sets Content-Type and Accept headers to application/json.

Returns:

  • (Hash)

    Parsed JSON response body

Raises:

  • (Net::HTTPClientException)

    On bad request

  • (TokenExpiredError)

    When the authentication token has expired

  • (AuthError)

    When authentication fails

  • (APIError)

    When the API returns an unexpected error



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/atproto_client/request.rb', line 37

def run
  request_class = HTTP_METHODS[method]
  req = request_class.new(uri).tap do |request|
    headers.each { |k, v| request[k] = v }
    request['Content-Type'] = 'application/json'
    request['Accept'] = 'application/json'
    request.body = body
  end
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(req)
  end
  handle_response(response)
end