Class: MoondreamClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/moondream-client/client.rb

Defined Under Namespace

Classes: SSEDecoder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = MoondreamClient.configuration) ⇒ MoondreamClient::Client

Initialize the client.

Parameters:



15
16
17
# File 'lib/moondream-client/client.rb', line 15

def initialize(configuration = MoondreamClient.configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationMoondreamClient::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.

Parameters:

  • path (String)

    The path to the API endpoint.

Returns:

  • (Hash)

    The response from 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.

Parameters:

  • response (Faraday::Response)

    The response 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 UnauthorizedError, 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.

Parameters:

  • path (String)

    The path to the API endpoint.

  • payload (Hash)

    The payload to send to the API.

  • headers (Hash) (defaults to: {})

    The headers to send to the API.

Returns:

  • (Hash)

    The response from 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.

Parameters:

  • path (String)
  • payload (Hash) (defaults to: {})
  • headers (Hash) (defaults to: {})

Yields:

  • (data)

    yields parsed JSON from each SSE event’s data field



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.options.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