Class: Fal::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = Fal.configuration) ⇒ Client

Returns a new instance of Client.



7
8
9
# File 'lib/fal/client.rb', line 7

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

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



5
6
7
# File 'lib/fal/client.rb', line 5

def configuration
  @configuration
end

Instance Method Details

#get(path, query: nil, headers: {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fal/client.rb', line 62

def get(path, query: nil, headers: {})
  url = build_url(path)
  url = "#{url}?#{URI.encode_www_form(query)}" if query && !query.empty?

  response = connection.get(url) do |request|
    request.headers["Authorization"] = "Key #{@configuration.api_key}" if @configuration.api_key
    request.headers["Accept"] = "application/json"
    request.headers.merge!(headers)
  end

  handle_error(response) unless response.success?

  parse_json(response.body)
end

#get_api(path, query: nil, headers: {}) ⇒ Hash?

Perform a GET against the platform API base (api.fal.ai/v1)

Parameters:

  • path (String)
  • query (Hash, nil) (defaults to: nil)
  • headers (Hash) (defaults to: {})

Returns:

  • (Hash, nil)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fal/client.rb', line 82

def get_api(path, query: nil, headers: {})
  url = build_api_url(path)
  url = "#{url}?#{URI.encode_www_form(query)}" if query && !query.empty?

  response = connection.get(url) do |request|
    request.headers["Authorization"] = "Key #{@configuration.api_key}" if @configuration.api_key
    request.headers["Accept"] = "application/json"
    request.headers.merge!(headers)
  end

  handle_error(response) unless response.success?

  parse_json(response.body)
end

#handle_error(response) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fal/client.rb', line 110

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: {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fal/client.rb', line 11

def post(path, payload = {}, headers: {})
  response = connection.post(build_url(path)) do |request|
    request.headers["Authorization"] = "Key #{@configuration.api_key}" if @configuration.api_key
    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?

  parse_json(response.body)
end

#post_api(path, payload = {}, headers: {}) ⇒ Hash?

Perform a POST against the platform API base (api.fal.ai/v1)

Parameters:

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

Returns:

  • (Hash, nil)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fal/client.rb', line 30

def post_api(path, payload = {}, headers: {})
  response = connection.post(build_api_url(path)) do |request|
    request.headers["Authorization"] = "Key #{@configuration.api_key}" if @configuration.api_key
    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?

  parse_json(response.body)
end

#post_stream(path, payload = {}, on_data:) ⇒ void

This method returns an undefined value.

Perform a POST to the streaming (sync) base with SSE/text-event-stream handling. The provided on_data Proc will be used to receive chunked data.

Parameters:

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

    called with chunks as they arrive



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fal/client.rb', line 50

def post_stream(path, payload = {}, on_data:)
  url = build_sync_url(path)
  connection.post(url) do |request|
    request.headers["Authorization"] = "Key #{@configuration.api_key}" if @configuration.api_key
    request.headers["Accept"] = "text/event-stream"
    request.headers["Cache-Control"] = "no-store"
    request.headers["Content-Type"] = "application/json"
    request.body = payload.compact.to_json
    request.options.on_data = on_data
  end
end

#put(path) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fal/client.rb', line 97

def put(path)
  response = connection.put(build_url(path)) do |request|
    request.headers["Authorization"] = "Key #{@configuration.api_key}" if @configuration.api_key
    request.headers["Content-Type"] = "application/json"
    request.headers["Accept"] = "application/json"
    request.body = {}.to_json
  end

  handle_error(response) unless response.success?

  parse_json(response.body)
end