18
18
module Async
19
19
module HTTP
20
20
module Faraday
21
+ # This is a simple wrapper around Faraday's body that allows it to be read in chunks.
21
22
class BodyReadWrapper < ::Protocol ::HTTP ::Body ::Readable
23
+ # Create a new wrapper around the given body.
24
+ #
25
+ # The body must respond to `#read` and `#close` and is often an instance of `IO` or `Faraday::Multipart::CompositeReadIO`.
26
+ #
27
+ # @parameter body [Interface(:read)] The input body to wrap.
28
+ # @parameter block_size [Integer] The size of the blocks to read from the body.
22
29
def initialize ( body , block_size : 4096 )
23
30
@body = body
24
31
@block_size = block_size
25
32
end
26
33
34
+ # Close the body if possible.
27
35
def close ( error = nil )
28
36
@body . close if @body . respond_to? ( :close )
29
37
ensure
30
38
super
31
39
end
32
40
41
+ # Read from the body in chunks.
33
42
def read
34
43
@body . read ( @block_size )
35
44
end
36
45
end
37
46
47
+ # An adapter that allows Faraday to use Async::HTTP as the underlying HTTP client.
38
48
class Adapter < ::Faraday ::Adapter
49
+ # The exceptions that are considered connection errors and result in a `Faraday::ConnectionFailed` exception.
39
50
CONNECTION_EXCEPTIONS = [
40
51
Errno ::EADDRNOTAVAIL ,
41
52
Errno ::ECONNABORTED ,
@@ -49,6 +60,10 @@ class Adapter < ::Faraday::Adapter
49
60
SocketError
50
61
] . freeze
51
62
63
+ # Create a Farady compatible adapter.
64
+ #
65
+ # @parameter timeout [Integer] The timeout for requests.
66
+ # @parameter options [Hash] Additional options to pass to the underlying Async::HTTP::Client.
52
67
def initialize ( *arguments , timeout : nil , **options , &block )
53
68
super ( *arguments , **options )
54
69
@@ -59,10 +74,18 @@ def initialize(*arguments, timeout: nil, **options, &block)
59
74
@options = options
60
75
end
61
76
77
+ # Make a new client for the given endpoint.
78
+ #
79
+ # @parameter endpoint [IO::Endpoint::Generic] The endpoint to create the client for.
62
80
def make_client ( endpoint )
63
81
Client . new ( endpoint , **@connection_options )
64
82
end
65
83
84
+ # Get the host key for the given endpoint.
85
+ #
86
+ # This is used to cache clients for the same host.
87
+ #
88
+ # @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the host key for.
66
89
def host_key ( endpoint )
67
90
url = endpoint . url . dup
68
91
@@ -73,6 +96,9 @@ def host_key(endpoint)
73
96
return url
74
97
end
75
98
99
+ # Get a client for the given endpoint. If a client already exists for the host, it will be reused.
100
+ #
101
+ # @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the client for.
76
102
def client_for ( endpoint )
77
103
key = host_key ( endpoint )
78
104
@@ -81,6 +107,10 @@ def client_for(endpoint)
81
107
end
82
108
end
83
109
110
+ # Get a client for the given proxy endpoint and endpoint. If a client already exists for the host, it will be reused.
111
+ #
112
+ # @parameter proxy_endpoint [IO::Endpoint::Generic] The proxy endpoint to use.
113
+ # @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the client for.
84
114
def proxy_client_for ( proxy_endpoint , endpoint )
85
115
key = [ host_key ( proxy_endpoint ) , host_key ( endpoint ) ]
86
116
@@ -90,6 +120,7 @@ def proxy_client_for(proxy_endpoint, endpoint)
90
120
end
91
121
end
92
122
123
+ # Close all clients.
93
124
def close
94
125
# The order of operations here is to avoid a race condition between iterating over clients (#close may yield) and creating new clients.
95
126
clients = @clients . values
@@ -99,6 +130,12 @@ def close
99
130
clients . each ( &:close )
100
131
end
101
132
133
+ # Make a request using the adapter.
134
+ #
135
+ # @parameter env [Faraday::Env] The environment to make the request in.
136
+ # @raises [Faraday::TimeoutError] If the request times out.
137
+ # @raises [Faraday::SSLError] If there is an SSL error.
138
+ # @raises [Faraday::ConnectionFailed] If there is a connection error.
102
139
def call ( env )
103
140
super
104
141
0 commit comments