faraday-httpclient 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 38e42915fda909553a3e9df19af6d3d4caa7873b7a085e94c71fd9db2aebd931
4
+ data.tar.gz: c8de5147fb1e9328f99df5156ab494e1c83fdb218ab64007f1f18245e8df52a2
5
+ SHA512:
6
+ metadata.gz: 4044cf8c2ce0303953d5ff0f1488f0221ea633ae37ec43902bbc50b7ecd270ef8c6f86438e94d9d33e4b989206ac59ed6ab5a02eb1b2b6e6184bf4ab7c94d96a
7
+ data.tar.gz: c8877551110b9d0bbb70ec820f04161d3edb2b6a0b04143016baccc3523e8a823a1ee4b87e9fc6c5e5adafe4d57d17545dee15dd94ab7dca40541defc1e91bc5
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Jan van der Pas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Faraday HTTPClient adapter
2
+
3
+ This gem is a [Faraday][faraday] adapter for the [HTTPClient][httpclient] library.
4
+ Faraday is an HTTP client library that provides a common interface over many adapters.
5
+ Every adapter is defined into its own gem. This gem defines the adapter for HTTPClient.
6
+
7
+ ## Installation
8
+
9
+ Add these lines to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'httpclient', '>= 2.2'
13
+ gem 'faraday'
14
+ gem 'faraday-httpclient'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install them yourself as:
22
+
23
+ $ gem install httpclient -v '>= 2.2'
24
+ $ gem install faraday faraday-httpclient
25
+
26
+ ## Usage
27
+
28
+ Configure your Faraday connection to use this adapter like this:
29
+
30
+ ```ruby
31
+ connection = Faraday.new(url, conn_options) do |conn|
32
+ conn.adapter(:httpclient)
33
+ end
34
+ ```
35
+
36
+ For more information on how to setup your Faraday connection and adapters usage, please refer to the [Faraday Website][faraday-website].
37
+
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+
42
+ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](rubygems).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on [GitHub][repo].
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [license][license].
51
+
52
+ ## Code of Conduct
53
+
54
+ Everyone interacting in the Faraday HTTPClient adapter project's codebase, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct][code-of-conduct].
55
+
56
+ [faraday]: https://github.com/lostisland/faraday
57
+ [faraday-website]: https://lostisland.github.io/faraday
58
+ [httpclient]: https://github.com/nahi/httpclient
59
+ [rubygems]: https://rubygems.org
60
+ [repo]: https://github.com/lostisland/faraday-httpclient
61
+ [license]: https://github.com/lostisland/faraday-httpclient/blob/main/LICENSE.md
62
+ [code-of-conduct]: https://github.com/lostisland/faraday-httpclient/blob/main/CODE_OF_CONDUCT.md
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ class Adapter
5
+ # This class provides the main implementation for your adapter.
6
+ # There are some key responsibilities that your adapter should satisfy:
7
+ # * Initialize and store internally the client you chose (e.g. Net::HTTP)
8
+ # * Process requests and save the response (see `#call`)
9
+ class HTTPClient < Faraday::Adapter
10
+ def build_connection(env)
11
+ @client ||= ::HTTPClient.new.tap do |cli|
12
+ # enable compression
13
+ cli.transparent_gzip_decompression = true
14
+ end
15
+
16
+ if (req = env[:request])
17
+ if (proxy = req[:proxy])
18
+ configure_proxy @client, proxy
19
+ end
20
+
21
+ if (bind = req[:bind])
22
+ configure_socket @client, bind
23
+ end
24
+
25
+ configure_timeouts @client, req
26
+ end
27
+
28
+ if env[:url].scheme == 'https' && (ssl = env[:ssl])
29
+ configure_ssl @client, ssl
30
+ end
31
+
32
+ configure_client @client
33
+
34
+ @client
35
+ end
36
+
37
+ def call(env)
38
+ super
39
+
40
+ # TODO: Don't stream yet.
41
+ # https://github.com/nahi/httpclient/pull/90
42
+ env[:body] = env[:body].read if env[:body].respond_to? :read
43
+
44
+ connection(env) do |http|
45
+ resp = http.request env[:method], env[:url],
46
+ body: env[:body],
47
+ header: env[:request_headers]
48
+
49
+ if (req = env[:request]).stream_response?
50
+ warn "Streaming downloads for #{self.class.name} " \
51
+ 'are not yet implemented.'
52
+ req.on_data.call(resp.body, resp.body.bytesize)
53
+ end
54
+ save_response env, resp.status, resp.body, resp.headers, resp.reason
55
+
56
+ @app.call env
57
+ end
58
+ rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
59
+ raise Faraday::TimeoutError, $ERROR_INFO
60
+ rescue ::HTTPClient::BadResponseError => e
61
+ if e.message.include?('status 407')
62
+ raise Faraday::ConnectionFailed,
63
+ %(407 "Proxy Authentication Required ")
64
+ end
65
+
66
+ raise Faraday::ClientError, $ERROR_INFO
67
+ rescue Errno::EADDRNOTAVAIL, Errno::ECONNREFUSED, IOError, SocketError
68
+ raise Faraday::ConnectionFailed, $ERROR_INFO
69
+ rescue StandardError => e
70
+ if defined?(::OpenSSL::SSL::SSLError) && \
71
+ e.is_a?(::OpenSSL::SSL::SSLError)
72
+ raise Faraday::SSLError, e
73
+ end
74
+
75
+ raise
76
+ end
77
+
78
+ # @param bind [Hash]
79
+ def configure_socket(client, bind)
80
+ client.socket_local.host = bind[:host]
81
+ client.socket_local.port = bind[:port]
82
+ end
83
+
84
+ # Configure proxy URI and any user credentials.
85
+ #
86
+ # @param proxy [Hash]
87
+ def configure_proxy(client, proxy)
88
+ client.proxy = proxy[:uri]
89
+ return unless proxy[:user] && proxy[:password]
90
+
91
+ client.set_proxy_auth(proxy[:user], proxy[:password])
92
+ end
93
+
94
+ # @param ssl [Hash]
95
+ def configure_ssl(client, ssl)
96
+ ssl_config = client.ssl_config
97
+ ssl_config.verify_mode = ssl_verify_mode(ssl)
98
+ ssl_config.cert_store = ssl_cert_store(ssl)
99
+
100
+ ssl_config.add_trust_ca ssl[:ca_file] if ssl[:ca_file]
101
+ ssl_config.add_trust_ca ssl[:ca_path] if ssl[:ca_path]
102
+ ssl_config.client_cert = ssl[:client_cert] if ssl[:client_cert]
103
+ ssl_config.client_key = ssl[:client_key] if ssl[:client_key]
104
+ ssl_config.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
105
+ end
106
+
107
+ # @param req [Hash]
108
+ def configure_timeouts(client, req)
109
+ if (sec = request_timeout(:open, req))
110
+ client.connect_timeout = sec
111
+ end
112
+
113
+ if (sec = request_timeout(:write, req))
114
+ client.send_timeout = sec
115
+ end
116
+
117
+ return unless (sec = request_timeout(:read, req))
118
+
119
+ client.receive_timeout = sec
120
+ end
121
+
122
+ def configure_client(client)
123
+ @config_block&.call(client)
124
+ end
125
+
126
+ # @param ssl [Hash]
127
+ # @return [OpenSSL::X509::Store]
128
+ def ssl_cert_store(ssl)
129
+ return ssl[:cert_store] if ssl[:cert_store]
130
+
131
+ # Memoize the cert store so that the same one is passed to
132
+ # HTTPClient each time, to avoid resyncing SSL sessions when
133
+ # it's changed
134
+
135
+ # Use the default cert store by default, i.e. system ca certs
136
+ @ssl_cert_store ||= OpenSSL::X509::Store.new.tap(&:set_default_paths)
137
+ end
138
+
139
+ # @param ssl [Hash]
140
+ def ssl_verify_mode(ssl)
141
+ ssl[:verify_mode] || begin
142
+ if ssl.fetch(:verify, true)
143
+ OpenSSL::SSL::VERIFY_PEER |
144
+ OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
145
+ else
146
+ OpenSSL::SSL::VERIFY_NONE
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'adapter/httpclient'
4
+ require_relative 'httpclient/version'
5
+
6
+ module Faraday
7
+ # Main Faraday::HTTPClient module
8
+ module HTTPClient
9
+ Faraday::Adapter.register_middleware(httpclient: Faraday::Adapter::HTTPClient)
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ module HTTPClient
5
+ VERSION = '1.0.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,205 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday-httpclient
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - "@iMacTia"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-07-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httpclient
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '2.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '2.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.19.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.19.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: multipart-parser
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.1.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.1.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.4'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.4'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 1.12.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 1.12.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop-packaging
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.5'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.5'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-performance
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '1.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '1.0'
167
+ description: Faraday adapter for HTTPClient
168
+ email:
169
+ - giuffrida.mattia@gmail.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - LICENSE.md
175
+ - README.md
176
+ - lib/faraday/adapter/httpclient.rb
177
+ - lib/faraday/httpclient.rb
178
+ - lib/faraday/httpclient/version.rb
179
+ homepage: https://github.com/lostisland/faraday-httpclient
180
+ licenses:
181
+ - MIT
182
+ metadata:
183
+ homepage_uri: https://github.com/lostisland/faraday-httpclient
184
+ source_code_uri: https://github.com/lostisland/faraday-httpclient
185
+ changelog_uri: https://github.com/lostisland/faraday-httpclient
186
+ post_install_message:
187
+ rdoc_options: []
188
+ require_paths:
189
+ - lib
190
+ required_ruby_version: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: 2.4.0
195
+ required_rubygems_version: !ruby/object:Gem::Requirement
196
+ requirements:
197
+ - - ">="
198
+ - !ruby/object:Gem::Version
199
+ version: '0'
200
+ requirements: []
201
+ rubygems_version: 3.1.6
202
+ signing_key:
203
+ specification_version: 4
204
+ summary: Faraday adapter for HTTPClient
205
+ test_files: []