Class: ReplicateClient::Client

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

Instance Method Summary collapse

Constructor Details

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

Initialize the client.

Parameters:



10
11
12
# File 'lib/replicate-client/client.rb', line 10

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

Instance Method Details

#delete(path) ⇒ void

This method returns an undefined value.

Make a DELETE request to the API.

Parameters:

  • path (String)

    The path to the API endpoint.



54
55
56
57
58
59
60
61
# File 'lib/replicate-client/client.rb', line 54

def delete(path)
  response = connection.delete(build_url(path)) do |request|
    request.headers["Authorization"] = "Bearer #{@configuration.access_token}"
    request.headers["Content-Type"] = "application/json"
  end

  handle_error(response) unless response.success?
end

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



38
39
40
41
42
43
44
45
46
47
# File 'lib/replicate-client/client.rb', line 38

def get(path)
  response = connection.get(build_url(path)) do |request|
    request.headers["Authorization"] = "Bearer #{@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.



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/replicate-client/client.rb', line 81

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

#patch(path, payload) ⇒ Object



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

def patch(path, payload)
  response = connection.patch(build_url(path)) do |request|
    request.headers["Authorization"] = "Bearer #{@configuration.access_token}"
    request.headers["Content-Type"] = "application/json"
    request.headers["Accept"] = "application/json"
    request.body = payload.compact.to_json
  end

  handle_error(response) unless response.success?

  JSON.parse(response.body)
end

#post(path, payload) ⇒ 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.

Returns:

  • (Hash)

    The response from the API.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/replicate-client/client.rb', line 20

def post(path, payload)
  response = connection.post(build_url(path)) do |request|
    request.headers["Authorization"] = "Bearer #{@configuration.access_token}"
    request.headers["Content-Type"] = "application/json"
    request.headers["Accept"] = "application/json"
    request.body = payload.compact.to_json
  end

  handle_error(response) unless response.success?

  JSON.parse(response.body)
end