Class: Asimov::ApiV1::Base
- Inherits:
-
Object
- Object
- Asimov::ApiV1::Base
- Extended by:
- Forwardable
- Includes:
- HTTParty
- Defined in:
- lib/asimov/api_v1/base.rb
Overview
Base class for API interface implementations. Currently manages the network logic for the interface.
Direct Known Subclasses
Audio, Chat, Completions, Edits, Embeddings, Files, Finetunes, Images, Models, Moderations
Instance Method Summary collapse
-
#initialize(client: nil) ⇒ Base
constructor
A new instance of Base.
-
#rest_create_w_json_params(resource:, parameters:) ⇒ Object
Executes a REST create with JSON-encoded parameters for the specified resource.
-
#rest_create_w_multipart_params(resource:, parameters: nil) ⇒ Object
Executes a REST create with multipart-encoded parameters for the specified resource.
-
#rest_delete(resource:, id:) ⇒ Object
Executes a REST delete on the specified resource.
-
#rest_get(resource:, id:) ⇒ Object
Executes a REST get on the specified resource.
-
#rest_get_streamed_download(resource:, writer:) ⇒ Object
Executes an REST get on the specified path, streaming the resulting body to the writer in case of success.
-
#rest_index(resource:) ⇒ Object
Executes a REST index for the specified resource.
Constructor Details
#initialize(client: nil) ⇒ Base
Returns a new instance of Base.
18 19 20 |
# File 'lib/asimov/api_v1/base.rb', line 18 def initialize(client: nil) @client = client end |
Instance Method Details
#rest_create_w_json_params(resource:, parameters:) ⇒ Object
Executes a REST create with JSON-encoded parameters for the specified resource.
to create the resource
75 76 77 78 79 80 81 82 83 |
# File 'lib/asimov/api_v1/base.rb', line 75 def rest_create_w_json_params(resource:, parameters:) wrap_response_with_error_handling do self.class.post( absolute_path("/#{Array(resource).join('/')}"), { headers: headers, body: parameters&.to_json }.merge!() ) end end |
#rest_create_w_multipart_params(resource:, parameters: nil) ⇒ Object
Executes a REST create with multipart-encoded parameters for the specified resource.
to create the resource
93 94 95 96 97 98 99 100 101 |
# File 'lib/asimov/api_v1/base.rb', line 93 def rest_create_w_multipart_params(resource:, parameters: nil) wrap_response_with_error_handling do self.class.post( absolute_path("/#{Array(resource).join('/')}"), { headers: headers("multipart/form-data"), body: parameters }.merge!() ) end end |
#rest_delete(resource:, id:) ⇒ Object
Executes a REST delete on the specified resource.
43 44 45 46 47 48 49 50 |
# File 'lib/asimov/api_v1/base.rb', line 43 def rest_delete(resource:, id:) wrap_response_with_error_handling do self.class.delete( absolute_path("/#{resource}/#{CGI.escape(id)}"), { headers: headers }.merge!() ) end end |
#rest_get(resource:, id:) ⇒ Object
Executes a REST get on the specified resource.
58 59 60 61 62 63 64 65 |
# File 'lib/asimov/api_v1/base.rb', line 58 def rest_get(resource:, id:) wrap_response_with_error_handling do self.class.get( absolute_path("/#{resource}/#{CGI.escape(id)}"), { headers: headers }.merge!() ) end end |
#rest_get_streamed_download(resource:, writer:) ⇒ Object
Executes an REST get on the specified path, streaming the resulting body to the writer in case of success.
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/asimov/api_v1/base.rb', line 110 def rest_get_streamed_download(resource:, writer:) self.class.get(absolute_path("/#{Array(resource).join('/')}"), { headers: headers, stream_body: true }.merge!()) do |fragment| fragment.code == 200 ? writer.write(fragment) : check_for_api_error(fragment) end rescue StandardError => e # Any error raised by the HTTP call is a network error NetworkErrorTranslator.translate(e) end |
#rest_index(resource:) ⇒ Object
Executes a REST index for the specified resource
28 29 30 31 32 33 34 35 |
# File 'lib/asimov/api_v1/base.rb', line 28 def rest_index(resource:) wrap_response_with_error_handling do self.class.get( absolute_path("/#{Array(resource).join('/')}"), { headers: headers }.merge!() ) end end |