Class: Network::Client
- Inherits:
-
Object
- Object
- Network::Client
- 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
-
#auth_token_header ⇒ Object
readonly
Returns the value of attribute auth_token_header.
-
#bearer_token ⇒ Object
readonly
Returns the value of attribute bearer_token.
-
#default_headers ⇒ Object
readonly
Returns the value of attribute default_headers.
-
#errors_to_propagate ⇒ Object
Error list for stop and propagate strategy.
-
#errors_to_recover ⇒ Object
Error list for retrying strategy.
-
#http ⇒ Object
Gives access to underlying NET::HTTP client instance.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#tries ⇒ Object
readonly
Returns the value of attribute tries.
-
#user_agent ⇒ Object
readonly
Returns the value of attribute user_agent.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
-
#delete(path, params: {}, headers: {}) ⇒ Object
Perform a delete request on the targeted client
endpoint
. -
#get(path, params: {}, headers: {}) ⇒ Object
Perform a get request on the targeted client
endpoint
. - #get_html(path, params: {}, headers: {}) ⇒ Object
-
#initialize(endpoint:, tries: 2, headers: {}, username: nil, password: nil, user_agent: 'Network Client') ⇒ Client
constructor
Construct and prepare client for requests targeting
endpoint
. -
#patch(path, params: {}, headers: {}) ⇒ Object
Perform a patch request on the targeted client
endpoint
. -
#post(path, params: {}, headers: {}) ⇒ Object
Perform a post request on the targeted client
endpoint
. - #post_form(path, params: {}, headers: {}) ⇒ Object
-
#put(path, params: {}, headers: {}) ⇒ Object
Perform a put request on the targeted client
endpoint
. - #set_basic_auth(username, password) ⇒ Object
-
#set_bearer_auth(token: '') ⇒ Object
Assigns authentication bearer type token for use in standard HTTP authorization header.
-
#set_logger ⇒ Object
Sets the client logger object.
-
#set_token_auth(header_value: '') ⇒ Object
Assigns custom authentication token for use in standard HTTP authorization header.
-
#set_user_agent(new_user_agent) ⇒ Object
Assigns a new
User-Agent
header to be sent in any subsequent request.
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",
... }
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/network/client.rb', line 63 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_header ⇒ Object (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_token ⇒ Object (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_headers ⇒ Object (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_propagate ⇒ Object
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_recover ⇒ Object
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 |
#http ⇒ Object
Gives access to underlying NET::HTTP client instance.
36 37 38 |
# File 'lib/network/client.rb', line 36 def http @http end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
23 24 25 |
# File 'lib/network/client.rb', line 23 def logger @logger end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
23 24 25 |
# File 'lib/network/client.rb', line 23 def password @password end |
#tries ⇒ Object (readonly)
Returns the value of attribute tries.
23 24 25 |
# File 'lib/network/client.rb', line 23 def tries @tries end |
#user_agent ⇒ Object (readonly)
Returns the value of attribute user_agent.
23 24 25 |
# File 'lib/network/client.rb', line 23 def user_agent @user_agent end |
#username ⇒ Object (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.
149 150 151 |
# File 'lib/network/client.rb', line 149 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 valuesarray
. - headers
-
hash
set of http request headers.
Returns:
http response data contained in Response
struct.
89 90 91 |
# File 'lib/network/client.rb', line 89 def get(path, params: {}, headers: {}) request_json :get, path, params, headers end |
#get_html(path, params: {}, headers: {}) ⇒ Object
153 154 155 |
# File 'lib/network/client.rb', line 153 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.
119 120 121 |
# File 'lib/network/client.rb', line 119 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.
104 105 106 |
# File 'lib/network/client.rb', line 104 def post(path, params: {}, headers: {}) request_json :post, path, params, headers end |
#post_form(path, params: {}, headers: {}) ⇒ Object
157 158 159 |
# File 'lib/network/client.rb', line 157 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.
134 135 136 |
# File 'lib/network/client.rb', line 134 def put(path, params: {}, headers: {}) request_json :put, path, params, headers end |
#set_basic_auth(username, password) ⇒ Object
180 181 182 183 |
# File 'lib/network/client.rb', line 180 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.
194 195 196 |
# File 'lib/network/client.rb', line 194 def set_bearer_auth(token: '') @bearer_token = token end |
#set_logger ⇒ Object
Sets the client logger object. Execution is yielded to passed block
to set, customize, and returning a logger instance.
Returns:
logger
instance variable.
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/network/client.rb', line 168 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.
208 209 210 |
# File 'lib/network/client.rb', line 208 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 assignedUser-Agent
header value.
221 222 223 |
# File 'lib/network/client.rb', line 221 def set_user_agent(new_user_agent) @user_agent = @default_headers['User-Agent'] = new_user_agent end |