Skip to content

Commit dd1e4e4

Browse files
committed
Documentation.
1 parent fcca9d8 commit dd1e4e4

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

lib/async/http/faraday.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@
77
require_relative "faraday/adapter"
88

99
Faraday::Adapter.register_middleware :async_http => Async::HTTP::Faraday::Adapter
10+
11+
# @namespace
12+
module Async
13+
# @namespace
14+
module HTTP
15+
# @namespace
16+
module Faraday
17+
end
18+
end
19+
end

lib/async/http/faraday/adapter.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,35 @@
1818
module Async
1919
module HTTP
2020
module Faraday
21+
# This is a simple wrapper around Faraday's body that allows it to be read in chunks.
2122
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.
2229
def initialize(body, block_size: 4096)
2330
@body = body
2431
@block_size = block_size
2532
end
2633

34+
# Close the body if possible.
2735
def close(error = nil)
2836
@body.close if @body.respond_to?(:close)
2937
ensure
3038
super
3139
end
3240

41+
# Read from the body in chunks.
3342
def read
3443
@body.read(@block_size)
3544
end
3645
end
3746

47+
# An adapter that allows Faraday to use Async::HTTP as the underlying HTTP client.
3848
class Adapter < ::Faraday::Adapter
49+
# The exceptions that are considered connection errors and result in a `Faraday::ConnectionFailed` exception.
3950
CONNECTION_EXCEPTIONS = [
4051
Errno::EADDRNOTAVAIL,
4152
Errno::ECONNABORTED,
@@ -49,6 +60,10 @@ class Adapter < ::Faraday::Adapter
4960
SocketError
5061
].freeze
5162

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.
5267
def initialize(*arguments, timeout: nil, **options, &block)
5368
super(*arguments, **options)
5469

@@ -59,10 +74,18 @@ def initialize(*arguments, timeout: nil, **options, &block)
5974
@options = options
6075
end
6176

77+
# Make a new client for the given endpoint.
78+
#
79+
# @parameter endpoint [IO::Endpoint::Generic] The endpoint to create the client for.
6280
def make_client(endpoint)
6381
Client.new(endpoint, **@connection_options)
6482
end
6583

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.
6689
def host_key(endpoint)
6790
url = endpoint.url.dup
6891

@@ -73,6 +96,9 @@ def host_key(endpoint)
7396
return url
7497
end
7598

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.
76102
def client_for(endpoint)
77103
key = host_key(endpoint)
78104

@@ -81,6 +107,10 @@ def client_for(endpoint)
81107
end
82108
end
83109

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.
84114
def proxy_client_for(proxy_endpoint, endpoint)
85115
key = [host_key(proxy_endpoint), host_key(endpoint)]
86116

@@ -90,6 +120,7 @@ def proxy_client_for(proxy_endpoint, endpoint)
90120
end
91121
end
92122

123+
# Close all clients.
93124
def close
94125
# The order of operations here is to avoid a race condition between iterating over clients (#close may yield) and creating new clients.
95126
clients = @clients.values
@@ -99,6 +130,12 @@ def close
99130
clients.each(&:close)
100131
end
101132

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.
102139
def call(env)
103140
super
104141

lib/async/http/faraday/default.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55

66
require_relative 'adapter'
77

8+
# Set the default adapter to use Async::HTTP.
89
::Faraday.default_adapter = :async_http

0 commit comments

Comments
 (0)