Class: ReactorSDK::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/reactor_sdk/connection.rb

Constant Summary collapse

ACCEPT_HEADER =

Pins the Reactor API to version 1 on every request.

'application/vnd.api+json;revision=1'
CONTENT_TYPE =

Required content type for all write requests

'application/vnd.api+json'

Instance Method Summary collapse

Constructor Details

#initialize(config, auth, rate_limiter = RateLimiter.new) ⇒ Connection

Returns a new instance of Connection.

Parameters:



31
32
33
34
35
36
# File 'lib/reactor_sdk/connection.rb', line 31

def initialize(config, auth, rate_limiter = RateLimiter.new)
  @config       = config
  @auth         = auth
  @rate_limiter = rate_limiter
  @http         = build_faraday_connection
end

Instance Method Details

#delete(path) ⇒ nil

Executes an authenticated DELETE request with no body.

Parameters:

  • path (String)

    Relative API path

Returns:

  • (nil)

Raises:



107
108
109
110
111
# File 'lib/reactor_sdk/connection.rb', line 107

def delete(path)
  @rate_limiter.acquire
  response = @http.delete(path) { |req| inject_headers(req) }
  handle_response(response)
end

#delete_relationship(path, body) ⇒ nil

Executes an authenticated DELETE request with a JSON body. Used for JSON:API relationship removal.

Parameters:

  • path (String)

    Relative API path

  • body (Hash)

    Relationship payload

Returns:

  • (nil)

Raises:



122
123
124
125
126
127
128
# File 'lib/reactor_sdk/connection.rb', line 122

def delete_relationship(path, body)
  @rate_limiter.acquire
  response = @http.run_request(:delete, path, body.to_json, {}) do |req|
    inject_headers(req)
  end
  handle_response(response)
end

#get(path, params: {}) ⇒ Hash?

Executes an authenticated GET request.

Parameters:

  • path (String)

    Relative path

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

    Optional query string parameters

Returns:

  • (Hash, nil)

    Parsed JSON response body

Raises:



46
47
48
49
50
# File 'lib/reactor_sdk/connection.rb', line 46

def get(path, params: {})
  @rate_limiter.acquire
  response = @http.get(path, params) { |req| inject_headers(req) }
  handle_response(response)
end

#patch(path, body) ⇒ Hash?

Executes an authenticated PATCH request.

Parameters:

  • path (String)

    Relative API path

  • body (Hash)

    Partial update body

Returns:

  • (Hash, nil)

    Parsed JSON response body

Raises:



94
95
96
97
98
# File 'lib/reactor_sdk/connection.rb', line 94

def patch(path, body)
  @rate_limiter.acquire
  response = @http.patch(path, body.to_json) { |req| inject_headers(req) }
  handle_response(response)
end

#post(path, body) ⇒ Hash?

Executes an authenticated POST request.

Parameters:

  • path (String)

    Relative API path

  • body (Hash)

    Request body

Returns:

  • (Hash, nil)

    Parsed JSON response body

Raises:



60
61
62
63
64
# File 'lib/reactor_sdk/connection.rb', line 60

def post(path, body)
  @rate_limiter.acquire
  response = @http.post(path, body.to_json) { |req| inject_headers(req) }
  handle_response(response)
end

#post_multipart(path, file_path:, field_name: 'package', mime_type: 'application/octet-stream') ⇒ Hash?

Executes an authenticated multipart POST request.

Parameters:

  • path (String)

    Relative API path

  • file_path (String)

    Path to the file to upload

  • field_name (String) (defaults to: 'package')

    Multipart field name

  • mime_type (String) (defaults to: 'application/octet-stream')

    MIME type for the uploaded file

Returns:

  • (Hash, nil)

    Parsed JSON response body



75
76
77
78
79
80
81
82
83
84
# File 'lib/reactor_sdk/connection.rb', line 75

def post_multipart(path, file_path:, field_name: 'package', mime_type: 'application/octet-stream')
  @rate_limiter.acquire
  response = @http.post(path) do |req|
    inject_headers(req, content_type: nil)
    req.body = {
      field_name => Faraday::Multipart::FilePart.new(file_path, mime_type)
    }
  end
  handle_response(response)
end