Skip to content

pass Headers as part of prepare_request #872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
46 changes: 38 additions & 8 deletions sdk/core/src/headers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ pub trait Header {
}

/// A collection of headers
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Default)]
pub struct Headers(std::collections::HashMap<HeaderName, HeaderValue>);

impl Headers {
pub(crate) fn new() -> Self {
Self(Default::default())
pub fn new() -> Self {
Self::default()
}

/// Get a header value as a String or error if it is not found
Expand Down Expand Up @@ -140,6 +140,16 @@ impl Headers {
self.0.insert(key.into(), value.into());
}

/// Add headers to the headers collection
pub fn add<H>(&mut self, header: H)
where
H: AsHeaders,
{
for (key, value) in header.as_headers() {
self.insert(key, value);
}
}

/// Iterate over all the header name/value pairs
pub fn iter(&self) -> impl Iterator<Item = (&HeaderName, &HeaderValue)> {
self.0.iter()
Expand Down Expand Up @@ -171,20 +181,32 @@ impl HeaderName {
Self(std::borrow::Cow::Borrowed(s))
}

fn from_cow<C>(c: C) -> Self
where
C: Into<std::borrow::Cow<'static, str>>,
{
let c = c.into();
assert!(
c.chars().all(|c| c.is_lowercase() || !c.is_alphabetic()),
"header names must be lowercase: {c}"
);
Self(c)
}

pub fn as_str(&self) -> &str {
self.0.as_ref()
}
}

impl From<&'static str> for HeaderName {
fn from(s: &'static str) -> Self {
Self::from_static(s)
Self::from_cow(s)
}
}

impl From<String> for HeaderName {
fn from(s: String) -> Self {
Self(std::borrow::Cow::Owned(s))
Self::from_cow(s.to_lowercase())
}
}

Expand All @@ -197,26 +219,33 @@ impl HeaderValue {
Self(std::borrow::Cow::Borrowed(s))
}

fn from_cow<C>(c: C) -> Self
where
C: Into<std::borrow::Cow<'static, str>>,
{
Self(c.into())
}

pub fn as_str(&self) -> &str {
self.0.as_ref()
}
}

impl From<&'static str> for HeaderValue {
fn from(s: &'static str) -> Self {
Self::from_static(s)
Self::from_cow(s)
}
}

impl From<String> for HeaderValue {
fn from(s: String) -> Self {
Self(std::borrow::Cow::Owned(s))
Self::from_cow(s)
}
}

impl From<&String> for HeaderValue {
fn from(s: &String) -> Self {
Self(std::borrow::Cow::Owned(s.clone()))
s.clone().into()
}
}

Expand Down Expand Up @@ -295,6 +324,7 @@ pub const MS_RANGE: HeaderName = HeaderName::from_static("x-ms-range");
pub const NAMESPACE_ENABLED: HeaderName = HeaderName::from_static("x-ms-namespace-enabled");
pub const PAGE_WRITE: HeaderName = HeaderName::from_static("x-ms-page-write");
pub const PROPERTIES: HeaderName = HeaderName::from_static("x-ms-properties");
pub const PREFER: HeaderName = HeaderName::from_static("prefer");
pub const PROPOSED_LEASE_ID: HeaderName = HeaderName::from_static("x-ms-proposed-lease-id");
pub const RANGE: HeaderName = HeaderName::from_static("range");
pub const RANGE_GET_CONTENT_CRC64: HeaderName =
Expand Down
9 changes: 5 additions & 4 deletions sdk/data_tables/src/clients/entity_client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{prelude::*, requests::*};
use azure_core::Method;
use azure_core::{
error::{Error, ErrorKind},
Request,
headers::Headers,
Method, Request,
};
use bytes::Bytes;
use std::sync::Arc;
Expand Down Expand Up @@ -98,14 +98,15 @@ impl EntityClient {
self.partition_key_client.http_client()
}

pub(crate) fn prepare_request(
pub(crate) fn finalize_request(
&self,
url: Url,
method: Method,
headers: Headers,
request_body: Option<Bytes>,
) -> azure_core::Result<Request> {
self.partition_key_client
.prepare_request(url, method, request_body)
.finalize_request(url, method, headers, request_body)
}
}

Expand Down
11 changes: 5 additions & 6 deletions sdk/data_tables/src/clients/partition_key_client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::{prelude::*, requests::*};

use azure_core::Method;
use azure_core::Request;
use azure_core::Url;
use azure_core::{headers::Headers, Method, Request, Url};
use azure_storage::core::clients::StorageAccountClient;
use bytes::Bytes;
use std::sync::Arc;
Expand Down Expand Up @@ -54,13 +51,15 @@ impl PartitionKeyClient {
self.table_client.http_client()
}

pub(crate) fn prepare_request(
pub(crate) fn finalize_request(
&self,
url: Url,
method: Method,
headers: Headers,
request_body: Option<Bytes>,
) -> azure_core::Result<Request> {
self.table_client.prepare_request(url, method, request_body)
self.table_client
.finalize_request(url, method, headers, request_body)
}
}

Expand Down
9 changes: 4 additions & 5 deletions sdk/data_tables/src/clients/table_client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{clients::TableServiceClient, requests::*};
use azure_core::Method;
use azure_core::Request;
use azure_core::Url;
use azure_core::{headers::Headers, Method, Request, Url};
use azure_storage::core::clients::StorageAccountClient;
use bytes::Bytes;
use std::sync::Arc;
Expand Down Expand Up @@ -65,14 +63,15 @@ impl TableClient {
self.table_service_client.http_client()
}

pub(crate) fn prepare_request(
pub(crate) fn finalize_request(
&self,
url: Url,
method: Method,
headers: Headers,
request_body: Option<Bytes>,
) -> azure_core::Result<Request> {
self.table_service_client
.prepare_request(url, method, request_body)
.finalize_request(url, method, headers, request_body)
}
}

Expand Down
14 changes: 9 additions & 5 deletions sdk/data_tables/src/clients/table_service_client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::requests::ListTablesBuilder;
use azure_core::error::{ErrorKind, ResultExt};
use azure_core::Method;
use azure_core::Request;
use azure_core::{
error::{ErrorKind, ResultExt},
headers::Headers,
Method, Request,
};
use azure_storage::core::clients::{StorageAccountClient, StorageClient};
use bytes::Bytes;
use std::sync::Arc;
Expand Down Expand Up @@ -55,17 +57,19 @@ impl TableServiceClient {
self.storage_client.http_client()
}

pub(crate) fn prepare_request(
pub(crate) fn finalize_request(
&self,
url: Url,
method: Method,
headers: Headers,
request_body: Option<Bytes>,
) -> azure_core::Result<Request> {
self.storage_client
.storage_account_client()
.prepare_request(
.finalize_request(
url,
method,
headers,
azure_storage::core::clients::ServiceType::Table,
request_body,
)
Expand Down
16 changes: 9 additions & 7 deletions sdk/data_tables/src/requests/create_table_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{prelude::*, responses::*};
use azure_core::prelude::*;
use azure_core::Method;
use azure_core::{headers::*, prelude::*, Method};
use std::convert::TryInto;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -34,15 +33,18 @@ impl<'a> CreateTableBuilder<'a> {
table_name: self.table_client.table_name(),
})?;

let mut request = self.table_client.prepare_request(
let mut headers = Headers::new();
headers.add(self.client_request_id.clone());
headers.insert(ACCEPT, "application/json;odata=fullmetadata");
headers.insert(CONTENT_TYPE, "application/json");
headers.insert(PREFER, "return-content");

let request = self.table_client.finalize_request(
url,
Method::Post,
headers,
Some(bytes::Bytes::from(request_body_serialized)),
)?;
request.add_optional_header(&self.client_request_id);
request.insert_header("Accept", "application/json;odata=fullmetadata");
request.insert_header("Content-Type", "application/json");
request.insert_header("Prefer", "return-content");

let response = self
.table_client
Expand Down
18 changes: 9 additions & 9 deletions sdk/data_tables/src/requests/delete_entity_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use crate::{
responses::*,
TransactionOperation,
};
use azure_core::Method;
use azure_core::{prelude::*, Request};
use azure_core::{headers::*, prelude::*, Method, Request};
use std::convert::TryInto;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -36,11 +35,13 @@ impl<'a> DeleteEntityBuilder<'a> {

self.timeout.append_to_url_query(&mut url);

let mut request = self
let mut headers = Headers::new();
headers.add(self.client_request_id.clone());
headers.add(self.if_match.clone());

let request = self
.entity_client
.prepare_request(url, Method::Delete, None)?;
request.add_optional_header(&self.client_request_id);
request.add_mandatory_header(&self.if_match);
.finalize_request(url, Method::Delete, headers, None)?;

let response = self
.entity_client
Expand All @@ -56,9 +57,8 @@ impl<'a> DeleteEntityBuilder<'a> {

let mut request = Request::new(url.clone(), Method::Delete);
request.add_optional_header(&self.client_request_id);
request.insert_header("Accept", "application/json;odata=minimalmetadata");
request.insert_header("If-Match", "*");

request.insert_header(ACCEPT, "application/json;odata=minimalmetadata");
request.insert_header(IF_MATCH, "*");
request.set_body("");

Ok(TransactionOperation::new(request))
Expand Down
11 changes: 7 additions & 4 deletions sdk/data_tables/src/requests/delete_table_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{prelude::*, responses::*};
use azure_core::Method;
use azure_core::{
error::{Error, ErrorKind},
headers::*,
prelude::*,
};
use std::convert::TryInto;
Expand Down Expand Up @@ -31,11 +32,13 @@ impl<'a> DeleteTableBuilder<'a> {
.pop()
.push(&format!("Tables('{}')", self.table_client.table_name()));

let mut request = self
let mut headers = Headers::new();
headers.add(self.client_request_id.clone());
headers.insert(ACCEPT, "application/json");

let request = self
.table_client
.prepare_request(url, Method::Delete, None)?;
request.add_optional_header(&self.client_request_id);
request.insert_header("Accept", "application/json");
.finalize_request(url, Method::Delete, headers, None)?;

let response = self
.table_client
Expand Down
13 changes: 8 additions & 5 deletions sdk/data_tables/src/requests/get_entity_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{prelude::*, responses::*};
use azure_core::Method;
use azure_core::{error::Result, prelude::*, AppendToUrlQuery};
use azure_core::{error::Result, headers::*, prelude::*, AppendToUrlQuery, Method};
use serde::de::DeserializeOwned;
use std::convert::TryInto;

Expand Down Expand Up @@ -33,9 +32,13 @@ impl<'a> GetEntityBuilder<'a> {

self.select.append_to_url_query(&mut url);

let mut request = self.entity_client.prepare_request(url, Method::Get, None)?;
request.add_optional_header(&self.client_request_id);
request.insert_header("Accept", "application/json;odata=fullmetadata");
let mut headers = Headers::new();
headers.add(self.client_request_id.clone());
headers.insert(ACCEPT, "application/json;odata=fullmetadata");

let request = self
.entity_client
.finalize_request(url, Method::Get, headers, None)?;

let response = self
.entity_client
Expand Down
18 changes: 11 additions & 7 deletions sdk/data_tables/src/requests/insert_entity_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{prelude::*, responses::*, TransactionOperation};
use azure_core::Method;
use azure_core::{
error::{Error, ErrorKind},
headers::*,
prelude::*,
Request,
};
Expand Down Expand Up @@ -46,15 +47,18 @@ impl<'a> InsertEntityBuilder<'a> {

let request_body_serialized = serde_json::to_string(entity)?;

let mut request = self.table_client.prepare_request(
let mut headers = Headers::new();
headers.add(self.client_request_id.clone());
headers.add(self.return_entity.clone());
headers.insert(ACCEPT, "application/json;odata=fullmetadata");
headers.insert(CONTENT_TYPE, "application/json");

let request = self.table_client.finalize_request(
url,
Method::Post,
headers,
Some(bytes::Bytes::from(request_body_serialized)),
)?;
request.add_optional_header(&self.client_request_id);
request.add_mandatory_header(&self.return_entity);
request.insert_header("Accept", "application/json;odata=fullmetadata");
request.insert_header("Content-Type", "application/json");

let response = self
.table_client
Expand All @@ -80,8 +84,8 @@ impl<'a> InsertEntityBuilder<'a> {

let mut request = Request::new(url, Method::Post);
request.add_optional_header(&self.client_request_id);
request.insert_header("Accept", "application/json;odata=fullmetadata");
request.insert_header("Content-Type", "application/json");
request.insert_header(ACCEPT, "application/json;odata=fullmetadata");
request.insert_header(CONTENT_TYPE, "application/json");

request.set_body(serde_json::to_vec(entity)?);

Expand Down
Loading