From 5b6965aae1cd5b62281d4de8289ae6ae6324c0af Mon Sep 17 00:00:00 2001 From: Zelda Hessler Date: Wed, 26 Jan 2022 12:09:31 -0600 Subject: [PATCH] Add conversion methods for `PresignedRequest` and update docs (#1116) * update: s3 presigning example with curl command generator add: impl into for PresignedRequest -> http::request::Builder sort: cargo deps * update: CHANGELOG.next.toml * appease: clippy * add: struct conversion doc links add: fn for PresignedRequest -> http::Request update: expand presigning example * update: changelog move: declaration of From for http::request::Builder * remove: leftover import --- CHANGELOG.next.toml | 6 +++++ .../aws-inlineable/src/presigning.rs | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/CHANGELOG.next.toml b/CHANGELOG.next.toml index f044289202..0610f69983 100644 --- a/CHANGELOG.next.toml +++ b/CHANGELOG.next.toml @@ -11,6 +11,12 @@ # meta = { "breaking" = false, "tada" = false, "bug" = false } # author = "rcoh" +[[aws-sdk-rust]] +message = "Added `impl Into for PresignedRequest` and a conversion method for turning `PresignedRequest`s into `http::Request`s." +references = ["aws-sdk-rust#423"] +meta = { "breaking" = false, "tada" = false, "bug" = false } +author = "Velfi" + [[aws-sdk-rust]] message = "Convert several `info` spans to `debug` in aws-config" references = ["smithy-rs#1087"] diff --git a/aws/rust-runtime/aws-inlineable/src/presigning.rs b/aws/rust-runtime/aws-inlineable/src/presigning.rs index 5a534eb1fd..6f88d4c34c 100644 --- a/aws/rust-runtime/aws-inlineable/src/presigning.rs +++ b/aws/rust-runtime/aws-inlineable/src/presigning.rs @@ -151,6 +151,11 @@ pub mod request { use std::fmt::{Debug, Formatter}; /// Represents a presigned request. This only includes the HTTP request method, URI, and headers. + /// + /// **This struct has conversion convenience functions:** + /// + /// - [`PresignedRequest::to_http_request`][Self::to_http_request] returns an [`http::Request`](https://docs.rs/http/0.2.6/http/request/struct.Request.html) + /// - [`PresignedRequest::into`](#impl-From) returns an [`http::request::Builder`](https://docs.rs/http/0.2.6/http/request/struct.Builder.html) #[non_exhaustive] pub struct PresignedRequest(http::Request<()>); @@ -175,6 +180,13 @@ pub mod request { pub fn headers(&self) -> &http::HeaderMap { self.0.headers() } + + /// Given a body, convert this `PresignedRequest` into an `http::Request` + pub fn to_http_request(self, body: B) -> Result, http::Error> { + let builder: http::request::Builder = self.into(); + + builder.body(body) + } } impl Debug for PresignedRequest { @@ -186,6 +198,20 @@ pub mod request { .finish() } } + + impl From for http::request::Builder { + fn from(req: PresignedRequest) -> Self { + let mut builder = http::request::Builder::new() + .uri(req.uri()) + .method(req.method()); + + if let Some(headers) = builder.headers_mut() { + *headers = req.headers().clone(); + } + + builder + } + } } /// Tower middleware service for creating presigned requests