Skip to content

Commit

Permalink
Add conversion methods for PresignedRequest and update docs (#1116)
Browse files Browse the repository at this point in the history
* 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<B>
update: expand presigning example

* update: changelog
move: declaration of From<PresignedRequest> for http::request::Builder

* remove: leftover import
  • Loading branch information
Velfi authored Jan 26, 2022
1 parent 32923a3 commit 5b6965a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
# meta = { "breaking" = false, "tada" = false, "bug" = false }
# author = "rcoh"

[[aws-sdk-rust]]
message = "Added `impl Into<http::request::Builder> 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"]
Expand Down
26 changes: 26 additions & 0 deletions aws/rust-runtime/aws-inlineable/src/presigning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<B>`][Self::to_http_request] returns an [`http::Request<B>`](https://docs.rs/http/0.2.6/http/request/struct.Request.html)
/// - [`PresignedRequest::into`](#impl-From<PresignedRequest>) returns an [`http::request::Builder`](https://docs.rs/http/0.2.6/http/request/struct.Builder.html)
#[non_exhaustive]
pub struct PresignedRequest(http::Request<()>);

Expand All @@ -175,6 +180,13 @@ pub mod request {
pub fn headers(&self) -> &http::HeaderMap<http::HeaderValue> {
self.0.headers()
}

/// Given a body, convert this `PresignedRequest` into an `http::Request`
pub fn to_http_request<B>(self, body: B) -> Result<http::Request<B>, http::Error> {
let builder: http::request::Builder = self.into();

builder.body(body)
}
}

impl Debug for PresignedRequest {
Expand All @@ -186,6 +198,20 @@ pub mod request {
.finish()
}
}

impl From<PresignedRequest> 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
Expand Down

0 comments on commit 5b6965a

Please sign in to comment.