async-http-faraday 0.10.0 → 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: c3de5d2e0aeda370b0fb885fb00abe5781492da63ee7ba7a0b36da91ea15df75
4
- data.tar.gz: a5fc3ac76a50db186cadf826c4e458029b337574ad9771365f2015d0be30a276
3
+ metadata.gz: 461ee3d89afc1d0c8077b02f2bddfa4e9fd91e311fa810cb005aea26c0135b57
4
+ data.tar.gz: fc58f167759eaeadfe849ea631986a2f112bc959ec72ea2b7b9703b5126baf8c
5
5
  SHA512:
6
- metadata.gz: 29096ef34c9bda8c50c62422cfd9a353c331d91fb3a61c8d621e90a0c8d7c0b12ff591f3e520e2c485a84e8f6c1519153d9c36f18e8a90bf627719d7787f6adb
7
- data.tar.gz: 9ee7b48f082dc8297ff05164988095d2ffcb3954a6b68c86eb52e7a0d2f348f83a5dc7209232c71c898a0d53b522172b7210b6e1b5da0e98a88a69a8e07c8ff1
6
+ metadata.gz: 421f27bf1a380003fee2c76ba501a74984e01abc7a380bb5bd8f75452bbf2fa5cc3d25a135bd47813e8248ecff92d625f285e078e04c335c4df21831726fe324
7
+ data.tar.gz: 30993cd6afe2bd13f20178d8ca1b1560a47a20cc68f2e4bfbb456a4f59b5c55590d8a1319db0210abdca3c3dabc1a703867ac550d0f801efec3c7c3397039c07
@@ -23,7 +23,9 @@
23
23
  require 'faraday'
24
24
  require 'faraday/adapter'
25
25
  require 'kernel/sync'
26
- require 'async/http/internet'
26
+
27
+ require 'async/http/client'
28
+ require 'async/http/proxy'
27
29
 
28
30
  require_relative 'agent'
29
31
 
@@ -44,24 +46,83 @@ module Async
44
46
  SocketError
45
47
  ].freeze
46
48
 
47
- def initialize(*arguments, **options, &block)
48
- super
49
+ def initialize(*arguments, timeout: nil, **options, &block)
50
+ super(*arguments, **options)
51
+
52
+ @timeout = timeout
53
+
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
49
65
 
50
- @internet = Async::HTTP::Internet.new
51
- @persistent = options.fetch(:persistent, true)
52
- @timeout = options[:timeout]
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
53
88
  end
54
89
 
55
90
  def close
56
- @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)
57
97
  end
58
98
 
59
99
  def call(env)
60
100
  super
61
101
 
62
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
+
63
124
  with_timeout do
64
- response = @internet.call(env[:method].to_s.upcase, env[:url].to_s, env[:request_headers], env[:body] || [])
125
+ response = client.call(request)
65
126
 
66
127
  save_response(env, response.status, response.read, response.headers)
67
128
  end
@@ -23,7 +23,7 @@
23
23
  module Async
24
24
  module HTTP
25
25
  module Faraday
26
- VERSION = "0.10.0"
26
+ VERSION = "0.11.0"
27
27
  end
28
28
  end
29
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-http-faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams