Class: ApiAdaptor::JsonClient

Inherits:
Object
  • Object
show all
Includes:
ExceptionHandling
Defined in:
lib/api_adaptor/json_client.rb

Constant Summary collapse

DEFAULT_TIMEOUT_IN_SECONDS =
4

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExceptionHandling

#build_specific_http_error, #error_class_for_code

Constructor Details

#initialize(options = {}) ⇒ JsonClient

Returns a new instance of JsonClient.



17
18
19
20
21
22
# File 'lib/api_adaptor/json_client.rb', line 17

def initialize(options = {})
  raise "It is no longer possible to disable the timeout." if options[:disable_timeout] || options[:timeout].to_i.negative?

  @logger = options[:logger] || NullLogger.new
  @options = options
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



15
16
17
# File 'lib/api_adaptor/json_client.rb', line 15

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/api_adaptor/json_client.rb', line 15

def options
  @options
end

Class Method Details

.default_request_headersObject



24
25
26
27
28
29
# File 'lib/api_adaptor/json_client.rb', line 24

def self.default_request_headers
  {
    "Accept" => "application/json",
    "User-Agent" => "#{Variables.app_name}/#{Variables.app_version} (#{Variables.app_contact})"
  }
end

.default_request_with_json_body_headersObject



31
32
33
# File 'lib/api_adaptor/json_client.rb', line 31

def self.default_request_with_json_body_headers
  default_request_headers.merge(json_body_headers)
end

.json_body_headersObject



35
36
37
38
39
# File 'lib/api_adaptor/json_client.rb', line 35

def self.json_body_headers
  {
    "Content-Type" => "application/json"
  }
end

Instance Method Details

#delete_json(url, params = {}, additional_headers = {}) ⇒ Object



67
68
69
# File 'lib/api_adaptor/json_client.rb', line 67

def delete_json(url, params = {}, additional_headers = {})
  do_json_request(:delete, url, params, additional_headers)
end

#get_json(url, additional_headers = {}, &create_response) ⇒ Object



51
52
53
# File 'lib/api_adaptor/json_client.rb', line 51

def get_json(url, additional_headers = {}, &create_response)
  do_json_request(:get, url, nil, additional_headers, &create_response)
end

#get_raw(url) ⇒ Object



47
48
49
# File 'lib/api_adaptor/json_client.rb', line 47

def get_raw(url)
  get_raw!(url)
end

#get_raw!(url) ⇒ Object



43
44
45
# File 'lib/api_adaptor/json_client.rb', line 43

def get_raw!(url)
  do_raw_request(:get, url)
end

#patch_json(url, params, additional_headers = {}) ⇒ Object



63
64
65
# File 'lib/api_adaptor/json_client.rb', line 63

def patch_json(url, params, additional_headers = {})
  do_json_request(:patch, url, params, additional_headers)
end

#post_json(url, params = {}, additional_headers = {}) ⇒ Object



55
56
57
# File 'lib/api_adaptor/json_client.rb', line 55

def post_json(url, params = {}, additional_headers = {})
  do_json_request(:post, url, params, additional_headers)
end

#post_multipart(url, params) ⇒ Object



71
72
73
74
# File 'lib/api_adaptor/json_client.rb', line 71

def post_multipart(url, params)
  r = do_raw_request(:post, url, params.merge(multipart: true))
  Response.new(r)
end

#put_json(url, params, additional_headers = {}) ⇒ Object



59
60
61
# File 'lib/api_adaptor/json_client.rb', line 59

def put_json(url, params, additional_headers = {})
  do_json_request(:put, url, params, additional_headers)
end

#put_multipart(url, params) ⇒ Object



76
77
78
79
# File 'lib/api_adaptor/json_client.rb', line 76

def put_multipart(url, params)
  r = do_raw_request(:put, url, params.merge(multipart: true))
  Response.new(r)
end