net-http-ext 0.0.1 → 1.0.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.md +140 -2
- data/lib/net/http/ext.rb +3 -0
- data/lib/net/http/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c90112ab31ec1229abfa8b159437f7d797d1f817dcd6ebb8670f69f24a4ef2f2
|
4
|
+
data.tar.gz: 8744a8146cf8f185e217469ee8dd02ac93d3fe6c67e72e11105d0d145de5f3f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad130f518516b5a3df732cbc06d07645474c4699fafd323d14f3688f4a6d6cb34f2242001e05fbd61a573bab95792613d8da46dfca006047256c4935abd583e7
|
7
|
+
data.tar.gz: 89e4e426d5b30b5c1baaf5f0c992ca0aad8c31b6d42503904af7c0a8a2e1a1540ad0078efbabe3a927704c5e2e92705ab76ab90eeafe25e1b2dad2dc06212ec8
|
data/README.md
CHANGED
@@ -1,5 +1,143 @@
|
|
1
1
|
# net-http-ext
|
2
2
|
|
3
|
-
|
3
|
+
[](https://github.com/GrantBirki/net-http-ext/actions/workflows/test.yml)
|
4
|
+
[](https://github.com/GrantBirki/net-http-ext/actions/workflows/lint.yml)
|
5
|
+
[](https://github.com/GrantBirki/net-http-ext/actions/workflows/acceptance.yml)
|
6
|
+
[](https://github.com/GrantBirki/net-http-ext/actions/workflows/build.yml)
|
7
|
+
[](https://github.com/GrantBirki/net-http-ext/actions/workflows/release.yml)
|
4
8
|
|
5
|
-
|
9
|
+
Safe defaults, persistent connections, thread safety, and basic logging for Ruby's Net::HTTP library.
|
10
|
+
|
11
|
+
A simple wrapper around [`net/http/persistent`](https://github.com/drbrain/net-http-persistent) in pure Ruby.
|
12
|
+
|
13
|
+
## About 💡
|
14
|
+
|
15
|
+
A very simple wrapper around the `net/http/persistent` library (which is a wrapper around `net/http`) that provides a few extra features and safe defaults. It is designed to be a *lite*-batteries-included version of `net/http/persistent` that is easy to use and configure.
|
16
|
+
|
17
|
+
Should you need anything more complex, check out [`faraday`](https://github.com/lostisland/faraday) + [`araday-net_http_persistent`](https://github.com/lostisland/faraday-net_http_persistent).
|
18
|
+
|
19
|
+
### Benefits ⭐
|
20
|
+
|
21
|
+
1. Reuse connections for multiple requests
|
22
|
+
2. Thread safe
|
23
|
+
3. Secure defaults
|
24
|
+
4. Basic logging
|
25
|
+
5. Automatically rebuild the connection if it is closed by the server
|
26
|
+
6. Automatically retry requests on connection failures if `max_retries` is set to a value >1
|
27
|
+
7. Easy to use and configure
|
28
|
+
|
29
|
+
## Installation 💎
|
30
|
+
|
31
|
+
You can download this Gem from [GitHub Packages](https://github.com/GrantBirki/net-http-ext/pkgs/rubygems/net-http-ext) or [RubyGems](https://rubygems.org/gems/net-http-ext)
|
32
|
+
|
33
|
+
Via a Gemfile:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
source "https://rubygems.org"
|
37
|
+
|
38
|
+
gem "net-http-ext", "~> X.X.X" # Replace X.X.X with the latest version
|
39
|
+
```
|
40
|
+
|
41
|
+
Via the CLI:
|
42
|
+
|
43
|
+
```bash
|
44
|
+
gem install net-http-ext
|
45
|
+
```
|
46
|
+
|
47
|
+
## Usage 💻
|
48
|
+
|
49
|
+
## Basic Usage
|
50
|
+
|
51
|
+
Here is an example using the library in its most basic form:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
require "net/http/ext"
|
55
|
+
|
56
|
+
# initialize the client
|
57
|
+
client = Net::HTTP::Ext.new("httpbin.org")
|
58
|
+
|
59
|
+
# make a request
|
60
|
+
response = client.get("/")
|
61
|
+
|
62
|
+
puts response.code
|
63
|
+
puts response.body
|
64
|
+
```
|
65
|
+
|
66
|
+
### Extended Usage
|
67
|
+
|
68
|
+
Here is a more detailed/full usage example:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
require "net/http/ext"
|
72
|
+
|
73
|
+
# Initialize the client
|
74
|
+
client = Net::HTTP::Ext.new("httpbin.org")
|
75
|
+
|
76
|
+
response = client.get("/")
|
77
|
+
puts response.code
|
78
|
+
|
79
|
+
# GET request with headers and query parameters
|
80
|
+
response = client.get("/get?param1=value1¶m2=value2",
|
81
|
+
headers: { "Authorization" => "Bearer token123" }
|
82
|
+
)
|
83
|
+
puts response.body
|
84
|
+
|
85
|
+
# GET request using query parameters as a qwarg
|
86
|
+
response = client.get("/get",
|
87
|
+
params: { param1: "value1kwarg", param2: "value2kwarg" },
|
88
|
+
)
|
89
|
+
puts response.body
|
90
|
+
|
91
|
+
# POST request with JSON payload
|
92
|
+
response = client.post("/post",
|
93
|
+
payload: { name: "John Doe", email: "john@example.com" }
|
94
|
+
)
|
95
|
+
puts response.body
|
96
|
+
|
97
|
+
# POST request with JSON payload and custom content type
|
98
|
+
response = client.post("/post",
|
99
|
+
headers: { "Content-Type" => "application/json+custom" },
|
100
|
+
payload: { name: "John Doe", email: "john@example.com" }
|
101
|
+
)
|
102
|
+
puts response.body
|
103
|
+
|
104
|
+
# Custom timeouts
|
105
|
+
client = Net::HTTP::Ext.new("https://httpbin.org",
|
106
|
+
open_timeout: 5, # connection establishment timeout (seconds)
|
107
|
+
read_timeout: 10, # response read timeout (seconds)
|
108
|
+
idle_timeout: 30, # how long to keep idle connections open (seconds)
|
109
|
+
request_timeout: 15 # overall request timeout (seconds)
|
110
|
+
)
|
111
|
+
response = client.get("/delay/2") # Simulate a delay of 2 seconds
|
112
|
+
puts response.code
|
113
|
+
|
114
|
+
# Default headers on all requests
|
115
|
+
client = Net::HTTP::Ext.new("https://httpbin.org",
|
116
|
+
default_headers: {
|
117
|
+
"User-Agent" => "MyApp/1.0"
|
118
|
+
}
|
119
|
+
)
|
120
|
+
response = client.get("/headers")
|
121
|
+
puts response.body
|
122
|
+
|
123
|
+
# Make a get request and automatically parse it as JSON
|
124
|
+
response = client.get_json("/get?param1=value1¶m2=value2")
|
125
|
+
puts response["args"]["param1"] # => "value1"
|
126
|
+
```
|
127
|
+
|
128
|
+
> See the full source code at [`lib/net/http/ext.rb`](lib/net/http/ext.rb) for more details on the available options and methods.
|
129
|
+
|
130
|
+
## Contributing 🤝
|
131
|
+
|
132
|
+
1. Fork the repository
|
133
|
+
2. Bootstrap the project with `script/bootstrap`
|
134
|
+
3. Create a new branch (`git checkout -b feature/your-feature`)
|
135
|
+
4. Write your code
|
136
|
+
5. Run the tests (`script/test`)
|
137
|
+
6. Run the acceptance tests (`script/acceptance`)
|
138
|
+
7. Run the linter (`script/lint -A`)
|
139
|
+
8. Commit your changes (`git commit -m "Add some feature"`)
|
140
|
+
9. Push to the branch (`git push origin feature/your-feature`)
|
141
|
+
10. Create a new Pull Request
|
142
|
+
11. Wait for an approval and passing ci tests
|
143
|
+
12. 🎉
|
data/lib/net/http/ext.rb
CHANGED
@@ -94,6 +94,9 @@ class Net::HTTP::Ext
|
|
94
94
|
ssl_cert_file: nil,
|
95
95
|
**options
|
96
96
|
)
|
97
|
+
# Add "https://" if the endpoint does not include a scheme
|
98
|
+
endpoint = "https://#{endpoint}" unless endpoint.start_with?("http://", "https://")
|
99
|
+
|
97
100
|
@uri = URI.parse(endpoint)
|
98
101
|
@name = name || ENV.fetch("HTTP_CLIENT_NAME", "http-client")
|
99
102
|
@request_timeout = request_timeout
|
data/lib/net/http/version.rb
CHANGED