Class: Replicate::Client

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

Overview

Client for the Replicate API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:, &block) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
# File 'lib/replicate/client.rb', line 10

def initialize(token:, &block)
  @token = token
  if block_given?
    conn &block
  else
    conn
  end

end

Instance Attribute Details

#conn(&block) ⇒ Object (readonly)

Returns the value of attribute conn.



8
9
10
# File 'lib/replicate/client.rb', line 8

def conn
  @conn
end

#tokenObject (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/replicate/client.rb', line 8

def token
  @token
end

Instance Method Details

#headersObject



33
34
35
36
37
38
# File 'lib/replicate/client.rb', line 33

def headers
  return {
    "Authorization": "Token #{@token}",
    "User-Agent": "replicate-ruby@#{Replicate::VERSION}",
  }
end

#models(path) ⇒ Object



29
30
31
# File 'lib/replicate/client.rb', line 29

def models(path)
  return Replicate::Model.new(client: self, path: path)
end

#requests(method:, path:, **kwargs) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/replicate/client.rb', line 40

def requests(method:, path:, **kwargs)
  if ["GET", "OPTIONS"].include? method
    kwargs = kwargs.merge({allow_redirects: true})
  end 
  if ["HEAD"].include? method
    kwargs = kwargs.merge({allow_redirects: false})
  end
  response = @conn.send(method.downcase, path, kwargs)
  if 400 <= response.status && response.status < 600
    raise ("HTTP error: #{response.status}, #{response.reason_phrase}")
  end
  return response
end