Class: ReplicateClient::Client
- Inherits:
-
Object
- Object
- ReplicateClient::Client
- Defined in:
- lib/replicate-client/client.rb
Instance Attribute Summary collapse
-
#configuration ⇒ ReplicateClient::Configuration
The configuration for the client.
Instance Method Summary collapse
-
#delete(path) ⇒ void
Make a DELETE request to the API.
-
#get(path) ⇒ Hash
Make a GET request to the API.
-
#handle_error(response) ⇒ void
Handle errors from the API.
-
#initialize(configuration = ReplicateClient.configuration) ⇒ ReplicateClient::Client
constructor
Initialize the client.
- #patch(path, payload) ⇒ Object
-
#post(path, payload, headers: {}) ⇒ Hash
Make a POST request to the API.
Constructor Details
#initialize(configuration = ReplicateClient.configuration) ⇒ ReplicateClient::Client
Initialize the client.
15 16 17 |
# File 'lib/replicate-client/client.rb', line 15 def initialize(configuration = ReplicateClient.configuration) @configuration = configuration end |
Instance Attribute Details
#configuration ⇒ ReplicateClient::Configuration
The configuration for the client.
8 9 10 |
# File 'lib/replicate-client/client.rb', line 8 def configuration @configuration end |
Instance Method Details
#delete(path) ⇒ void
This method returns an undefined value.
Make a DELETE request to the API.
61 62 63 64 65 66 67 68 |
# File 'lib/replicate-client/client.rb', line 61 def delete(path) response = connection.delete(build_url(path)) do |request| request.headers["Authorization"] = "Bearer #{@configuration.access_token}" request.headers["Content-Type"] = "application/json" end handle_error(response) unless response.success? end |
#get(path) ⇒ Hash
Make a GET request to the API.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/replicate-client/client.rb', line 45 def get(path) response = connection.get(build_url(path)) do |request| request.headers["Authorization"] = "Bearer #{@configuration.access_token}" request.headers["Content-Type"] = "application/json" end handle_error(response) unless response.success? JSON.parse(response.body) end |
#handle_error(response) ⇒ void
This method returns an undefined value.
Handle errors from the API.
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/replicate-client/client.rb', line 88 def handle_error(response) case response.status when 401 raise , response.body when 403 raise ForbiddenError, response.body when 404 raise NotFoundError, response.body else raise ServerError, response.body end end |
#patch(path, payload) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/replicate-client/client.rb', line 70 def patch(path, payload) response = connection.patch(build_url(path)) do |request| request.headers["Authorization"] = "Bearer #{@configuration.access_token}" request.headers["Content-Type"] = "application/json" request.headers["Accept"] = "application/json" request.body = payload.compact.to_json end handle_error(response) unless response.success? JSON.parse(response.body) end |
#post(path, payload, headers: {}) ⇒ Hash
Make a POST request to the API.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/replicate-client/client.rb', line 26 def post(path, payload, headers: {}) response = connection.post(build_url(path)) do |request| request.headers["Authorization"] = "Bearer #{@configuration.access_token}" request.headers["Content-Type"] = "application/json" request.headers["Accept"] = "application/json" request.headers.merge!(headers) request.body = payload.compact.to_json end handle_error(response) unless response.success? JSON.parse(response.body) end |