chimera_http_client 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.markdown +22 -5
- data/lib/chimera_http_client/connection.rb +2 -2
- data/lib/chimera_http_client/request.rb +1 -0
- data/lib/chimera_http_client/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f53a50a92fd82daebeae2cad2156dd5a0eef366d4aec27d2ecb13fffe8f771f3
|
4
|
+
data.tar.gz: ef515b583ba8cbbd76286e626dfbe34cf06662710c04e441f9d4e926577962cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa715d6646f2f924a67bd0049598156eaacee8b60a81d28858cddf86f8a7e42c2ae50097fd7be824d8d7ff91fd0bc1569713cb014b7e3a1859a634bf9c3d4568
|
7
|
+
data.tar.gz: '069987a5e71950ba299b313a93f9cb6533adfe275bbda057a076614aeb8d053fc1e574a7db588d75dfc36a7611d10260c6b0ebde9fac274474e5ef85603fe356'
|
data/README.markdown
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# ChimeraHttpClient
|
2
2
|
|
3
|
-
When starting to split monolithic apps into smaller services, you need an easy way to access the remote data from the other apps. This chimera_http_client gem should serve as a unified way to access endpoints from other apps. And what works for the internal communication between your own apps, will also work
|
3
|
+
When starting to split monolithic apps into smaller services, you need an easy way to access the remote data from the other apps. This chimera_http_client gem should serve as a unified way to access endpoints from other apps. And what works for the internal communication between your own apps, will also work for external APIs that do not offer a client for simplified access.
|
4
4
|
|
5
5
|
## Dependencies
|
6
6
|
|
7
|
-
The `chimera_http_client` gem is using the _libcurl_ wrapper [**Typhoeus**](https://typhoeus.github.io/). This allows for fast requests, for caching, and for queueing requests to run them in parallel (
|
7
|
+
The `chimera_http_client` gem is using the _libcurl_ wrapper [**Typhoeus**](https://typhoeus.github.io/). This allows for fast requests, for caching, and for queueing requests to run them in parallel (not all features are implemented in the gem yet).
|
8
8
|
|
9
9
|
It does not have any other runtime dependencies.
|
10
10
|
|
@@ -25,7 +25,8 @@ response = connection.get!(endpoint, params: params)
|
|
25
25
|
`base_url: 'http://localhost:3000/v1'`.
|
26
26
|
|
27
27
|
On this connection object, you can call methods like `#get!` or `#post!` with an endpoint and an options hash as parameters, e.g.
|
28
|
-
`connection.get!("users/#{id}")`
|
28
|
+
`connection.get!("users/#{id}")`
|
29
|
+
or
|
29
30
|
`connection.get(['users', id], options: { headers: { ' Accept-Charset' => 'utf-8' })`
|
30
31
|
|
31
32
|
Please take note that _the endpoint can be given as a String, a Symbol, or as an Array._
|
@@ -33,6 +34,8 @@ While they do no harm, there is _no need to pass leading or trailing `/` in endp
|
|
33
34
|
When passing the endpoint as an Array, _it's elements are converted to Strings and concatenated with `/`._
|
34
35
|
On each request _the http-headers can be amended or overwritten_ completely or partially.
|
35
36
|
|
37
|
+
### Basic auth
|
38
|
+
|
36
39
|
In case you need to use an API that is protected by **basic_auth** just pass the credentials in the options hash:
|
37
40
|
`options: { username: 'admin', password: 'secret' }`
|
38
41
|
|
@@ -46,6 +49,12 @@ If you want to use a different timeout, you can pass the key `timeout` when init
|
|
46
49
|
|
47
50
|
By default no logging is happening. If you need request logging, you can pass your custom Logger to the key `logger` when initializing the `Connection`. It will write to `logger.info` when starting and when completing a request.
|
48
51
|
|
52
|
+
### Caching responses
|
53
|
+
|
54
|
+
To cache all the reponses of a connection, just pass the optional parameter `cache` to its initializer. You can also overwrite the connection's cache configuration by passing the parameter `cache` to any `get` call.
|
55
|
+
|
56
|
+
Read more about how to use it: https://github.com/typhoeus/typhoeus#caching
|
57
|
+
|
49
58
|
### Example usage
|
50
59
|
|
51
60
|
To use the gem, it is recommended to write wrapper classes for the endpoints used. While it would be possible to use the `get, get!, post, post!, put, put!, patch, patch!, delete, delete!` or also the bare `request.run` methods directly, wrapper classes will unify the usage pattern and be very convenient to use by veterans and newcomers to the team. A wrapper class could look like this:
|
@@ -58,6 +67,8 @@ class Users
|
|
58
67
|
@base_url = base_url
|
59
68
|
end
|
60
69
|
|
70
|
+
# GET one user by id and instantiate a User
|
71
|
+
#
|
61
72
|
def find(id:)
|
62
73
|
response = connection.get!(['users', id])
|
63
74
|
|
@@ -68,6 +79,8 @@ class Users
|
|
68
79
|
# handle / log / raise error
|
69
80
|
end
|
70
81
|
|
82
|
+
# GET a list of users and instantiate an Array of Users
|
83
|
+
#
|
71
84
|
def all(filter: nil, page: nil)
|
72
85
|
params = {}
|
73
86
|
params[:filter] = filter
|
@@ -82,6 +95,8 @@ class Users
|
|
82
95
|
# handle / log / raise error
|
83
96
|
end
|
84
97
|
|
98
|
+
# CREATE a new user by sending attributes in a JSON body and instantiate the new User
|
99
|
+
#
|
85
100
|
def create(body:)
|
86
101
|
response = connection.post!('users', body: body.to_json) # body.to_json (!!)
|
87
102
|
|
@@ -120,7 +135,9 @@ Usually it does not have to be used directly. It is the class that executes the
|
|
120
135
|
|
121
136
|
The `body` which it receives from the `Connection` class has to be in the in the (serialized) form in which the endpoint expects it. Usually this means you have to pass a JSON string to the `body` (it will **not** be serialized automatically).
|
122
137
|
|
123
|
-
|
138
|
+
> Upcoming feature:
|
139
|
+
>
|
140
|
+
> It will be expanded by a `.queue` method, that will queue (sic) calls and run them in parallel and not run every call directly, like the `.run` method does.
|
124
141
|
|
125
142
|
## The Response class
|
126
143
|
|
@@ -167,7 +184,7 @@ The error classes and their corresponding http error codes:
|
|
167
184
|
|
168
185
|
Add this line to your application's Gemfile:
|
169
186
|
|
170
|
-
gem 'chimera_http_client', '~> 0.
|
187
|
+
gem 'chimera_http_client', '~> 0.3'
|
171
188
|
|
172
189
|
And then execute:
|
173
190
|
|
@@ -5,7 +5,7 @@ module ChimeraHttpClient
|
|
5
5
|
class Connection
|
6
6
|
USER_AGENT = "ChimeraHttpClient (by mediafinger)".freeze
|
7
7
|
|
8
|
-
def initialize(base_url:, logger: nil, timeout: nil, user_agent: USER_AGENT, verbose: false)
|
8
|
+
def initialize(base_url:, logger: nil, timeout: nil, user_agent: USER_AGENT, verbose: false, cache: nil)
|
9
9
|
fail(ChimeraHttpClient::ParameterMissingError, "base_url expected, but not given") if base_url.nil?
|
10
10
|
|
11
11
|
@base_url = base_url
|
@@ -17,7 +17,7 @@ module ChimeraHttpClient
|
|
17
17
|
Typhoeus::Config.user_agent = user_agent
|
18
18
|
Typhoeus::Config.verbose = verbose
|
19
19
|
Typhoeus::Config.memoize = false
|
20
|
-
|
20
|
+
Typhoeus::Config.cache = cache
|
21
21
|
end
|
22
22
|
|
23
23
|
def request
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chimera_http_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andreas Finger
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: typhoeus
|