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
Completions, Edits, Embeddings, Files, Finetunes, Images, Models, Moderations
Instance Method Summary collapse
-
#http_delete(path:) ⇒ Object
Executes an HTTP DELETE on the specified path.
-
#http_get(path:) ⇒ Object
Executes an HTTP GET on the specified path.
-
#http_streamed_download(path:, writer:) ⇒ Object
Executes an HTTP GET on the specified path, streaming the resulting body to the writer in case of success.
-
#initialize(client: nil) ⇒ Base
constructor
A new instance of Base.
-
#json_post(path:, parameters:) ⇒ Object
Executes an HTTP POST with JSON-encoded parameters on the specified path.
-
#multipart_post(path:, parameters: nil) ⇒ Object
Executes an HTTP POST with multipart encoded parameters on the specified path.
Constructor Details
#initialize(client: nil) ⇒ Base
Returns a new instance of Base.
20 21 22 |
# File 'lib/asimov/api_v1/base.rb', line 20 def initialize(client: nil) @client = client end |
Instance Method Details
#http_delete(path:) ⇒ Object
Executes an HTTP DELETE on the specified path.
base_uri) against which the DELETE is executed.
31 32 33 34 35 36 37 38 |
# File 'lib/asimov/api_v1/base.rb', line 31 def http_delete(path:) wrap_response_with_error_handling do self.class.delete( path, { headers: headers }.merge!() ) end end |
#http_get(path:) ⇒ Object
Executes an HTTP GET on the specified path.
base_uri) against which the GET is executed.
46 47 48 49 50 51 52 53 |
# File 'lib/asimov/api_v1/base.rb', line 46 def http_get(path:) wrap_response_with_error_handling do self.class.get( path, { headers: headers }.merge!() ) end end |
#http_streamed_download(path:, writer:) ⇒ Object
Executes an HTTP GET on the specified path, streaming the resulting body to the writer in case of success.
base_uri) against which the POST is executed.
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/asimov/api_v1/base.rb', line 95 def http_streamed_download(path:, writer:) self.class.get(path, { 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 |
#json_post(path:, parameters:) ⇒ Object
Executes an HTTP POST with JSON-encoded parameters on the specified path.
base_uri) against which the POST is executed.
61 62 63 64 65 66 67 68 69 |
# File 'lib/asimov/api_v1/base.rb', line 61 def json_post(path:, parameters:) wrap_response_with_error_handling do self.class.post( path, { headers: headers, body: parameters&.to_json }.merge!() ) end end |
#multipart_post(path:, parameters: nil) ⇒ Object
Executes an HTTP POST with multipart encoded parameters on the specified path.
base_uri) against which the POST is executed.
77 78 79 80 81 82 83 84 85 |
# File 'lib/asimov/api_v1/base.rb', line 77 def multipart_post(path:, parameters: nil) wrap_response_with_error_handling do self.class.post( path, { headers: headers("multipart/form-data"), body: parameters }.merge!() ) end end |