Class: ApiAdaptor::JsonClient
- Inherits:
-
Object
- Object
- ApiAdaptor::JsonClient
- Includes:
- ExceptionHandling
- Defined in:
- lib/api_adaptor/json_client.rb
Overview
HTTP client for JSON APIs with comprehensive redirect handling and authentication support.
JSONClient provides a low-level interface for making HTTP requests to JSON APIs. It handles automatic JSON parsing, configurable redirect following, authentication (bearer token and basic auth), timeout management, and comprehensive error handling.
Constant Summary collapse
- DEFAULT_TIMEOUT_IN_SECONDS =
Default request timeout in seconds
4- DEFAULT_MAX_REDIRECTS =
Default maximum number of redirects to follow
3
Instance Attribute Summary collapse
Class Method Summary collapse
-
.default_request_headers ⇒ Hash
Returns default HTTP headers for all requests.
-
.default_request_with_json_body_headers ⇒ Hash
Returns default headers for requests with JSON body.
-
.json_body_headers ⇒ Hash
Returns Content-Type header for JSON requests.
Instance Method Summary collapse
-
#delete_json(url, params = {}, additional_headers = {}) ⇒ Response
Performs a DELETE request with optional JSON body.
-
#get_json(url, additional_headers = {}) {|response| ... } ⇒ Response, Object
Performs a GET request and parses the JSON response.
-
#get_raw(url) ⇒ RestClient::Response
Performs a GET request and returns the raw response (alias for get_raw!).
-
#get_raw!(url) ⇒ RestClient::Response
Performs a GET request and returns the raw response.
-
#initialize(options = {}) ⇒ JsonClient
constructor
Initializes a new JSON HTTP client.
-
#patch_json(url, params, additional_headers = {}) ⇒ Response
Performs a PATCH request with JSON body.
-
#post_json(url, params = {}, additional_headers = {}) ⇒ Response
Performs a POST request with JSON body.
-
#post_multipart(url, params) ⇒ Response
Performs a POST request with multipart/form-data.
-
#put_json(url, params, additional_headers = {}) ⇒ Response
Performs a PUT request with JSON body.
-
#put_multipart(url, params) ⇒ Response
Performs a PUT request with multipart/form-data.
Methods included from ExceptionHandling
#build_specific_http_error, #error_class_for_code
Constructor Details
#initialize(options = {}) ⇒ JsonClient
Initializes a new JSON HTTP client
74 75 76 77 78 79 |
# File 'lib/api_adaptor/json_client.rb', line 74 def initialize( = {}) raise "It is no longer possible to disable the timeout." if [:disable_timeout] || [:timeout].to_i.negative? @logger = [:logger] || NullLogger.new = end |
Instance Attribute Details
#logger ⇒ Logger, Hash
46 47 48 |
# File 'lib/api_adaptor/json_client.rb', line 46 def logger @logger end |
#options ⇒ Logger, Hash
46 47 48 |
# File 'lib/api_adaptor/json_client.rb', line 46 def end |
Class Method Details
.default_request_headers ⇒ Hash
Returns default HTTP headers for all requests
84 85 86 87 88 89 |
# File 'lib/api_adaptor/json_client.rb', line 84 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_headers ⇒ Hash
Returns default headers for requests with JSON body
94 95 96 |
# File 'lib/api_adaptor/json_client.rb', line 94 def self.default_request_with_json_body_headers default_request_headers.merge(json_body_headers) end |
.json_body_headers ⇒ Hash
Returns Content-Type header for JSON requests
101 102 103 104 105 |
# File 'lib/api_adaptor/json_client.rb', line 101 def self.json_body_headers { "Content-Type" => "application/json" } end |
Instance Method Details
#delete_json(url, params = {}, additional_headers = {}) ⇒ Response
Performs a DELETE request with optional JSON body
219 220 221 |
# File 'lib/api_adaptor/json_client.rb', line 219 def delete_json(url, params = {}, additional_headers = {}) do_json_request(:delete, url, params, additional_headers) end |
#get_json(url, additional_headers = {}) {|response| ... } ⇒ Response, Object
Performs a GET request and parses the JSON response
161 162 163 |
# File 'lib/api_adaptor/json_client.rb', line 161 def get_json(url, additional_headers = {}, &create_response) do_json_request(:get, url, nil, additional_headers, &create_response) end |
#get_raw(url) ⇒ RestClient::Response
Performs a GET request and returns the raw response (alias for get_raw!)
136 137 138 |
# File 'lib/api_adaptor/json_client.rb', line 136 def get_raw(url) get_raw!(url) end |
#get_raw!(url) ⇒ RestClient::Response
Performs a GET request and returns the raw response
125 126 127 |
# File 'lib/api_adaptor/json_client.rb', line 125 def get_raw!(url) do_raw_request(:get, url) end |
#patch_json(url, params, additional_headers = {}) ⇒ Response
Performs a PATCH request with JSON body
206 207 208 |
# File 'lib/api_adaptor/json_client.rb', line 206 def patch_json(url, params, additional_headers = {}) do_json_request(:patch, url, params, additional_headers) end |
#post_json(url, params = {}, additional_headers = {}) ⇒ Response
Performs a POST request with JSON body
180 181 182 |
# File 'lib/api_adaptor/json_client.rb', line 180 def post_json(url, params = {}, additional_headers = {}) do_json_request(:post, url, params, additional_headers) end |
#post_multipart(url, params) ⇒ Response
Performs a POST request with multipart/form-data
235 236 237 238 |
# File 'lib/api_adaptor/json_client.rb', line 235 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 = {}) ⇒ Response
Performs a PUT request with JSON body
193 194 195 |
# File 'lib/api_adaptor/json_client.rb', line 193 def put_json(url, params, additional_headers = {}) do_json_request(:put, url, params, additional_headers) end |
#put_multipart(url, params) ⇒ Response
Performs a PUT request with multipart/form-data
246 247 248 249 |
# File 'lib/api_adaptor/json_client.rb', line 246 def put_multipart(url, params) r = do_raw_request(:put, url, params.merge(multipart: true)) Response.new(r) end |