Class: ReactorSDK::Connection
- Inherits:
-
Object
- Object
- ReactorSDK::Connection
- 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
-
#delete(path) ⇒ nil
Executes an authenticated DELETE request with no body.
-
#delete_relationship(path, body) ⇒ nil
Executes an authenticated DELETE request with a JSON body.
-
#get(path, params: {}) ⇒ Hash?
Executes an authenticated GET request.
-
#initialize(config, auth, rate_limiter = RateLimiter.new) ⇒ Connection
constructor
A new instance of Connection.
-
#patch(path, body) ⇒ Hash?
Executes an authenticated PATCH request.
-
#post(path, body) ⇒ Hash?
Executes an authenticated POST request.
-
#post_multipart(path, file_path:, field_name: 'package', mime_type: 'application/octet-stream') ⇒ Hash?
Executes an authenticated multipart POST request.
Constructor Details
#initialize(config, auth, rate_limiter = RateLimiter.new) ⇒ Connection
Returns a new instance of Connection.
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.
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.
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.
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.
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.
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.
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 |