async-http-faraday 0.7.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c109996bf4e1ac263d36167d2f090404defde07adf3be7471f175f5bdd279d22
4
- data.tar.gz: 7cdaaad412d469ac673dc85cf149ffdf7fcd593b859a2c891c23bde36c065e6b
3
+ metadata.gz: 461ee3d89afc1d0c8077b02f2bddfa4e9fd91e311fa810cb005aea26c0135b57
4
+ data.tar.gz: fc58f167759eaeadfe849ea631986a2f112bc959ec72ea2b7b9703b5126baf8c
5
5
  SHA512:
6
- metadata.gz: 40b5496840bec8a74b60adfb807e84c1f863b1dd6719c6406018617d57c4c9313b45e492e88a2e2d224c15abf7c19b7fd5c0a7ed75280622193850942b0f86c0
7
- data.tar.gz: 1d326fe89303b9e64ba5824d72d29bf063e5124a314238dce919c1ff75fb3f56926a633c00d2ce0d6f6fd5b656596febdb4e4670789593b8d9fd1e44395801b3
6
+ metadata.gz: 421f27bf1a380003fee2c76ba501a74984e01abc7a380bb5bd8f75452bbf2fa5cc3d25a135bd47813e8248ecff92d625f285e078e04c335c4df21831726fe324
7
+ data.tar.gz: 30993cd6afe2bd13f20178d8ca1b1560a47a20cc68f2e4bfbb456a4f59b5c55590d8a1319db0210abdca3c3dabc1a703867ac550d0f801efec3c7c3397039c07
data/examples/topics.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
4
5
 
data/lib/.DS_Store ADDED
Binary file
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
4
  #
3
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
4
  #
3
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -20,14 +22,16 @@
20
22
 
21
23
  require 'faraday'
22
24
  require 'faraday/adapter'
23
- require 'async/http/internet'
25
+ require 'kernel/sync'
26
+
27
+ require 'async/http/client'
28
+ require 'async/http/proxy'
29
+
30
+ require_relative 'agent'
24
31
 
25
32
  module Async
26
33
  module HTTP
27
34
  module Faraday
28
- # Detect whether we can use persistent connections:
29
- PERSISTENT = ::Faraday::Connection.instance_methods.include?(:close)
30
-
31
35
  class Adapter < ::Faraday::Adapter
32
36
  CONNECTION_EXCEPTIONS = [
33
37
  Errno::EADDRNOTAVAIL,
@@ -41,26 +45,87 @@ module Async
41
45
  IOError,
42
46
  SocketError
43
47
  ].freeze
44
-
45
- def initialize(*arguments, **options, &block)
46
- super
48
+
49
+ def initialize(*arguments, timeout: nil, **options, &block)
50
+ super(*arguments, **options)
51
+
52
+ @timeout = timeout
47
53
 
48
- @internet = Async::HTTP::Internet.new
49
- @persistent = PERSISTENT && options.fetch(:persistent, true)
50
- @timeout = options[:timeout]
54
+ @clients = {}
55
+
56
+ @options = options
57
+ end
58
+
59
+ def make_client(endpoint)
60
+ Client.new(endpoint, **@connection_options)
61
+ end
62
+
63
+ def host_key(endpoint)
64
+ url = endpoint.url.dup
65
+
66
+ url.path = ""
67
+ url.fragment = nil
68
+ url.query = nil
69
+
70
+ return url
71
+ end
72
+
73
+ def client_for(endpoint)
74
+ key = host_key(endpoint)
75
+
76
+ @clients.fetch(key) do
77
+ @clients[key] = make_client(endpoint)
78
+ end
79
+ end
80
+
81
+ def proxy_client_for(proxy_endpoint, endpoint)
82
+ key = [host_key(proxy_endpoint), host_key(endpoint)]
83
+
84
+ @clients.fetch(key) do
85
+ client = client_for(proxy_endpoint)
86
+ @clients[key] = client.proxied_client(endpoint)
87
+ end
51
88
  end
52
89
 
53
90
  def close
54
- @internet.close
91
+ # The order of operations here is to avoid a race condition between iterating over clients (#close may yield) and creating new clients.
92
+ clients = @clients.values
93
+
94
+ @clients.clear
95
+
96
+ clients.each(&:close)
55
97
  end
56
98
 
57
99
  def call(env)
58
100
  super
59
101
 
60
- with_timeout do
61
- response = @internet.call(env[:method].to_s.upcase, env[:url].to_s, env[:request_headers], env[:body] || [])
62
-
63
- save_response(env, response.status, response.read, response.headers)
102
+ Sync do
103
+ endpoint = Endpoint.new(env.url)
104
+
105
+ if proxy = env.request.proxy
106
+ proxy_endpoint = Endpoint.new(proxy.uri)
107
+ client = self.proxy_client_for(proxy_endpoint, endpoint)
108
+ else
109
+ client = self.client_for(endpoint)
110
+ end
111
+
112
+ if body = env.body
113
+ body = Body::Buffered.wrap(body)
114
+ end
115
+
116
+ if headers = env.request_headers
117
+ headers = ::Protocol::HTTP::Headers[headers]
118
+ end
119
+
120
+ method = env.method.upcase
121
+
122
+ request = ::Protocol::HTTP::Request.new(endpoint.scheme, endpoint.authority, method, endpoint.path, nil, headers, body)
123
+
124
+ with_timeout do
125
+ response = client.call(request)
126
+
127
+ save_response(env, response.status, response.read, response.headers)
128
+ end
64
129
  end
65
130
 
66
131
  return @app.call(env)
@@ -70,13 +135,10 @@ module Async
70
135
  raise ::Faraday::SSLError, e
71
136
  rescue *CONNECTION_EXCEPTIONS => e
72
137
  raise ::Faraday::ConnectionFailed, e
73
- ensure
74
- # Don't retain persistent connections unless they will eventually be closed:
75
- @internet.close unless @persistent
76
138
  end
77
-
139
+
78
140
  private
79
-
141
+
80
142
  def with_timeout(task: Async::Task.current)
81
143
  if @timeout
82
144
  task.with_timeout(@timeout, ::Faraday::TimeoutError) do
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
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.
22
+
23
+ begin
24
+ require 'sawyer/agent'
25
+
26
+ # This is a nasty hack until https://github.com/lostisland/sawyer/pull/67 is resolved:
27
+ unless Sawyer::Agent.instance_methods.include?(:close)
28
+ class Sawyer::Agent
29
+ def close
30
+ @conn.close if @conn.respond_to?(:close)
31
+ end
32
+ end
33
+ end
34
+ rescue LoadError
35
+ # Ignore.
36
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
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.
22
+
23
+ require_relative 'adapter'
24
+
25
+ ::Faraday.default_adapter = :async_http
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
4
  #
3
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -21,7 +23,7 @@
21
23
  module Async
22
24
  module HTTP
23
25
  module Faraday
24
- VERSION = "0.7.1"
26
+ VERSION = "0.11.0"
25
27
  end
26
28
  end
27
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-http-faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-30 00:00:00.000000000 Z
11
+ date: 2021-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async-http
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.2'
55
55
  - !ruby/object:Gem::Dependency
56
- name: bake-bundler
56
+ name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -80,20 +80,6 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: bundler
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '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'
97
83
  - !ruby/object:Gem::Dependency
98
84
  name: rspec
99
85
  requirement: !ruby/object:Gem::Requirement
@@ -108,28 +94,24 @@ dependencies:
108
94
  - - "~>"
109
95
  - !ruby/object:Gem::Version
110
96
  version: '3.6'
111
- description:
97
+ description:
112
98
  email:
113
- - samuel.williams@oriontransfer.co.nz
114
99
  executables: []
115
100
  extensions: []
116
101
  extra_rdoc_files: []
117
102
  files:
118
- - ".editorconfig"
119
- - ".gitignore"
120
- - ".rspec"
121
- - ".travis.yml"
122
- - Gemfile
123
- - README.md
124
- - async-http-faraday.gemspec
125
103
  - examples/topics.rb
104
+ - lib/.DS_Store
126
105
  - lib/async/http/faraday.rb
127
106
  - lib/async/http/faraday/adapter.rb
107
+ - lib/async/http/faraday/agent.rb
108
+ - lib/async/http/faraday/default.rb
128
109
  - lib/async/http/faraday/version.rb
129
110
  homepage: https://github.com/socketry/async-http
130
- licenses: []
111
+ licenses:
112
+ - MIT
131
113
  metadata: {}
132
- post_install_message:
114
+ post_install_message:
133
115
  rdoc_options: []
134
116
  require_paths:
135
117
  - lib
@@ -144,8 +126,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
126
  - !ruby/object:Gem::Version
145
127
  version: '0'
146
128
  requirements: []
147
- rubygems_version: 3.1.2
148
- signing_key:
129
+ rubygems_version: 3.2.15
130
+ signing_key:
149
131
  specification_version: 4
150
132
  summary: Provides an adaptor between async-http and faraday.
151
133
  test_files: []
data/.editorconfig DELETED
@@ -1,6 +0,0 @@
1
- root = true
2
-
3
- [*]
4
- indent_style = tab
5
- indent_size = 2
6
-
data/.gitignore DELETED
@@ -1,13 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
10
-
11
- # rspec failure tracking
12
- .rspec_status
13
- .covered.db
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --warnings
3
- --require spec_helper
data/.travis.yml DELETED
@@ -1,19 +0,0 @@
1
- language: ruby
2
- dist: trusty
3
- cache: bundler
4
-
5
- script: bundle exec rspec
6
-
7
- matrix:
8
- include:
9
- - rvm: 2.5
10
- - rvm: 2.6
11
- - rvm: 2.7
12
- - rvm: jruby-head
13
- env: JRUBY_OPTS="--debug -X+O"
14
- - rvm: truffleruby
15
- - rvm: ruby-head
16
- allow_failures:
17
- - rvm: ruby-head
18
- - rvm: truffleruby
19
- - rvm: jruby-head
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
4
-
5
- # gem "faraday", git: "https://github.com/lostisland/faraday.git"
6
- # gem "async-http", path: "../async-http"
data/README.md DELETED
@@ -1,83 +0,0 @@
1
- # Async::HTTP::Faraday
2
-
3
- Provides an adaptor for [Faraday] to perform async HTTP requests. If you are designing a new library, you should probably just use `Async::HTTP::Client` directly.
4
-
5
- [![Build Status](https://secure.travis-ci.org/socketry/async-http-faraday.svg)](http://travis-ci.org/socketry/async-http-faraday)
6
- [![Code Climate](https://codeclimate.com/github/socketry/async-http-faraday.svg)](https://codeclimate.com/github/socketry/async-http-faraday)
7
- [![Coverage Status](https://coveralls.io/repos/socketry/async-http-faraday/badge.svg)](https://coveralls.io/r/socketry/async-http-faraday)
8
-
9
- [async]: https://github.com/socketry/async
10
- [async-io]: https://github.com/socketry/async-io
11
- [Faraday]: https://github.com/lostisland/faraday
12
-
13
- ## Installation
14
-
15
- Add this line to your application's Gemfile:
16
-
17
- ```ruby
18
- gem 'async-http-faraday'
19
- ```
20
-
21
- And then execute:
22
-
23
- $ bundle
24
-
25
- Or install it yourself as:
26
-
27
- $ gem install async-http-faraday
28
-
29
- ## Usage
30
-
31
- Here is how you set faraday to use `Async::HTTP`:
32
-
33
- ```ruby
34
- require 'async/http/faraday'
35
-
36
- # Make it the global default:
37
- Faraday.default_adapter = :async_http
38
-
39
- # Per connection:
40
- conn = Faraday.new(...) do |faraday|
41
- faraday.adapter :async_http
42
- end
43
- ```
44
-
45
- Here is how you make a request:
46
-
47
- ```ruby
48
- Async::Reactor.run do
49
- response = conn.get("/index")
50
- end
51
- ```
52
-
53
- ## Contributing
54
-
55
- 1. Fork it
56
- 2. Create your feature branch (`git checkout -b my-new-feature`)
57
- 3. Commit your changes (`git commit -am 'Add some feature'`)
58
- 4. Push to the branch (`git push origin my-new-feature`)
59
- 5. Create new Pull Request
60
-
61
- ## License
62
-
63
- Released under the MIT license.
64
-
65
- Copyright, 2015, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
66
-
67
- Permission is hereby granted, free of charge, to any person obtaining a copy
68
- of this software and associated documentation files (the "Software"), to deal
69
- in the Software without restriction, including without limitation the rights
70
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
71
- copies of the Software, and to permit persons to whom the Software is
72
- furnished to do so, subject to the following conditions:
73
-
74
- The above copyright notice and this permission notice shall be included in
75
- all copies or substantial portions of the Software.
76
-
77
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
78
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
79
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
80
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
81
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
82
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
83
- THE SOFTWARE.
@@ -1,29 +0,0 @@
1
-
2
- require_relative 'lib/async/http/faraday/version'
3
-
4
- Gem::Specification.new do |spec|
5
- spec.name = "async-http-faraday"
6
- spec.version = Async::HTTP::Faraday::VERSION
7
- spec.authors = ["Samuel Williams"]
8
- spec.email = ["samuel.williams@oriontransfer.co.nz"]
9
-
10
- spec.summary = "Provides an adaptor between async-http and faraday."
11
- spec.homepage = "https://github.com/socketry/async-http"
12
-
13
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
14
- f.match(%r{^(test|spec|features)/})
15
- end
16
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
- spec.require_paths = ["lib"]
18
-
19
- spec.add_dependency("async-http", "~> 0.42")
20
- spec.add_dependency("faraday")
21
-
22
- spec.add_development_dependency "async-rspec", "~> 1.2"
23
-
24
- spec.add_development_dependency "bake-bundler"
25
-
26
- spec.add_development_dependency "covered"
27
- spec.add_development_dependency "bundler"
28
- spec.add_development_dependency "rspec", "~> 3.6"
29
- end