Skip to content

Commit 34b63c0

Browse files
authored
Try #401:
2 parents 786e82c + c008bf4 commit 34b63c0

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/utilities/request.jl

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
# this is used to allow custom dispatches to `_http_request`
2+
abstract type AbstractBackend end
3+
4+
"""
5+
AWS.HTTPBackend <: AWS.AbstractBackend
6+
7+
An `HTTPBackend` can hold default `http_options::AbstractDict{Symbol,<:Any}`
8+
to pass to HTTP.jl, which can be overwritten per-request by any `http_options`
9+
supplied there.
10+
"""
11+
struct HTTPBackend{T<:AbstractDict{Symbol,<:Any}} <: AbstractBackend
12+
http_options::T
13+
end
14+
15+
HTTPBackend() = HTTPBackend(LittleDict{Symbol,String}())
16+
17+
const DEFAULT_BACKEND = Ref{Union{Nothing, AbstractBackend}}(HTTPBackend())
18+
119
Base.@kwdef mutable struct Request
220
service::String
321
api_version::String
@@ -13,6 +31,7 @@ Base.@kwdef mutable struct Request
1331
http_options::AbstractDict{Symbol,<:Any}=LittleDict{Symbol,String}()
1432
return_raw::Bool=false
1533
response_dict_type::Type{<:AbstractDict}=LittleDict
34+
backend::AbstractBackend=DEFAULT_BACKEND[]
1635
end
1736

1837

@@ -54,7 +73,7 @@ function submit_request(aws::AbstractAWSConfig, request::Request; return_headers
5473
@repeat 3 try
5574
credentials(aws) === nothing || sign!(aws, request)
5675

57-
response = @mock _http_request(request)
76+
response = @mock _http_request(request.backend, request)
5877

5978
if response.status in REDIRECT_ERROR_CODES
6079
if HTTP.header(response, "Location") != ""
@@ -141,7 +160,9 @@ function submit_request(aws::AbstractAWSConfig, request::Request; return_headers
141160
end
142161

143162

144-
function _http_request(request::Request)
163+
function _http_request(http_backend::HTTPBackend, request::Request)
164+
http_options = merge(http_backend.http_options, request.http_options)
165+
145166
@repeat 4 try
146167
http_stack = HTTP.stack(redirect=false, retry=false, aws_authorization=false)
147168

@@ -157,7 +178,7 @@ function _http_request(request::Request)
157178
request.content;
158179
require_ssl_verification=false,
159180
response_stream=request.response_stream,
160-
request.http_options...
181+
http_options...
161182
)
162183
catch e
163184
# Base.IOError is needed because HTTP.jl can often have errors that aren't

src/utilities/utilities.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function _extract_common_kw_args(service, args)
6868
headers=LittleDict{String, String}(_pop!(args, "headers", [])),
6969
http_options=_pop!(args, "http_options", LittleDict{Symbol, String}()),
7070
response_dict_type=_pop!(args, "response_dict_type", LittleDict),
71+
backend=_pop!(args, "backend", DEFAULT_BACKEND[]),
7172
)
7273
end
7374

test/patch.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ end
5656

5757

5858
function _aws_http_request_patch(response::HTTP.Messages.Response=_response())
59-
return @patch AWS._http_request(request::Request) = response
59+
return @patch AWS._http_request(::Any, request::Request) = response
6060
end
6161

6262
_cred_file_patch = @patch function dot_aws_credentials_file()
@@ -73,7 +73,7 @@ _web_identity_patch = function (;
7373
session_token="web_session_token",
7474
role_arn="arn:aws:sts:::assumed-role/role-name",
7575
)
76-
@patch function AWS._http_request(request)
76+
@patch function AWS._http_request(::AWS.HTTPBackend, request)
7777
params = Dict(split.(split(request.content, '&'), '='))
7878
creds = Dict(
7979
"AccessKeyId" => access_key,

0 commit comments

Comments
 (0)