Class: Asimov::ApiV1::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
HTTParty
Defined in:
lib/asimov/api_v1/base.rb

Overview

Base class for API interface implementations. Currently manages the network logic for the interface.

Instance Method Summary collapse

Constructor Details

#initialize(client: nil) ⇒ Base

Returns a new instance of Base.



20
21
22
# File 'lib/asimov/api_v1/base.rb', line 20

def initialize(client: nil)
  @client = client
end

Instance Method Details

#http_delete(path:) ⇒ Object

Executes an HTTP DELETE on the specified path.

base_uri) against which the DELETE is executed.

Parameters:

  • path (String)

    the URI (when combined with the



31
32
33
34
35
36
37
38
# File 'lib/asimov/api_v1/base.rb', line 31

def http_delete(path:)
  wrap_response_with_error_handling do
    self.class.delete(
      path,
      { headers: headers }.merge!(request_options)
    )
  end
end

#http_get(path:) ⇒ Object

Executes an HTTP GET on the specified path.

base_uri) against which the GET is executed.

Parameters:

  • path (String)

    the URI (when combined with the



46
47
48
49
50
51
52
53
# File 'lib/asimov/api_v1/base.rb', line 46

def http_get(path:)
  wrap_response_with_error_handling do
    self.class.get(
      path,
      { headers: headers }.merge!(request_options)
    )
  end
end

#http_streamed_download(path:, writer:) ⇒ Object

Executes an HTTP GET on the specified path, streaming the resulting body to the writer in case of success.

base_uri) against which the POST is executed.

Parameters:

  • path (String)

    the URI (when combined with the

  • writer (Writer)

    an object, typically a File, that responds to a ‘write` method



95
96
97
98
99
100
101
102
103
104
# File 'lib/asimov/api_v1/base.rb', line 95

def http_streamed_download(path:, writer:)
  self.class.get(path,
                 { headers: headers,
                   stream_body: true }.merge!(request_options)) do |fragment|
    fragment.code == 200 ? writer.write(fragment) : check_for_api_error(fragment)
  end
rescue StandardError => e
  # Any error raised by the HTTP call is a network error
  NetworkErrorTranslator.translate(e)
end

#json_post(path:, parameters:) ⇒ Object

Executes an HTTP POST with JSON-encoded parameters on the specified path.

base_uri) against which the POST is executed.

Parameters:

  • path (String)

    the URI (when combined with the



61
62
63
64
65
66
67
68
69
# File 'lib/asimov/api_v1/base.rb', line 61

def json_post(path:, parameters:)
  wrap_response_with_error_handling do
    self.class.post(
      path,
      { headers: headers,
        body: parameters&.to_json }.merge!(request_options)
    )
  end
end

#multipart_post(path:, parameters: nil) ⇒ Object

Executes an HTTP POST with multipart encoded parameters on the specified path.

base_uri) against which the POST is executed.

Parameters:

  • path (String)

    the URI (when combined with the



77
78
79
80
81
82
83
84
85
# File 'lib/asimov/api_v1/base.rb', line 77

def multipart_post(path:, parameters: nil)
  wrap_response_with_error_handling do
    self.class.post(
      path,
      { headers: headers("multipart/form-data"),
        body: parameters }.merge!(request_options)
    )
  end
end