Skip to content

Commit

Permalink
Cleaned up RestXML and Query responses (#165)
Browse files Browse the repository at this point in the history
* Cleaned up RestXML and Query responses

* Update test/AWS.jl

Co-authored-by: Curtis Vogt <curtis.vogt@gmail.com>

Co-authored-by: Curtis Vogt <curtis.vogt@gmail.com>
  • Loading branch information
mattBrzezinski and omus authored Sep 1, 2020
1 parent 0ef6b77 commit 14eb7ce
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
5 changes: 4 additions & 1 deletion src/AWS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,10 @@ function submit_request(aws::AWSConfig, request::Request; return_headers::Bool=f

if occursin(r"/xml", mime)
xml_dict_type = response_dict_type{Union{Symbol, String}, Any}
return (return_headers ? (xml_dict(body, xml_dict_type), response_dict_type(response.headers)) : xml_dict(body, xml_dict_type))
body = parse_xml(body)
root = XMLDict.root(body.x)

return (return_headers ? (xml_dict(root, xml_dict_type), response_dict_type(response.headers)) : xml_dict(root, xml_dict_type))
elseif occursin(r"/x-amz-json-1.[01]$", mime) || endswith(mime, "json")
info = isempty(response.body) ? nothing : JSON.parse(body, dicttype=response_dict_type)
return (return_headers ? (info, response_dict_type(response.headers)) : info)
Expand Down
2 changes: 1 addition & 1 deletion src/AWSConfig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ end

function _update_creds!(aws_config::AWSConfig)
r = AWSServices.sts("GetCallerIdentity"; aws_config=aws_config)
r = r["GetCallerIdentityResponse"]["GetCallerIdentityResult"]
r = r["GetCallerIdentityResult"]
creds = aws_config.credentials

creds.user_arn = r["Arn"]
Expand Down
43 changes: 24 additions & 19 deletions test/AWS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ end
@testset "submit_request" begin
aws = AWS.AWSConfig()

function _expected_body(body::AbstractString, dict_type::Type)
parsed = parse_xml(body)
return xml_dict(XMLDict.root(parsed.x), dict_type)
end

@testset "HEAD request method" begin
expected_result_type = LittleDict

Expand Down Expand Up @@ -182,9 +187,9 @@ end
end

@testset "text/xml" begin
expected_headers = Pair["Content-Type"=>"",]
expected_headers = Pair["Content-Type"=>"text/xml",]
expected_body_type = LittleDict{Union{Symbol, String}, Any}
expected_body = xml_dict((Patches.body), expected_body_type)
expected_body = _expected_body(Patches.body, expected_body_type)

expected_header_type = LittleDict{SubString{String}, SubString{String}}

Expand Down Expand Up @@ -219,7 +224,7 @@ end
)

expected_body_type = LittleDict{Union{Symbol, String}, Any}
expected_body = xml_dict((Patches.body), expected_body_type)
expected_body = _expected_body(Patches.body, expected_body_type)
expected_headers = Pair["Content-Type"=>"application/xml",]

apply(Patches._aws_http_request_patch) do
Expand Down Expand Up @@ -501,11 +506,11 @@ end
expected_policy_document = JSON.json(expected_policy_document)

response = IAM.create_policy(expected_policy_document, expected_policy_name)
policy_arn = policy_arn = response["CreatePolicyResponse"]["CreatePolicyResult"]["Policy"]["Arn"]
policy_arn = response["CreatePolicyResult"]["Policy"]["Arn"]

try
response_policy_version = IAM.get_policy_version(policy_arn, "v1")
response_document = response_policy_version["GetPolicyVersionResponse"]["GetPolicyVersionResult"]["PolicyVersion"]["Document"]
response_document = response_policy_version["GetPolicyVersionResult"]["PolicyVersion"]["Document"]
@test HTTP.unescapeuri(response_document) == expected_policy_document
finally
IAM.delete_policy(policy_arn)
Expand All @@ -530,11 +535,11 @@ end
expected_policy_document = JSON.json(expected_policy_document)

response = AWSServices.iam("CreatePolicy", LittleDict("PolicyName"=>expected_policy_name, "PolicyDocument"=>expected_policy_document))
policy_arn = response["CreatePolicyResponse"]["CreatePolicyResult"]["Policy"]["Arn"]
policy_arn = response["CreatePolicyResult"]["Policy"]["Arn"]

try
response_policy_version = AWSServices.iam("GetPolicyVersion", LittleDict("PolicyArn"=>policy_arn, "VersionId"=>"v1"))
response_document = response_policy_version["GetPolicyVersionResponse"]["GetPolicyVersionResult"]["PolicyVersion"]["Document"]
response_document = response_policy_version["GetPolicyVersionResult"]["PolicyVersion"]["Document"]
@test HTTP.unescapeuri(response_document) == expected_policy_document
finally
AWSServices.iam("DeletePolicy", LittleDict("PolicyArn"=>policy_arn))
Expand All @@ -552,7 +557,7 @@ end
function _get_queue_url(queue_name)
result = SQS.get_queue_url(queue_name)

return result["GetQueueUrlResponse"]["GetQueueUrlResult"]["QueueUrl"]
return result["GetQueueUrlResult"]["QueueUrl"]
end

# Create Queue
Expand All @@ -569,7 +574,7 @@ end
SQS.send_message(expected_message, queue_url)

response = SQS.receive_message(queue_url)
receipt_handle = response["ReceiveMessageResponse"]["ReceiveMessageResult"]["Message"]["ReceiptHandle"]
receipt_handle = response["ReceiveMessageResult"]["Message"]["ReceiptHandle"]

response = SQS.delete_message_batch(
[
Expand All @@ -581,13 +586,13 @@ end
queue_url,
)

message_id = response["DeleteMessageBatchResponse"]["DeleteMessageBatchResult"]["DeleteMessageBatchResultEntry"]["Id"]
message_id = response["DeleteMessageBatchResult"]["DeleteMessageBatchResultEntry"]["Id"]
@test message_id == expected_message_id

SQS.send_message(expected_message, queue_url)

result = SQS.receive_message(queue_url)
message = result["ReceiveMessageResponse"]["ReceiveMessageResult"]["Message"]["Body"]
message = result["ReceiveMessageResult"]["Message"]["Body"]
@test message == expected_message
finally
SQS.delete_queue(queue_url)
Expand All @@ -603,7 +608,7 @@ end
function _get_queue_url(queue_name)
result = AWSServices.sqs("GetQueueUrl", LittleDict("QueueName" => queue_name))

return result["GetQueueUrlResponse"]["GetQueueUrlResult"]["QueueUrl"]
return result["GetQueueUrlResult"]["QueueUrl"]
end

# Create Queue
Expand All @@ -623,7 +628,7 @@ end
)

response = AWSServices.sqs("ReceiveMessage", LittleDict("QueueUrl" => queue_url,))
receipt_handle = response["ReceiveMessageResponse"]["ReceiveMessageResult"]["Message"]["ReceiptHandle"]
receipt_handle = response["ReceiveMessageResult"]["Message"]["ReceiptHandle"]

response = AWSServices.sqs("DeleteMessageBatch", LittleDict(
"QueueUrl" => queue_url,
Expand All @@ -636,7 +641,7 @@ end
)
)

message_id = response["DeleteMessageBatchResponse"]["DeleteMessageBatchResult"]["DeleteMessageBatchResultEntry"]["Id"]
message_id = response["DeleteMessageBatchResult"]["DeleteMessageBatchResultEntry"]["Id"]
@test message_id == expected_message_id

# Send message
Expand All @@ -648,7 +653,7 @@ end

# Receive Message
result = AWSServices.sqs("ReceiveMessage", LittleDict("QueueUrl" => queue_url))
message = result["ReceiveMessageResponse"]["ReceiveMessageResult"]["Message"]["Body"]
message = result["ReceiveMessageResult"]["Message"]["Body"]
@test message == expected_message
finally
AWSServices.sqs("DeleteQueue", LittleDict("QueueUrl" => queue_url))
Expand Down Expand Up @@ -693,12 +698,12 @@ end

# GET operation
result = S3.list_objects(bucket_name)
@test result["ListBucketResult"]["Contents"]["Key"] == file_name
@test result["Contents"]["Key"] == file_name

# GET with parameters operation
max_keys = 1
result = S3.list_objects(bucket_name, Dict("max_keys" => max_keys))
@test length([result["ListBucketResult"]["Contents"]]) == max_keys
@test length([result["Contents"]]) == max_keys
finally
# DELETE with parameters operation
S3.delete_object(bucket_name, file_name)
Expand Down Expand Up @@ -745,12 +750,12 @@ end

# GET operation
result = AWSServices.s3("GET", "/$bucket_name")
@test result["ListBucketResult"]["Contents"]["Key"] == file_name
@test result["Contents"]["Key"] == file_name

# GET with parameters operation
max_keys = 1
result = AWSServices.s3("GET", "/$bucket_name", Dict("max_keys" => max_keys))
@test length([result["ListBucketResult"]["Contents"]]) == max_keys
@test length([result["Contents"]]) == max_keys

# POST with parameters operation
body = """
Expand Down

2 comments on commit 14eb7ce

@mattBrzezinski
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/20506

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.0 -m "<description of version>" 14eb7ce642c1ae4b72feadc605c2f3974a5798d0
git push origin v1.0.0

Please sign in to comment.