Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/utilities/request.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# this is used to allow custom dispatches to `_http_request`
Comment thread
ericphanson marked this conversation as resolved.
Outdated
abstract type AbstractBackend end

"""
AWS.HTTPBackend <: AWS.AbstractBackend

An `HTTPBackend` can hold default `http_options::AbstractDict{Symbol,<:Any}`
to pass to HTTP.jl, which can be overwritten per-request by any `http_options`
supplied there.
"""
struct HTTPBackend{T<:AbstractDict{Symbol,<:Any}} <: AbstractBackend
http_options::T
end
Comment thread
ericphanson marked this conversation as resolved.
Outdated

HTTPBackend() = HTTPBackend(LittleDict{Symbol,String}())

const DEFAULT_BACKEND = Ref{Union{Nothing, AbstractBackend}}(HTTPBackend())
Comment thread
ericphanson marked this conversation as resolved.
Outdated

Base.@kwdef mutable struct Request
service::String
api_version::String
Expand All @@ -13,6 +31,7 @@ Base.@kwdef mutable struct Request
http_options::AbstractDict{Symbol,<:Any}=LittleDict{Symbol,String}()
return_raw::Bool=false
response_dict_type::Type{<:AbstractDict}=LittleDict
backend::AbstractBackend=DEFAULT_BACKEND[]
end


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

response = @mock _http_request(request)
response = @mock _http_request(request.backend, request)

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


function _http_request(request::Request)
function _http_request(http_backend::HTTPBackend, request::Request)
http_options = merge(http_backend.http_options, request.http_options)

@repeat 4 try
http_stack = HTTP.stack(redirect=false, retry=false, aws_authorization=false)

Expand All @@ -157,7 +178,7 @@ function _http_request(request::Request)
request.content;
require_ssl_verification=false,
response_stream=request.response_stream,
request.http_options...
http_options...
)
catch e
# Base.IOError is needed because HTTP.jl can often have errors that aren't
Expand Down
1 change: 1 addition & 0 deletions src/utilities/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function _extract_common_kw_args(service, args)
headers=LittleDict{String, String}(_pop!(args, "headers", [])),
http_options=_pop!(args, "http_options", LittleDict{Symbol, String}()),
response_dict_type=_pop!(args, "response_dict_type", LittleDict),
backend=_pop!(args, "backend", DEFAULT_BACKEND[]),
Comment thread
omus marked this conversation as resolved.
)
end

Expand Down
4 changes: 2 additions & 2 deletions test/patch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ end


function _aws_http_request_patch(response::HTTP.Messages.Response=_response())
return @patch AWS._http_request(request::Request) = response
return @patch AWS._http_request(::Any, request::Request) = response
Comment thread
ericphanson marked this conversation as resolved.
Outdated
end

_cred_file_patch = @patch function dot_aws_credentials_file()
Expand All @@ -73,7 +73,7 @@ _web_identity_patch = function (;
session_token="web_session_token",
role_arn="arn:aws:sts:::assumed-role/role-name",
)
@patch function AWS._http_request(request)
@patch function AWS._http_request(::AWS.HTTPBackend, request)
Comment thread
ericphanson marked this conversation as resolved.
Outdated
params = Dict(split.(split(request.content, '&'), '='))
creds = Dict(
"AccessKeyId" => access_key,
Expand Down