httparty 0.22.0 → 0.23.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/.github/workflows/ci.yml +1 -0
- data/Gemfile +1 -0
- data/README.md +16 -16
- data/docs/README.md +2 -0
- data/examples/party_foul_mode.rb +90 -0
- data/lib/httparty/exceptions.rb +30 -3
- data/lib/httparty/request.rb +19 -15
- data/lib/httparty/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f336e1951278fdb4b1480a73cf466ab02159e7d20c0bfcafe26a512ead0e8d8f
|
4
|
+
data.tar.gz: 2ed6bc6ff6afc5055efe6cd21653ab66452b352de1d4a4c84805078daa1704f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31011b129880b9158d59598ac20b4dfbf2bc7a8987360c3468f18ec38d54ad1397a746319bf88d5b6c5dae1f55a2ce2b54d7dc1b7750917635f175c1b5ee6efb
|
7
|
+
data.tar.gz: 101315ce9fff16cd2b67be282c97495ec112cebd2836a65385f268c5ca1de6e803f0ec1589afdf353bab4d94eed1757ec5d3beaa391e107c68dd361d86e83670
|
data/.github/workflows/ci.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[](https://github.com/jnunemaker/httparty/actions/workflows/ci.yml)
|
4
4
|
|
5
|
-
Makes http fun again!
|
5
|
+
Makes http fun again! Ain't no party like a httparty, because a httparty don't stop.
|
6
6
|
|
7
7
|
## Install
|
8
8
|
|
@@ -12,9 +12,8 @@ gem install httparty
|
|
12
12
|
|
13
13
|
## Requirements
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
* You like to party!
|
15
|
+
- Ruby 2.7.0 or higher
|
16
|
+
- You like to party!
|
18
17
|
|
19
18
|
## Examples
|
20
19
|
|
@@ -47,7 +46,8 @@ puts stack_exchange.questions
|
|
47
46
|
puts stack_exchange.users
|
48
47
|
```
|
49
48
|
|
50
|
-
See the [examples directory](http://github.com/jnunemaker/httparty/tree/
|
49
|
+
See the [examples directory](http://github.com/jnunemaker/httparty/tree/main/examples) for even more goodies.
|
50
|
+
|
51
51
|
## Command Line Interface
|
52
52
|
|
53
53
|
httparty also includes the executable `httparty` which can be
|
@@ -63,17 +63,17 @@ httparty "https://api.stackexchange.com/2.2/questions?site=stackoverflow"
|
|
63
63
|
|
64
64
|
## Help and Docs
|
65
65
|
|
66
|
-
|
67
|
-
|
68
|
-
|
66
|
+
- [Docs](https://github.com/jnunemaker/httparty/tree/main/docs)
|
67
|
+
- https://github.com/jnunemaker/httparty/discussions
|
68
|
+
- https://www.rubydoc.info/github/jnunemaker/httparty
|
69
69
|
|
70
70
|
## Contributing
|
71
71
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
72
|
+
- Fork the project.
|
73
|
+
- Run `bundle`
|
74
|
+
- Run `bundle exec rake`
|
75
|
+
- Make your feature addition or bug fix.
|
76
|
+
- Add tests for it. This is important so I don't break it in a future version unintentionally.
|
77
|
+
- Run `bundle exec rake` (No, REALLY :))
|
78
|
+
- Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself in another branch so I can ignore when I pull)
|
79
|
+
- Send me a pull request. Bonus points for topic branches.
|
data/docs/README.md
CHANGED
@@ -36,6 +36,8 @@ You can use this guide to work with SSL certificates.
|
|
36
36
|
|
37
37
|
```ruby
|
38
38
|
# Use this example if you are using a pem file
|
39
|
+
# - cert.pem must contain the content of a PEM file having the private key appended (separated from the cert by a newline \n)
|
40
|
+
# - Use an empty string for the password if the cert is not password protected
|
39
41
|
|
40
42
|
class Client
|
41
43
|
include HTTParty
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
|
3
|
+
class APIClient
|
4
|
+
include HTTParty
|
5
|
+
base_uri 'api.example.com'
|
6
|
+
|
7
|
+
def self.fetch_user(id)
|
8
|
+
begin
|
9
|
+
get("/users/#{id}", foul: true)
|
10
|
+
rescue HTTParty::NetworkError => e
|
11
|
+
handle_network_error(e)
|
12
|
+
rescue HTTParty::ResponseError => e
|
13
|
+
handle_api_error(e)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def self.handle_network_error(error)
|
20
|
+
case error.cause
|
21
|
+
when Errno::ECONNREFUSED
|
22
|
+
{
|
23
|
+
error: :server_down,
|
24
|
+
message: "The API server appears to be down",
|
25
|
+
details: error.message
|
26
|
+
}
|
27
|
+
when Net::OpenTimeout, Timeout::Error
|
28
|
+
{
|
29
|
+
error: :timeout,
|
30
|
+
message: "The request timed out",
|
31
|
+
details: error.message
|
32
|
+
}
|
33
|
+
when SocketError
|
34
|
+
{
|
35
|
+
error: :network_error,
|
36
|
+
message: "Could not connect to the API server",
|
37
|
+
details: error.message
|
38
|
+
}
|
39
|
+
when OpenSSL::SSL::SSLError
|
40
|
+
{
|
41
|
+
error: :ssl_error,
|
42
|
+
message: "SSL certificate verification failed",
|
43
|
+
details: error.message
|
44
|
+
}
|
45
|
+
else
|
46
|
+
{
|
47
|
+
error: :unknown_network_error,
|
48
|
+
message: "An unexpected network error occurred",
|
49
|
+
details: error.message
|
50
|
+
}
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.handle_api_error(error)
|
55
|
+
{
|
56
|
+
error: :api_error,
|
57
|
+
message: "API returned error #{error.response.code}",
|
58
|
+
details: error.response.body
|
59
|
+
}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Example usage:
|
64
|
+
|
65
|
+
# 1. When server is down
|
66
|
+
result = APIClient.fetch_user(123)
|
67
|
+
puts "Server down example:"
|
68
|
+
puts result.inspect
|
69
|
+
puts
|
70
|
+
|
71
|
+
# 2. When request times out
|
72
|
+
result = APIClient.fetch_user(456)
|
73
|
+
puts "Timeout example:"
|
74
|
+
puts result.inspect
|
75
|
+
puts
|
76
|
+
|
77
|
+
# 3. When SSL error occurs
|
78
|
+
result = APIClient.fetch_user(789)
|
79
|
+
puts "SSL error example:"
|
80
|
+
puts result.inspect
|
81
|
+
puts
|
82
|
+
|
83
|
+
# 4. Simple example without a wrapper class
|
84
|
+
begin
|
85
|
+
HTTParty.get('https://api.example.com/users', foul: true)
|
86
|
+
rescue HTTParty::Foul => e
|
87
|
+
puts "Direct usage example:"
|
88
|
+
puts "Error type: #{e.cause.class}"
|
89
|
+
puts "Error message: #{e.message}"
|
90
|
+
end
|
data/lib/httparty/exceptions.rb
CHANGED
@@ -1,18 +1,42 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module HTTParty
|
4
|
+
COMMON_NETWORK_ERRORS = [
|
5
|
+
EOFError,
|
6
|
+
Errno::ECONNABORTED,
|
7
|
+
Errno::ECONNREFUSED,
|
8
|
+
Errno::ECONNRESET,
|
9
|
+
Errno::EHOSTUNREACH,
|
10
|
+
Errno::EINVAL,
|
11
|
+
Errno::ENETUNREACH,
|
12
|
+
Errno::ENOTSOCK,
|
13
|
+
Errno::EPIPE,
|
14
|
+
Errno::ETIMEDOUT,
|
15
|
+
Net::HTTPBadResponse,
|
16
|
+
Net::HTTPHeaderSyntaxError,
|
17
|
+
Net::ProtocolError,
|
18
|
+
Net::ReadTimeout,
|
19
|
+
OpenSSL::SSL::SSLError,
|
20
|
+
SocketError,
|
21
|
+
Timeout::Error # Also covers subclasses like Net::OpenTimeout
|
22
|
+
].freeze
|
23
|
+
|
4
24
|
# @abstract Exceptions raised by HTTParty inherit from Error
|
5
25
|
class Error < StandardError; end
|
6
26
|
|
27
|
+
# @abstract Exceptions raised by HTTParty inherit from this because it is funny
|
28
|
+
# and if you don't like fun you should be using a different library.
|
29
|
+
class Foul < Error; end
|
30
|
+
|
7
31
|
# Exception raised when you attempt to set a non-existent format
|
8
|
-
class UnsupportedFormat <
|
32
|
+
class UnsupportedFormat < Foul; end
|
9
33
|
|
10
34
|
# Exception raised when using a URI scheme other than HTTP or HTTPS
|
11
|
-
class UnsupportedURIScheme <
|
35
|
+
class UnsupportedURIScheme < Foul; end
|
12
36
|
|
13
37
|
# @abstract Exceptions which inherit from ResponseError contain the Net::HTTP
|
14
38
|
# response object accessible via the {#response} method.
|
15
|
-
class ResponseError <
|
39
|
+
class ResponseError < Foul
|
16
40
|
# Returns the response of the last request
|
17
41
|
# @return [Net::HTTPResponse] A subclass of Net::HTTPResponse, e.g.
|
18
42
|
# Net::HTTPOK
|
@@ -32,4 +56,7 @@ module HTTParty
|
|
32
56
|
|
33
57
|
# Exception that is raised when request redirects and location header is present more than once
|
34
58
|
class DuplicateLocationHeader < ResponseError; end
|
59
|
+
|
60
|
+
# Exception that is raised when common network errors occur.
|
61
|
+
class NetworkError < Foul; end
|
35
62
|
end
|
data/lib/httparty/request.rb
CHANGED
@@ -153,24 +153,28 @@ module HTTParty
|
|
153
153
|
chunked_body = nil
|
154
154
|
current_http = http
|
155
155
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
156
|
+
begin
|
157
|
+
self.last_response = current_http.request(@raw_request) do |http_response|
|
158
|
+
if block
|
159
|
+
chunks = []
|
160
|
+
|
161
|
+
http_response.read_body do |fragment|
|
162
|
+
encoded_fragment = encode_text(fragment, http_response['content-type'])
|
163
|
+
chunks << encoded_fragment if !options[:stream_body]
|
164
|
+
block.call ResponseFragment.new(encoded_fragment, http_response, current_http)
|
165
|
+
end
|
166
|
+
|
167
|
+
chunked_body = chunks.join
|
164
168
|
end
|
165
|
-
|
166
|
-
chunked_body = chunks.join
|
167
169
|
end
|
168
|
-
end
|
169
170
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
171
|
+
handle_host_redirection if response_redirects?
|
172
|
+
result = handle_unauthorized
|
173
|
+
result ||= handle_response(chunked_body, &block)
|
174
|
+
result
|
175
|
+
rescue *COMMON_NETWORK_ERRORS => e
|
176
|
+
raise options[:foul] ? HTTParty::NetworkError.new("#{e.class}: #{e.message}") : e
|
177
|
+
end
|
174
178
|
end
|
175
179
|
|
176
180
|
def handle_unauthorized(&block)
|
data/lib/httparty/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: httparty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.23.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Nunemaker
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2025-03-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: csv
|
@@ -91,6 +91,7 @@ files:
|
|
91
91
|
- examples/microsoft_graph.rb
|
92
92
|
- examples/multipart.rb
|
93
93
|
- examples/nokogiri_html_parser.rb
|
94
|
+
- examples/party_foul_mode.rb
|
94
95
|
- examples/peer_cert.rb
|
95
96
|
- examples/rescue_json.rb
|
96
97
|
- examples/rubyurl.rb
|