Class: HttpApiClient::Client

Inherits:
Object
  • Object
show all
Includes:
Errors::Factory
Defined in:
lib/http_api_client/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Errors::Factory

#error_for_status

Constructor Details

#initialize(client_id, config_file = nil) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
24
25
26
27
# File 'lib/http_api_client/client.rb', line 18

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

#configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/http_api_client/client.rb', line 16

def config
  @config
end

Instance Method Details

#connectionObject



75
76
77
# File 'lib/http_api_client/client.rb', line 75

def connection
  @connection ||= ConnectionFactory.new(config).create
end

#create(path, payload, custom_headers = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/http_api_client/client.rb', line 52

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).as_json), request_headers(update_headers, custom_headers))
  end

  handle_response(response, :post, path)
end

#destroy(base_path, id, custom_headers = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/http_api_client/client.rb', line 63

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



29
30
31
# File 'lib/http_api_client/client.rb', line 29

def find(base_path, id, query = {})
  get("#{base_path}/#{id}", query)
end

#find_all(base_path, query = {}) ⇒ Object



37
38
39
# File 'lib/http_api_client/client.rb', line 37

def find_all(base_path, query = {})
  get("#{base_path}", query)
end

#find_nested(base_path, id, nested_path) ⇒ Object



33
34
35
# File 'lib/http_api_client/client.rb', line 33

def find_nested(base_path, id, nested_path)
  get("#{base_path}/#{id}/#{nested_path}")
end

#get(path, query = {}, custom_headers = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/http_api_client/client.rb', line 41

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