Class: Network::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/network/client.rb

Overview

This class is simple JSON client that is meant to be initialized with a single URI. Subsequent calls should target endpoints/paths of that URI.

Defined Under Namespace

Classes: Response

Constant Summary collapse

DEFAULT_HEADERS =
{ 'Accept' => 'application/json', 'Content-Type' => 'application/json' }
LOG_TAG =

Stamp in front of each log written by client @logger.

'[NETWORK CLIENT]:'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:, tries: 2, headers: {}, username: nil, password: nil, user_agent: 'Network Client') ⇒ Client

Construct and prepare client for requests targeting endpoint.

Parameters:

[endpoint] string Uri for the host with schema and port. any other segment like paths will be discarded. [tries] integer to specify how many is to repeat failed calls. Default is 2. [headers] hash to contain any common HTTP headers to be set in client calls. [username] string for HTTP basic authentication. Applies on all requests. Default to nil. [password] string for HTTP basic authentication. Applies on all requests. Default to nil. [user_agent] string Specifies the User-Agent header value when making requests. User-Agent header value provided within headers parameter in initialize or on one of request methods will take precedence over user_agent parameter.

Example:

require "network-client"

github_client = Network::Client.new(endpoint: 'https://api.github.com')
github_client.get '/emojis'

#=> { "+1": "https://assets-cdn.github.com/images/icons/emoji/unicode/1f44d.png?v7",
    "-1": "https://assets-cdn.github.com/images/icons/emoji/unicode/1f44e.png?v7",
    ... }


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

def initialize(endpoint:, tries: 2, headers: {}, username: nil, password: nil,
               user_agent: 'Network Client')
  @uri = URI.parse(endpoint)
  @tries = tries

  set_http_client
  define_error_strategies
  set_default_headers(headers)
  set_basic_auth(username, password)
  set_bearer_auth
  set_token_auth
  set_logger
  set_user_agent(headers['User-Agent'] || user_agent)
end

Instance Attribute Details

#auth_token_headerObject (readonly)

Returns the value of attribute auth_token_header.



23
24
25
# File 'lib/network/client.rb', line 23

def auth_token_header
  @auth_token_header
end

#bearer_tokenObject (readonly)

Returns the value of attribute bearer_token.



23
24
25
# File 'lib/network/client.rb', line 23

def bearer_token
  @bearer_token
end

#default_headersObject (readonly)

Returns the value of attribute default_headers.



23
24
25
# File 'lib/network/client.rb', line 23

def default_headers
  @default_headers
end

#errors_to_propagateObject

Error list for stop and propagate strategy. Takes priority over @errors_to_recover. Do not assign ancestor error classes here that prevent retry for descendant ones.



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

def errors_to_propagate
  @errors_to_propagate
end

#errors_to_recoverObject

Error list for retrying strategy. Initially contains common errors encountered usually in net calls.



28
29
30
# File 'lib/network/client.rb', line 28

def errors_to_recover
  @errors_to_recover
end

#loggerObject (readonly)

Returns the value of attribute logger.



23
24
25
# File 'lib/network/client.rb', line 23

def logger
  @logger
end

#passwordObject (readonly)

Returns the value of attribute password.



23
24
25
# File 'lib/network/client.rb', line 23

def password
  @password
end

#triesObject (readonly)

Returns the value of attribute tries.



23
24
25
# File 'lib/network/client.rb', line 23

def tries
  @tries
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



23
24
25
# File 'lib/network/client.rb', line 23

def user_agent
  @user_agent
end

#usernameObject (readonly)

Returns the value of attribute username.



23
24
25
# File 'lib/network/client.rb', line 23

def username
  @username
end

Instance Method Details

#delete(path, params: {}, headers: {}) ⇒ Object

Perform a delete request on the targeted client endpoint.

Parameters:

[path] string path on client's target host. [params] hash request parameters to json encoded in request body. [headers] hash set of http request headers.

Returns:

http response data contained in Response struct.



146
147
148
# File 'lib/network/client.rb', line 146

def delete(path, params: {}, headers: {})
  request_json :delete, path, params, headers
end

#get(path, params: {}, headers: {}) ⇒ Object

Perform a get request on the targeted client endpoint.

Parameters:

[path] string path on client's target host. [params] request parameters to be url encoded. Can be hash or pair of values array. [headers] hash set of http request headers.

Returns:

http response data contained in Response struct.



86
87
88
# File 'lib/network/client.rb', line 86

def get(path, params: {}, headers: {})
  request_json :get, path, params, headers
end

#get_html(path, params: {}, headers: {}) ⇒ Object

Raises:

  • (NotImplementedError)


150
151
152
# File 'lib/network/client.rb', line 150

def get_html(path, params: {}, headers: {})
  raise NotImplementedError
end

#patch(path, params: {}, headers: {}) ⇒ Object

Perform a patch request on the targeted client endpoint.

Parameters:

[path] string path on client's target host. [params] hash request parameters to json encoded in request body. [headers] hash set of http request headers.

Returns:

http response data contained in Response struct.



116
117
118
# File 'lib/network/client.rb', line 116

def patch(path, params: {}, headers: {})
  request_json :patch, path, params, headers
end

#post(path, params: {}, headers: {}) ⇒ Object

Perform a post request on the targeted client endpoint.

Parameters:

[path] string path on client's target host. [params] hash request parameters to json encoded in request body. [headers] hash set of http request headers.

Returns:

http response data contained in Response struct.



101
102
103
# File 'lib/network/client.rb', line 101

def post(path, params: {}, headers: {})
  request_json :post, path, params, headers
end

#post_form(path, params: {}, headers: {}) ⇒ Object

Raises:

  • (NotImplementedError)


154
155
156
# File 'lib/network/client.rb', line 154

def post_form(path, params: {}, headers: {})
  raise NotImplementedError
end

#put(path, params: {}, headers: {}) ⇒ Object

Perform a put request on the targeted client endpoint.

Parameters:

[path] string path on client's target host. [params] hash request parameters to json encoded in request body. [headers] hash set of http request headers.

Returns:

http response data cotained in Response strcut.



131
132
133
# File 'lib/network/client.rb', line 131

def put(path, params: {}, headers: {})
  request_json :put, path, params, headers
end

#set_basic_auth(username, password) ⇒ Object



177
178
179
180
# File 'lib/network/client.rb', line 177

def set_basic_auth(username, password)
  @username = username.nil? ? '' : username
  @password = password.nil? ? '' : password
end

#set_bearer_auth(token: '') ⇒ Object

Assigns authentication bearer type token for use in standard HTTP authorization header.

Parameters:

[token] string bearer token value.

Returns:

[@bearer_token] string the newly assigned @bearer_token value.



191
192
193
# File 'lib/network/client.rb', line 191

def set_bearer_auth(token: '')
  @bearer_token = token
end

#set_loggerObject

Sets the client logger object. Execution is yielded to passed block to set, customize, and returning a logger instance.

Returns:

logger instance variable.



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/network/client.rb', line 165

def set_logger
  @logger = if block_given?
    yield
  elsif defined?(Rails)
    Rails.logger
  else
    logger = Logger.new(STDOUT)
    logger.level = Logger::DEBUG
    logger
  end
end

#set_token_auth(header_value: '') ⇒ Object

Assigns custom authentication token for use in standard HTTP authorization header. This takes precedence over Bearer authentication if both are set.

Parameters:

[header_value] string full authorization header value. (e.g. Token token=123).

Returns:

[@auth_token_header] string the newly assigned @auth_token_header value.



205
206
207
# File 'lib/network/client.rb', line 205

def set_token_auth(header_value: '')
  @auth_token_header = header_value
end

#set_user_agent(new_user_agent) ⇒ Object

Assigns a new User-Agent header to be sent in any subsequent request.

Parameters:

[new_user_agent] string the user-agent header value.

Returns:

[@user_agent] string the newly assigned User-Agent header value.



218
219
220
# File 'lib/network/client.rb', line 218

def set_user_agent(new_user_agent)
  @user_agent = @default_headers['User-Agent'] = new_user_agent
end