Network Client
Installation
Add this line to your application's Gemfile:
gem 'network-client'
And then execute:
$ bundle
Or install the gem directly:
$ gem install network-client
Usage
Making JSON requests
Given this client set up:
require "network-client"
client = Network::Client.new(endpoint: 'https://jsonplaceholder.typicode.com')
We can perform the following requests:
client.get '/todos/10'
client.post '/todos', params: { title: 'foo bar', completed: 'false', userId: 1 }.to_json
client.patch '/todos/10', params: { title: 'new title' }.to_json
client.put '/todos/43', params: { completed: false }.to_json
client.delete '/todos/25'
Returned Response
As appears in previous examples, the returned value of each successful request is a Response
struct.
It holds the response's HTTP code and body parsed as JSON.
response = client.get '/posts/30'
response.code
response.body
Since this is mainly JSON web client, Accept
and Content-Type
headers are set to json by default.
You can override them and set extra headers during initialization by providing headers:
argument:
headers = { 'X-SPECIAL-KEY' => '123456' }
client = Network::Client.new(endpoint: 'https://api.example.com', headers: headers)
Or on request basis with the headers:
argument too:
client.get 'posts/', headers: { 'X-SPECIAL-KEY' => '123456' }
HTTP Authentication
- Basic:
client = Network::Client.new(endpoint: 'https://api.example.com',
username: 'ABC',
password: '999')
client.username
client.password
client.set_basic_auth('John Doe', '112233')
client.username
client.password
- OAuth Bearer:
client.set_bearer_auth(token: 'e08f7739c3abb78c')
client.bearer_token
- Token Based:
client.set_token_auth(header_value: 'Token token=sec_key_aZcNRzoCMpmdMEP4OEeDUQ==')
client.auth_token_header
Customizing User Agent
You can set the user agent header during initialization:
client = Network::Client.new(endpoint: 'https://maps.googleapis.com', user_agent: 'App Service')
client.user_agent
Or later on via #set_user_agent
method:
client.set_user_agent('Gateway Server')
client.user_agent
The default user agent is Network Client
.
Retry and Error Handling
Set the tries:
named argument to define the number of tries when request fails with one of the retryable errors.
client = Network::Client.new(endpoint: 'https://api.foursquare.com', tries: 3)
client.tries
The default #tries
is 2.
To retrieve or extend the list of triable errors through #errors_to_recover
:
client.errors_to_recover
client.errors_to_recover << Net::HTTPRequestTimeOut
The list of errors_to_propagate
takes precedence over errors_to_recover
, and they are not retried.
You can retrieve them for rescue in your application layer, and extend them too.
client.errors_to_propagate
client.errors_to_propagate << Net::HTTPNotAcceptable
Be careful not to add ancestor error class (higher in the inheritance chain) as it will prevent any of it's descendant classes from getting retried. Unless this is an intended behavior, of course.
Logger
When Rails
is in scope, it's logger will be used by default.
If not, then it defaults to a fallback logger that writes to STDOUT
.
Additionally, you can override with your custom logger by supplying block to #set_logger
like so:
client = Network::Client.new(endpoint: 'https://api.foursquare.com')
client.set_logger { Logger.new(STDERR) }
client.logger
Documentation
For more details, please refer to the API documentation.
Contributing
Bug reports and pull requests are very much appreciated at Github.
- Fork The repository.
- Create a branch with the fix or feature name.
- Make your changes (with test or README changes/additions if applicable).
- Push changes to the created branch.
- Create an Pull Request.
- That's it!
License
MIT.