Class: Replicate::Endpoint

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

Overview

Network layer for API clients.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint_url:, api_token:, content_type: 'application/json') ⇒ Endpoint

Returns a new instance of Endpoint.



14
15
16
17
18
# File 'lib/replicate/endpoint.rb', line 14

def initialize(endpoint_url:, api_token:, content_type: 'application/json')
  @endpoint_url = endpoint_url
  @api_token = api_token
  @content_type = content_type
end

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



12
13
14
# File 'lib/replicate/endpoint.rb', line 12

def api_token
  @api_token
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



12
13
14
# File 'lib/replicate/endpoint.rb', line 12

def content_type
  @content_type
end

#endpoint_urlObject (readonly)

Returns the value of attribute endpoint_url.



12
13
14
# File 'lib/replicate/endpoint.rb', line 12

def endpoint_url
  @endpoint_url
end

Instance Method Details

#agentSawyer::Agent

Hypermedia agent for the datatrans API

Returns:

  • (Sawyer::Agent)


77
78
79
80
81
82
83
84
85
# File 'lib/replicate/endpoint.rb', line 77

def agent
  @agent ||= Faraday.new(url: endpoint_url) do |conn|
    conn.request :retry
    conn.request :authorization, 'Token', api_token if api_token
    conn.headers["Content-Type"] = content_type

    conn.adapter :net_http
  end
end

#delete(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP DELETE request

Parameters:

  • url (String)

    The path, relative to #api_endpoint

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

    Query and header params for request

Returns:

  • (Sawyer::Resource)


61
62
63
# File 'lib/replicate/endpoint.rb', line 61

def delete(url, options = {})
  request :delete, url, options
end

#get(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP GET request

Parameters:

  • url (String)

    The path, relative to #api_endpoint

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

    Query and header params for request

Returns:

  • (Sawyer::Resource)


25
26
27
# File 'lib/replicate/endpoint.rb', line 25

def get(url, options = {})
  request :get, url, options
end

#head(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP HEAD request

Parameters:

  • url (String)

    The path, relative to #api_endpoint

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

    Query and header params for request

Returns:

  • (Sawyer::Resource)


70
71
72
# File 'lib/replicate/endpoint.rb', line 70

def head(url, options = {})
  request :head, url, options
end

#last_responseSawyer::Response

Response for last HTTP request

Returns:

  • (Sawyer::Response)


90
91
92
# File 'lib/replicate/endpoint.rb', line 90

def last_response
  @last_response if defined? @last_response
end

#patch(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP PATCH request

Parameters:

  • url (String)

    The path, relative to #api_endpoint

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

    Body and header params for request

Returns:

  • (Sawyer::Resource)


52
53
54
# File 'lib/replicate/endpoint.rb', line 52

def patch(url, options = {})
  request :patch, url, options.to_json
end

#post(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP POST request

Parameters:

  • url (String)

    The path, relative to #api_endpoint

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

    Body and header params for request

Returns:

  • (Sawyer::Resource)


34
35
36
# File 'lib/replicate/endpoint.rb', line 34

def post(url, options = {})
  request :post, url, options.to_json
end

#put(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP PUT request

Parameters:

  • url (String)

    The path, relative to #api_endpoint

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

    Body and header params for request

Returns:

  • (Sawyer::Resource)


43
44
45
# File 'lib/replicate/endpoint.rb', line 43

def put(url, options = {})
  request :put, url, options.to_json
end