Skip to content

Commit 767de78

Browse files
committed
Convert options hashes to keyword arguments
1 parent 99f93d7 commit 767de78

23 files changed

Lines changed: 414 additions & 342 deletions

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Changed
1111

12+
- **BREAKING** Convert options hash parameters to keyword arguments across the
13+
public API. Methods like `HTTP.get(url, body: "data")` continue to work, but
14+
passing an explicit hash (e.g., `HTTP.get(url, {body: "data"})`) is no longer
15+
supported. Affected methods: all HTTP verb methods (`get`, `post`, etc.),
16+
`request`, `follow`, `Request.new`, `Response.new`, `Redirector.new`,
17+
`Retriable::Performer.new`, `Retriable::DelayCalculator.new`, and
18+
`Timeout::Null.new` (and subclasses)
1219
- **BREAKING** Extract request building into `HTTP::Request::Builder`. The
1320
`build_request` method has been removed from `Client`, `Session`, and the
1421
top-level `HTTP` module. Use `HTTP::Request::Builder.new(options).build(verb, uri)`

lib/http/chainable.rb

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ module Chainable
1919
# @param (see Client#request)
2020
# @return [HTTP::Response]
2121
# @api public
22-
def request(verb, uri, opts = {})
23-
make_client(default_options).request(verb, uri, opts)
22+
def request(verb, uri, **)
23+
make_client(default_options).request(verb, uri, **)
2424
end
2525

2626
# Set timeout on the request
@@ -127,12 +127,12 @@ def via(*proxy)
127127
# @example
128128
# HTTP.follow.get("http://example.com")
129129
#
130-
# @param [Hash] options redirect options
130+
# @param options [Hash] redirect options
131131
# @return [HTTP::Session]
132132
# @see Redirector#initialize
133133
# @api public
134-
def follow(options = {})
135-
branch default_options.with_follow options
134+
def follow(**options)
135+
branch default_options.with_follow(options)
136136
end
137137

138138
# Make a request with the given headers
@@ -210,17 +210,12 @@ def auth(value)
210210
# HTTP.basic_auth(user: "user", pass: "pass").get("http://example.com")
211211
#
212212
# @see http://tools.ietf.org/html/rfc2617
213-
# @param [#fetch] opts
214-
# @option opts [#to_s] :user
215-
# @option opts [#to_s] :pass
213+
# @param [#to_s] user
214+
# @param [#to_s] pass
216215
# @return [HTTP::Session]
217216
# @api public
218-
def basic_auth(opts)
219-
user = opts.fetch(:user)
220-
pass = opts.fetch(:pass)
221-
creds = "#{user}:#{pass}"
222-
223-
auth("Basic #{encode64(creds)}")
217+
def basic_auth(user:, pass:)
218+
auth("Basic #{encode64("#{user}:#{pass}")}")
224219
end
225220

226221
# Get options for HTTP

lib/http/chainable/verbs.rb

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ module Verbs
1212
# HTTP.head("http://example.com")
1313
#
1414
# @param [String, URI] uri URI to request
15-
# @param [Hash] options request options
15+
# @param options [Hash] request options
1616
# @return [HTTP::Response]
1717
# @api public
18-
def head(uri, options = {})
19-
request :head, uri, options
18+
def head(uri, **)
19+
request(:head, uri, **)
2020
end
2121

2222
# Get a resource
@@ -25,11 +25,11 @@ def head(uri, options = {})
2525
# HTTP.get("http://example.com")
2626
#
2727
# @param [String, URI] uri URI to request
28-
# @param [Hash] options request options
28+
# @param options [Hash] request options
2929
# @return [HTTP::Response]
3030
# @api public
31-
def get(uri, options = {})
32-
request :get, uri, options
31+
def get(uri, **)
32+
request(:get, uri, **)
3333
end
3434

3535
# Post to a resource
@@ -38,11 +38,11 @@ def get(uri, options = {})
3838
# HTTP.post("http://example.com", body: "data")
3939
#
4040
# @param [String, URI] uri URI to request
41-
# @param [Hash] options request options
41+
# @param options [Hash] request options
4242
# @return [HTTP::Response]
4343
# @api public
44-
def post(uri, options = {})
45-
request :post, uri, options
44+
def post(uri, **)
45+
request(:post, uri, **)
4646
end
4747

4848
# Put to a resource
@@ -51,11 +51,11 @@ def post(uri, options = {})
5151
# HTTP.put("http://example.com", body: "data")
5252
#
5353
# @param [String, URI] uri URI to request
54-
# @param [Hash] options request options
54+
# @param options [Hash] request options
5555
# @return [HTTP::Response]
5656
# @api public
57-
def put(uri, options = {})
58-
request :put, uri, options
57+
def put(uri, **)
58+
request(:put, uri, **)
5959
end
6060

6161
# Delete a resource
@@ -64,11 +64,11 @@ def put(uri, options = {})
6464
# HTTP.delete("http://example.com/resource")
6565
#
6666
# @param [String, URI] uri URI to request
67-
# @param [Hash] options request options
67+
# @param options [Hash] request options
6868
# @return [HTTP::Response]
6969
# @api public
70-
def delete(uri, options = {})
71-
request :delete, uri, options
70+
def delete(uri, **)
71+
request(:delete, uri, **)
7272
end
7373

7474
# Echo the request back to the client
@@ -77,11 +77,11 @@ def delete(uri, options = {})
7777
# HTTP.trace("http://example.com")
7878
#
7979
# @param [String, URI] uri URI to request
80-
# @param [Hash] options request options
80+
# @param options [Hash] request options
8181
# @return [HTTP::Response]
8282
# @api public
83-
def trace(uri, options = {})
84-
request :trace, uri, options
83+
def trace(uri, **)
84+
request(:trace, uri, **)
8585
end
8686

8787
# Return the methods supported on the given URI
@@ -90,11 +90,11 @@ def trace(uri, options = {})
9090
# HTTP.options("http://example.com")
9191
#
9292
# @param [String, URI] uri URI to request
93-
# @param [Hash] options request options
93+
# @param options [Hash] request options
9494
# @return [HTTP::Response]
9595
# @api public
96-
def options(uri, options = {})
97-
request :options, uri, options
96+
def options(uri, **)
97+
request(:options, uri, **)
9898
end
9999

100100
# Convert to a transparent TCP/IP tunnel
@@ -103,11 +103,11 @@ def options(uri, options = {})
103103
# HTTP.connect("http://example.com")
104104
#
105105
# @param [String, URI] uri URI to request
106-
# @param [Hash] options request options
106+
# @param options [Hash] request options
107107
# @return [HTTP::Response]
108108
# @api public
109-
def connect(uri, options = {})
110-
request :connect, uri, options
109+
def connect(uri, **)
110+
request(:connect, uri, **)
111111
end
112112

113113
# Apply partial modifications to a resource
@@ -116,11 +116,11 @@ def connect(uri, options = {})
116116
# HTTP.patch("http://example.com/resource", body: "data")
117117
#
118118
# @param [String, URI] uri URI to request
119-
# @param [Hash] options request options
119+
# @param options [Hash] request options
120120
# @return [HTTP::Response]
121121
# @api public
122-
def patch(uri, options = {})
123-
request :patch, uri, options
122+
def patch(uri, **)
123+
request(:patch, uri, **)
124124
end
125125
end
126126
end

lib/http/client.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ def initialize(default_options = nil, **)
4343
# @param opts [Hash] request options
4444
# @return [HTTP::Response] the response
4545
# @api public
46-
def request(verb, uri, opts = {})
46+
def request(verb, uri, **opts)
4747
opts = @default_options.merge(opts)
4848
builder = Request::Builder.new(opts)
4949
req = builder.build(verb, uri)
5050
res = perform(req, opts)
5151
return res unless opts.follow
5252

53-
Redirector.new(opts.follow).perform(req, res) do |request|
53+
Redirector.new(**opts.follow).perform(req, res) do |request|
5454
perform(builder.wrap(request), opts)
5555
end
5656
end
@@ -123,7 +123,7 @@ def perform_once(req, options)
123123
# @return [HTTP::Response] the response
124124
# @api private
125125
def perform_with_retry(req, options)
126-
Retriable::Performer.new(options.retriable).perform(self, req) do
126+
Retriable::Performer.new(**options.retriable).perform(self, req) do
127127
perform_once(req, options)
128128
end
129129
end

lib/http/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def check_premature_eof(eof)
238238
# @return [void]
239239
# @api private
240240
def connect_socket(req, options)
241-
@socket = options.timeout_class.new(options.timeout_options) # steep:ignore
241+
@socket = options.timeout_class.new(**options.timeout_options) # steep:ignore
242242
@socket.connect(options.socket_class, req.socket_host, req.socket_port, nodelay: options.nodelay)
243243

244244
send_proxy_connect_request(req)

lib/http/features/auto_inflate.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class AutoInflate < Feature
1818
def wrap_response(response)
1919
return response unless supported_encoding?(response)
2020

21-
Response.new(inflated_response_options(response))
21+
Response.new(**inflated_response_options(response)) # steep:ignore
2222
end
2323

2424
# Returns an inflating body stream for a connection

lib/http/features/logging.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def wrap_response(response)
7878
logger.debug { stringify_headers(response.headers) }
7979
return response unless logger.debug?
8080

81-
Response.new(logged_response_options(response))
81+
Response.new(**logged_response_options(response)) # steep:ignore
8282
end
8383

8484
private

lib/http/redirector.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ class EndlessRedirectError < TooManyRedirectsError; end
4848
# @example
4949
# HTTP::Redirector.new(strict: true, max_hops: 5)
5050
#
51-
# @param [Hash] opts
52-
# @option opts [Boolean] :strict (true) redirector hops policy
53-
# @option opts [#to_i] :max_hops (5) maximum allowed amount of hops
51+
# @param [Boolean] strict (true) redirector hops policy
52+
# @param [#to_i] max_hops (5) maximum allowed amount of hops
53+
# @param [#call, nil] on_redirect optional redirect callback
5454
# @api public
5555
# @return [HTTP::Redirector]
56-
def initialize(opts = {})
57-
@strict = opts.fetch(:strict, true)
58-
@max_hops = Integer(opts.fetch(:max_hops, 5))
59-
@on_redirect = opts.fetch(:on_redirect, nil)
56+
def initialize(strict: true, max_hops: 5, on_redirect: nil)
57+
@strict = strict
58+
@max_hops = Integer(max_hops)
59+
@on_redirect = on_redirect
6060
end
6161

6262
# Follows redirects until non-redirect response found

lib/http/request.rb

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -147,28 +147,30 @@ class InvalidURIError < RequestError; end
147147

148148
# Create a new HTTP request
149149
#
150-
# @option opts [String] :version
151-
# @option opts [#to_s] :verb HTTP request method
152-
# @option opts [#call] :uri_normalizer (HTTP::URI::NORMALIZER)
153-
# @option opts [HTTP::URI, #to_s] :uri
154-
# @option opts [Hash] :headers
155-
# @option opts [Hash] :proxy
156-
# @option opts [String, Enumerable, IO, nil] :body
150+
# @param [#to_s] verb HTTP request method
151+
# @param [HTTP::URI, #to_s] uri
152+
# @param [Hash] headers
153+
# @param [Hash] proxy
154+
# @param [String, Enumerable, IO, nil] body
155+
# @param [String] version
156+
# @param [#call] uri_normalizer
157157
#
158158
# @example
159159
# Request.new(verb: :get, uri: "https://example.com")
160160
#
161161
# @return [HTTP::Request]
162162
# @api public
163-
def initialize(opts)
164-
@uri_normalizer = opts[:uri_normalizer] || HTTP::URI::NORMALIZER
165-
parse_verb_and_uri!(opts)
163+
def initialize(verb:, uri:, headers: nil, proxy: {}, body: nil, version: "1.1",
164+
uri_normalizer: nil)
165+
@uri_normalizer = uri_normalizer || HTTP::URI::NORMALIZER
166+
@verb = verb.to_s.downcase.to_sym
167+
parse_uri!(uri)
166168
validate_method_and_scheme!
167169

168-
@proxy = opts[:proxy] || {}
169-
@version = opts[:version] || "1.1"
170-
@headers = prepare_headers(opts[:headers])
171-
@body = prepare_body(opts[:body])
170+
@proxy = proxy
171+
@version = version
172+
@headers = prepare_headers(headers)
173+
@body = prepare_body(body)
172174
end
173175

174176
# Returns new Request with updated uri
@@ -329,20 +331,14 @@ def default_host_header_value
329331
value
330332
end
331333

332-
# Parse verb, URI, and scheme from options
334+
# Parse and normalize the URI, setting scheme
333335
# @return [void]
334336
# @api private
335-
def parse_verb_and_uri!(opts)
336-
@verb = opts.fetch(:verb).to_s.downcase.to_sym
337-
uri = opts.fetch(:uri)
338-
339-
begin
340-
@uri = @uri_normalizer.call(uri)
341-
rescue TypeError, Addressable::URI::InvalidURIError
342-
raise InvalidURIError, "invalid URI: #{uri.inspect}"
343-
end
344-
337+
def parse_uri!(uri)
338+
@uri = @uri_normalizer.call(uri)
345339
@scheme = @uri.scheme.to_s.downcase.to_sym if @uri.scheme
340+
rescue TypeError, Addressable::URI::InvalidURIError
341+
raise InvalidURIError, "invalid URI: #{uri.inspect}"
346342
end
347343

348344
# Validate HTTP method and URI scheme

0 commit comments

Comments
 (0)