simple_http_service 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +17 -17
- data/README.md +100 -16
- data/lib/simple_http_service/client.rb +27 -4
- data/lib/simple_http_service/version.rb +1 -1
- data/lib/simple_http_service.rb +4 -1
- data/simple_http_service.gemspec +3 -4
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48c5a5d1d5a310878535fe40f8760e7e880268aa8b5903c69e6ee8d3514d3f29
|
4
|
+
data.tar.gz: b46618612ffda2302a65c4e1c4a7aa7448d894f2add044ef853e221cd7a029eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 568fd0858274de7ed76426e3d6d64834c1269d2456e1a37a7a89b110515676e2b84489aebf521e09ecee66f85beaa3718076fb288db0e695c2ea18b16eb09dee
|
7
|
+
data.tar.gz: ecf2e3fe4c140e06439ac1af14f968c9f31df7d95a4304ad89dae9de910e72fe3e2081d856541c1c22c8f10135065b9175f012f5642bb5f1d6e883d6dabd1385
|
data/Gemfile.lock
CHANGED
@@ -1,39 +1,39 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
simple_http_service (0.1.
|
5
|
-
net-http (~> 0.
|
4
|
+
simple_http_service (0.1.2)
|
5
|
+
net-http (~> 0.4.1)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
diff-lcs (1.5.
|
11
|
-
net-http (0.
|
10
|
+
diff-lcs (1.5.1)
|
11
|
+
net-http (0.4.1)
|
12
12
|
uri
|
13
13
|
rake (10.5.0)
|
14
|
-
rspec (3.
|
15
|
-
rspec-core (~> 3.
|
16
|
-
rspec-expectations (~> 3.
|
17
|
-
rspec-mocks (~> 3.
|
18
|
-
rspec-core (3.
|
19
|
-
rspec-support (~> 3.
|
20
|
-
rspec-expectations (3.
|
14
|
+
rspec (3.13.0)
|
15
|
+
rspec-core (~> 3.13.0)
|
16
|
+
rspec-expectations (~> 3.13.0)
|
17
|
+
rspec-mocks (~> 3.13.0)
|
18
|
+
rspec-core (3.13.0)
|
19
|
+
rspec-support (~> 3.13.0)
|
20
|
+
rspec-expectations (3.13.0)
|
21
21
|
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
-
rspec-support (~> 3.
|
23
|
-
rspec-mocks (3.
|
22
|
+
rspec-support (~> 3.13.0)
|
23
|
+
rspec-mocks (3.13.1)
|
24
24
|
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
-
rspec-support (~> 3.
|
26
|
-
rspec-support (3.
|
25
|
+
rspec-support (~> 3.13.0)
|
26
|
+
rspec-support (3.13.1)
|
27
27
|
uri (0.12.0)
|
28
28
|
|
29
29
|
PLATFORMS
|
30
30
|
ruby
|
31
31
|
|
32
32
|
DEPENDENCIES
|
33
|
-
bundler (~>
|
33
|
+
bundler (~> 2.5.10)
|
34
34
|
rake (~> 10.0)
|
35
35
|
rspec (~> 3.0)
|
36
36
|
simple_http_service!
|
37
37
|
|
38
38
|
BUNDLED WITH
|
39
|
-
|
39
|
+
2.5.11
|
data/README.md
CHANGED
@@ -1,38 +1,122 @@
|
|
1
|
-
#
|
1
|
+
# SimpleHttpService
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
SimpleHttpService is a simple Ruby library to make HTTP requests with customizable options for headers, timeouts, and retries. It provides a convenient way to create and send HTTP requests using a clean and simple interface.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
7
|
Add this line to your application's Gemfile:
|
10
8
|
|
11
|
-
|
12
|
-
gem 'simple_http_service'
|
13
|
-
```
|
9
|
+
gem 'simple_http_service'
|
14
10
|
|
15
11
|
And then execute:
|
16
12
|
|
17
|
-
|
13
|
+
bundle install
|
18
14
|
|
19
15
|
Or install it yourself as:
|
20
16
|
|
21
|
-
|
17
|
+
gem install simple_http_service
|
22
18
|
|
23
19
|
## Usage
|
24
20
|
|
25
|
-
|
21
|
+
### Creating a Client
|
22
|
+
You can create a new HTTP client using the SimpleHttpService.new method, which initializes an instance of SimpleHttpService::Client.
|
26
23
|
|
27
|
-
|
24
|
+
```ruby
|
25
|
+
require 'simple_http_service'
|
26
|
+
|
27
|
+
client = SimpleHttpService.new(
|
28
|
+
url: 'https://api.example.com/endpoint',
|
29
|
+
http_method: :get,
|
30
|
+
headers: {
|
31
|
+
accept: 'application/json',
|
32
|
+
authorization: 'Bearer your_token',
|
33
|
+
content_type: 'application/json',
|
34
|
+
cookie: 'your cookie'
|
35
|
+
},
|
36
|
+
open_timeout: 5,
|
37
|
+
read_timeout: 10,
|
38
|
+
write_timeout: 5,
|
39
|
+
retry_count: 3
|
40
|
+
)
|
41
|
+
```
|
42
|
+
### Making a Request
|
43
|
+
After creating the client, you can call the call method to make the HTTP request:
|
44
|
+
```ruby
|
45
|
+
response = client.call
|
46
|
+
puts response.body
|
47
|
+
```
|
28
48
|
|
29
|
-
|
49
|
+
### Options
|
50
|
+
- `url` (required): The URL for the HTTP request.
|
51
|
+
- `http_method` (required): The HTTP method to use (:get, :post, :put).
|
52
|
+
- `headers`: A hash of headers to include in the request.
|
53
|
+
- `open_timeout`: Timeout for opening the connection (default is false).
|
54
|
+
- `read_timeout`: Timeout for reading the response (default is false).
|
55
|
+
- `write_timeout`: Timeout for writing the request (default is false).
|
56
|
+
- `retry_count`: The number of times to retry the request in case of failure.
|
57
|
+
- `request_body`: The body of the request (used for POST and PUT requests).
|
30
58
|
|
31
|
-
|
59
|
+
### Example
|
60
|
+
Here's a complete example of using `SimpleHttpService` to make a `GET` request:
|
32
61
|
|
33
|
-
|
62
|
+
```ruby
|
63
|
+
require 'simple_http_service'
|
64
|
+
|
65
|
+
client = SimpleHttpService.new(
|
66
|
+
url: 'https://api.example.com/endpoint',
|
67
|
+
http_method: :get,
|
68
|
+
headers: {
|
69
|
+
accept: 'application/json',
|
70
|
+
authorization: 'Bearer your_token'
|
71
|
+
},
|
72
|
+
open_timeout: 5,
|
73
|
+
read_timeout: 10
|
74
|
+
)
|
75
|
+
|
76
|
+
response = client.call
|
77
|
+
puts response.body
|
78
|
+
```
|
79
|
+
For a POST request with a request body:
|
80
|
+
```ruby
|
81
|
+
require 'simple_http_service'
|
82
|
+
|
83
|
+
client = SimpleHttpService.new(
|
84
|
+
url: 'https://api.example.com/endpoint',
|
85
|
+
http_method: :post,
|
86
|
+
headers: {
|
87
|
+
accept: 'application/json',
|
88
|
+
authorization: 'Bearer your_token',
|
89
|
+
content_type: 'application/json'
|
90
|
+
},
|
91
|
+
request_body: { key: 'value' }.to_json,
|
92
|
+
open_timeout: 5,
|
93
|
+
read_timeout: 10,
|
94
|
+
write_timeout: 5
|
95
|
+
)
|
96
|
+
|
97
|
+
response = client.call
|
98
|
+
puts response.body
|
99
|
+
```
|
100
|
+
|
101
|
+
### Error Handling
|
102
|
+
The library defines a custom error class SimpleHttpService::Error that you can use to handle exceptions:
|
103
|
+
```ruby
|
104
|
+
begin
|
105
|
+
response = client.call
|
106
|
+
puts response.body
|
107
|
+
rescue SimpleHttpService::Error => e
|
108
|
+
puts "An error occurred: #{e.message}"
|
109
|
+
end
|
110
|
+
```
|
34
111
|
|
35
|
-
|
112
|
+
|
113
|
+
## Development
|
114
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
115
|
+
|
116
|
+
To install this gem onto your local machine, run `bundle install`. To release a new version, update the version number in `version.rb`.
|
117
|
+
|
118
|
+
## Contributing
|
119
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/gklsan/simple_http_service.
|
36
120
|
|
37
121
|
## License
|
38
122
|
|
@@ -40,4 +124,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
40
124
|
|
41
125
|
## Code of Conduct
|
42
126
|
|
43
|
-
Everyone interacting in the
|
127
|
+
Everyone interacting in the SimpleHttpService project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
|
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
module SimpleHttpService
|
3
3
|
class Client
|
4
|
-
attr_accessor :uri, :headers, :http_method, :
|
4
|
+
attr_accessor :uri, :headers, :http_method, :open_timeout, :read_timeout, :write_timeout,
|
5
|
+
:retry_count, :request_body
|
5
6
|
|
6
7
|
def initialize(opts)
|
7
8
|
raise 'URL must be present' unless opts[:url]
|
@@ -10,12 +11,17 @@ module SimpleHttpService
|
|
10
11
|
@uri = URI(opts[:url])
|
11
12
|
@http_method = opts[:http_method]
|
12
13
|
@headers = opts[:headers] || {}
|
13
|
-
@
|
14
|
+
@request_body = opts[:request_body]
|
15
|
+
@open_timeout = opts[:open_timeout] || false
|
16
|
+
@read_timeout = opts[:read_timeout] || false
|
17
|
+
@write_timeout = opts[:write_timeout] || false
|
18
|
+
@retry_count = opts[:retry_count]
|
14
19
|
end
|
15
20
|
|
16
21
|
def call
|
17
22
|
enable_ssl
|
18
23
|
set_headers
|
24
|
+
set_timeout
|
19
25
|
http.request(request)
|
20
26
|
end
|
21
27
|
|
@@ -23,13 +29,20 @@ module SimpleHttpService
|
|
23
29
|
|
24
30
|
def set_headers
|
25
31
|
request["Accept"] = headers[:accept] if headers[:accept]
|
26
|
-
request["Authorization"] = headers[:
|
32
|
+
request["Authorization"] = headers[:authorization] if headers[:authorization]
|
27
33
|
request["Content-Type"] = headers[:content_type] if headers[:content_type]
|
28
34
|
request["Cookie"] = headers[:cookie] if headers[:cookie]
|
29
35
|
end
|
30
36
|
|
31
37
|
def request
|
32
|
-
@request ||=
|
38
|
+
@request ||= case http_method
|
39
|
+
when :post
|
40
|
+
Net::HTTP::Post.new(uri)
|
41
|
+
when :put
|
42
|
+
Net::HTTP::Put.new(uri)
|
43
|
+
else
|
44
|
+
Net::HTTP::Get.new(uri)
|
45
|
+
end
|
33
46
|
end
|
34
47
|
|
35
48
|
def enable_ssl
|
@@ -39,6 +52,16 @@ module SimpleHttpService
|
|
39
52
|
http.ssl_version = :TLSv1_2
|
40
53
|
end
|
41
54
|
|
55
|
+
def set_timeout
|
56
|
+
http.open_timeout = open_timeout if open_timeout
|
57
|
+
http.read_timeout = read_timeout if read_timeout
|
58
|
+
http.write_timeout = write_timeout if write_timeout
|
59
|
+
end
|
60
|
+
|
61
|
+
def set_request_params
|
62
|
+
request.body = request_body if request_body.present?
|
63
|
+
end
|
64
|
+
|
42
65
|
def http
|
43
66
|
@http ||= Net::HTTP.new(uri.host, uri.port)
|
44
67
|
end
|
data/lib/simple_http_service.rb
CHANGED
data/simple_http_service.gemspec
CHANGED
@@ -6,9 +6,8 @@ require "simple_http_service/version"
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "simple_http_service"
|
8
8
|
spec.version = SimpleHttpService::VERSION
|
9
|
-
spec.authors = ["Gokul"]
|
9
|
+
spec.authors = ["Gokul (gklsan)"]
|
10
10
|
spec.email = ["pgokulmca@gmail.com"]
|
11
|
-
|
12
11
|
spec.summary = 'Welcome to SimpleHttpService gem'
|
13
12
|
spec.description = 'Welcome to SimpleHttpService gem'
|
14
13
|
spec.homepage = "https://github.com/gklsan/simple_http_service"
|
@@ -36,8 +35,8 @@ Gem::Specification.new do |spec|
|
|
36
35
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
36
|
spec.require_paths = ["lib"]
|
38
37
|
|
39
|
-
spec.add_dependency "net-http", "~> 0.
|
40
|
-
spec.add_development_dependency "bundler", "~>
|
38
|
+
spec.add_dependency "net-http", "~> 0.4.1"
|
39
|
+
spec.add_development_dependency "bundler", "~> 2.5.10"
|
41
40
|
spec.add_development_dependency "rake", "~> 10.0"
|
42
41
|
spec.add_development_dependency "rspec", "~> 3.0"
|
43
42
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_http_service
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Gokul
|
7
|
+
- Gokul (gklsan)
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: net-http
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 0.4.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: 0.4.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 2.5.10
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 2.5.10
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0'
|
112
112
|
requirements: []
|
113
|
-
rubygems_version: 3.
|
113
|
+
rubygems_version: 3.5.11
|
114
114
|
signing_key:
|
115
115
|
specification_version: 4
|
116
116
|
summary: Welcome to SimpleHttpService gem
|