faraday-net-http2 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8e8099be6e0ad053b6fc8eefd9114f1bfbd05b8a58b9727b9fe751e1ef70ce75
4
+ data.tar.gz: e084f41c03c7bb8b78084db42be8a0e77005e236dd0136ee3e0e6ab9efad58f7
5
+ SHA512:
6
+ metadata.gz: fd8bbd059b16384aad30e2d8eaaa7d2957ce955b16d8401575478da00062f7255d36798a903d65d8927cf203f56365b7c39e4447f68f33f665321865004bda84
7
+ data.tar.gz: 34f640e1ea88196fe89e0dfccff98f1049d1d937b7e80d104c1d9f862c26f27e990183897c79315fff4b08f6791c052145f282a881363a30d2056323a6433c9c
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 André Luis Leal Cardoso Junior
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,52 @@
1
+ # Faraday NetHttp2 Adapter
2
+
3
+ This is an initial implementation of a [Faraday 2][faraday] adapter for the [NetHttp2][net-http2] HTTP client.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'faraday-net-http2'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself with `gem install faraday-net-http2` and require it in your ruby code with `require 'faraday/net-http2'`
18
+
19
+ ## Usage
20
+
21
+ ### Basic
22
+
23
+ ```ruby
24
+ conn = Faraday.new(...) do |f|
25
+ f.adapter :net_http2
26
+ end
27
+ ```
28
+
29
+ ## Development
30
+
31
+ 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.
32
+
33
+ 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).
34
+
35
+ ### TODO
36
+
37
+ - [ ] Enable all possible faraday features on spec/faraday/adapter/net_http2_spec.rb.
38
+ - [ ] Streaming support?
39
+ - [ ] Review custom webmock adapter, and maybe push it back to the main repo
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on [GitHub][repo].
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [license][license].
48
+
49
+ [faraday]: https://github.com/lostisland/faraday
50
+ [net-http2]: https://github.com/ostinelli/net-http2
51
+ [repo]: https://github.com/andrehjr/faraday-net-http2
52
+ [license]: LICENSE.md
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net-http2'
4
+ require 'net/http/status'
5
+
6
+ module Faraday
7
+ class Adapter
8
+ # This class provides the main implementation for your adapter.
9
+ # There are some key responsibilities that your adapter should satisfy:
10
+ # * Initialize and store internally the client you chose (e.g. Net::HTTP)
11
+ # * Process requests and save the response (see `#call`)
12
+ class NetHttp2 < Faraday::Adapter
13
+ # The initialize method is lazy-called ONCE when the connection stack is built.
14
+ # See https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb
15
+ #
16
+ # @param app [#call] the "rack app" wrapped in middleware. See https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb#L157
17
+ # @param opts [Hash] the options hash with all the options necessary for the adapter to correctly configure itself.
18
+ # These are automatically stored into `@connection_options` when you call `super`.
19
+ # @param block [Proc] the configuration block for the adapter. This usually provides the possibility to access the internal client from the outside
20
+ # and set properties that are not included in Faraday's API. It's automatically stored into `@config_block` when you call `super`.
21
+ def initialize(app = nil, opts = {}, &block)
22
+ super(app, opts, &block)
23
+ end
24
+
25
+ # This is the main method in your adapter. Since an adapter is a middleware, this method will be called FOR EVERY REQUEST.
26
+ # The main task of this method is to perform a call using the internal client and save the response.
27
+ # Since this method is not called directly f`rom the outside, you'll need to use `env` in order to:
28
+ # * Get the request parameters (see `Faraday::Env` and `Faraday::RequestOptions` for the full list). This includes processing:
29
+ # * The request method, url, headers, parameters and body
30
+ # * The SSL configuration (env[:ssl])
31
+ # * The request configuration (env[:request]), i.e. things like: timeout, proxy, etc...
32
+ # * Set the response attributes. This can be done easily by calling `save_response`. These include:
33
+ # * Response headers and body
34
+ # * Response status and reason_phrase
35
+ #
36
+ # @param env [Faraday::Env] the environment of the request being processed
37
+ def call(env)
38
+ # First thing to do should be to call `super`. This will take care of the following for you:
39
+ # * Clear the body if the request supports a body
40
+ # * Initialize `env.response` to a `Faraday::Response`
41
+ super
42
+
43
+ client = ::NetHttp2::Client.new(env[:url], connect_timeout: env[:open_timeout])
44
+
45
+ opts = {
46
+ method: env[:method],
47
+ body: env[:body],
48
+ headers: env[:request_headers],
49
+ accept_encoding: ''
50
+ }.merge(@connection_options)
51
+
52
+ response = client.call(env[:method], env[:url].request_uri, opts)
53
+
54
+ # Now that you got the response in the client's format, you need to call `save_response` to store the necessary
55
+ # details into the `env`. This method accepts a block to make it easier for you to set response headers using
56
+ # `Faraday::Utils::Headers`. Simply provide a block that given a `response_headers` hash sets the necessary key/value pairs.
57
+ # Faraday will automatically take care of things like capitalising keys and coercing values.
58
+ reason_phrase = Net::HTTP::STATUS_CODES.fetch(response.status.to_i)
59
+ save_response(env, response.status, response.body, response.headers, reason_phrase) do |response_headers|
60
+ response.headers.each do |key, value|
61
+ response_headers[key] = value
62
+ end
63
+ end
64
+
65
+ client.close
66
+
67
+ # NOTE: An adapter `call` MUST return the `env.response`. If `save_response` is the last line in your `call`
68
+ # method implementation, it will automatically return the response for you.
69
+ # Otherwise, you'll need to manually do so. You can do this with any (not both) of the following lines:
70
+
71
+ # @app.call(env)
72
+ env.response
73
+ rescue Errno::ETIMEDOUT, ::NetHttp2::AsyncRequestTimeout => e
74
+ raise Faraday::TimeoutError, e
75
+ rescue Errno::ECONNREFUSED => e
76
+ raise Faraday::ConnectionFailed, e
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1 @@
1
+ require_relative 'net_http2'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ module NetHttp2
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'webmock'
4
+
5
+ module WebMock
6
+ module HttpLibAdapters
7
+ class WebMockNetHttp2Client < NetHttp2::Client
8
+
9
+ def call(method, path, options={})
10
+ request = prepare_request(method, path, options)
11
+
12
+ request_signature = WebMock::RequestSignature.new method, request.uri, body: request.body, headers: request.headers
13
+ WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
14
+
15
+ if (mock_response = WebMock::StubRegistry.instance.response_for_request(request_signature))
16
+ raise Errno::ETIMEDOUT if mock_response.should_timeout
17
+ response = NetHttp2::Response.new(
18
+ headers: { ":status" => mock_response.status[0] }.merge(mock_response.headers || {}),
19
+ body: mock_response.body
20
+ )
21
+
22
+ WebMock::CallbackRegistry.invoke_callbacks({ lib: :net_http2 }, request_signature, mock_response)
23
+ response
24
+ elsif WebMock.net_connect_allowed?(request_signature.uri)
25
+ super
26
+ else
27
+ raise WebMock::NetConnectNotAllowedError, request_signature
28
+ end
29
+ end
30
+ end
31
+
32
+ class NetHttp2Adapter < WebMock::HttpLibAdapter
33
+ adapter_for :net_http2
34
+
35
+ OriginalNetHttp2Client = ::NetHttp2::Client unless const_defined?(:OriginalNetHttp2Client)
36
+
37
+ def self.enable!
38
+ ::NetHttp2.send(:remove_const, :Client)
39
+ ::NetHttp2.send(:const_set, :Client, WebMockNetHttp2Client)
40
+ end
41
+
42
+ def self.disable!
43
+ ::NetHttp2.send(:remove_const, :Client)
44
+ ::NetHttp2.send(:const_set, :Client, OriginalNetHttp2Client)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'adapter/net_http2'
4
+ require_relative 'net_http2/version'
5
+
6
+ module Faraday
7
+ module NetHttp2
8
+ Faraday::Adapter.register_middleware(net_http2: Faraday::Adapter::NetHttp2)
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday-net-http2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - André Luis Leal Cardoso Junior
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2024-07-29 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: faraday
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '1.10'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '1.10'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '3'
32
+ - !ruby/object:Gem::Dependency
33
+ name: net-http2
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Faraday adapter for NetHttp2
47
+ email:
48
+ - andrehjr@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - LICENSE.md
54
+ - README.md
55
+ - lib/faraday/adapter/net_http2.rb
56
+ - lib/faraday/net-http2.rb
57
+ - lib/faraday/net_http2.rb
58
+ - lib/faraday/net_http2/version.rb
59
+ - lib/faraday/net_http2/webmock_adapter.rb
60
+ homepage: https://github.com/andrehjr/faraday-net-http2
61
+ licenses:
62
+ - MIT
63
+ metadata:
64
+ homepage_uri: https://github.com/andrehjr/faraday-net-http2
65
+ source_code_uri: https://github.com/andrehjr/faraday-net-http2
66
+ changelog_uri: https://github.com/andrehjr/faraday-net-http2
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.6.0.dev
82
+ specification_version: 4
83
+ summary: Faraday adapter for NetHttp2
84
+ test_files: []