Class: HttpApiClient::Client
- Inherits:
-
Object
- Object
- HttpApiClient::Client
- Includes:
- Errors::Factory
- Defined in:
- lib/http_api_client/client.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #connection ⇒ Object
- #create(path, payload, custom_headers = {}) ⇒ Object
- #destroy(base_path, id, custom_headers = {}) ⇒ Object
- #find(base_path, id, query = {}) ⇒ Object
- #find_all(base_path, query = {}) ⇒ Object
- #find_nested(base_path, id, nested_path) ⇒ Object
- #get(path, query = {}, custom_headers = {}) ⇒ Object
-
#initialize(client_id, config_file = nil) ⇒ Client
constructor
A new instance of Client.
Methods included from Errors::Factory
Constructor Details
#initialize(client_id, config_file = nil) ⇒ Client
Returns a new instance of Client.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/http_api_client/client.rb', line 19 def initialize(client_id, config_file = nil) raise "You must supply a http client config id (as defined in #{config_file || Config::DEFAULT_CONFIG_FILE_LOCATION}" unless client_id if config_file @config = Config.new(config_file).send(client_id) else @config = Config.new.send(client_id) end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
17 18 19 |
# File 'lib/http_api_client/client.rb', line 17 def config @config end |
Instance Method Details
#connection ⇒ Object
76 77 78 |
# File 'lib/http_api_client/client.rb', line 76 def connection @connection ||= ConnectionFactory.new(config).create end |
#create(path, payload, custom_headers = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/http_api_client/client.rb', line 53 def create(path, payload, custom_headers = {}) log_data = { method: 'post', remote_host: config.server, path: full_path(path) } response = HttpApiClient.metrics.time('http_api_client_request', log_data) do connection.post(full_path(path), JSON.fast_generate(with_auth(payload)), request_headers(update_headers, custom_headers)) end handle_response(response, :post, path) end |
#destroy(base_path, id, custom_headers = {}) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/http_api_client/client.rb', line 64 def destroy(base_path, id, custom_headers = {}) path = "#{base_path}/#{id}" log_data = { method: 'delete', remote_host: config.server, path: full_path(path) } response = HttpApiClient.metrics.time('http_api_client_request', log_data) do connection.delete(full_path(path), request_headers(update_headers, custom_headers)) end handle_response(response, :delete, path) end |
#find(base_path, id, query = {}) ⇒ Object
30 31 32 |
# File 'lib/http_api_client/client.rb', line 30 def find(base_path, id, query = {}) get("#{base_path}/#{id}", query) end |
#find_all(base_path, query = {}) ⇒ Object
38 39 40 |
# File 'lib/http_api_client/client.rb', line 38 def find_all(base_path, query = {}) get("#{base_path}", query) end |
#find_nested(base_path, id, nested_path) ⇒ Object
34 35 36 |
# File 'lib/http_api_client/client.rb', line 34 def find_nested(base_path, id, nested_path) get("#{base_path}/#{id}/#{nested_path}") end |
#get(path, query = {}, custom_headers = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/http_api_client/client.rb', line 42 def get(path, query = {}, custom_headers = {}) log_data = { method: 'get', remote_host: config.server, path: path_with_query(path, query) } response = HttpApiClient.metrics.time('http_api_client_request', log_data) do connection.get(full_path(path), with_auth(query), request_headers(get_headers, custom_headers)) end handle_response(response, :get, path) end |