Class: MoondreamClient::Client
- Inherits:
-
Object
- Object
- MoondreamClient::Client
- Defined in:
- lib/moondream-client/client.rb
Defined Under Namespace
Classes: SSEDecoder
Instance Attribute Summary collapse
-
#configuration ⇒ MoondreamClient::Configuration
The configuration for the client.
Instance Method Summary collapse
-
#get(path) ⇒ Hash
Make a GET request to the API.
-
#handle_error(response) ⇒ void
Handle errors from the API.
-
#initialize(configuration = MoondreamClient.configuration) ⇒ MoondreamClient::Client
constructor
Initialize the client.
-
#post(path, payload, headers: {}) ⇒ Hash
Make a POST request to the API.
-
#post_stream(path, payload = {}, headers: {}) {|data| ... } ⇒ void
Make a streaming POST request to the API using text/event-stream.
Constructor Details
#initialize(configuration = MoondreamClient.configuration) ⇒ MoondreamClient::Client
Initialize the client.
15 16 17 |
# File 'lib/moondream-client/client.rb', line 15 def initialize(configuration = MoondreamClient.configuration) @configuration = configuration end |
Instance Attribute Details
#configuration ⇒ MoondreamClient::Configuration
The configuration for the client.
8 9 10 |
# File 'lib/moondream-client/client.rb', line 8 def configuration @configuration end |
Instance Method Details
#get(path) ⇒ Hash
Make a GET request to the API.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/moondream-client/client.rb', line 45 def get(path) response = connection.get(build_url(path)) do |request| request.headers["X-Moondream-Auth"] = @configuration.access_token if @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.
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/moondream-client/client.rb', line 93 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 |
#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/moondream-client/client.rb', line 26 def post(path, payload, headers: {}) response = connection.post(build_url(path)) do |request| request.headers["X-Moondream-Auth"] = @configuration.access_token if @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 |
#post_stream(path, payload = {}, headers: {}) {|data| ... } ⇒ void
This method returns an undefined value.
Make a streaming POST request to the API using text/event-stream. Parses SSE lines and yields decoded event data Hashes to the provided block.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/moondream-client/client.rb', line 64 def post_stream(path, payload = {}, headers: {}, &block) decoder = SSEDecoder.new buffer = "" connection.post(build_url(path)) do |request| request.headers["X-Moondream-Auth"] = @configuration.access_token if @configuration.access_token request.headers["Accept"] = "text/event-stream" request.headers["Cache-Control"] = "no-store" request.headers["Content-Type"] = "application/json" request.headers.merge!(headers) request.body = payload.compact.to_json request..on_data = lambda { |chunk, _total_bytes, _env| # Normalize and split into lines, preserving last partial line in buffer buffer = (buffer + chunk.to_s).gsub(/\r\n?/, "\n") lines = buffer.split("\n", -1) buffer = lines.pop || "" lines.each do |line| event = decoder.decode(line) block&.call(event["data"]) if event && event["data"] end } end end |