Skip to content
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

Standardize on json & xml helpers #1505

Merged
merged 4 commits into from
Dec 8, 2023
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
8 changes: 4 additions & 4 deletions sdk/core/src/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ pub fn diff(first: OffsetDateTime, second: OffsetDateTime) -> Duration {
#[cfg(test)]
mod tests {
use super::*;
use crate::from_json;
use serde::{Deserialize, Serialize};
use serde_json;
use time::macros::datetime;

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -207,7 +207,7 @@ mod tests {
"created_time": "2021-07-01T10:45:02Z"
}"#;

let state: ExampleState = serde_json::from_str(json_state).unwrap();
let state: ExampleState = from_json(json_state)?;

assert_eq!(parse_rfc3339("2021-07-01T10:45:02Z")?, state.created_time);
assert_eq!(state.deleted_time, None);
Expand All @@ -222,12 +222,12 @@ mod tests {
"deleted_time": "2022-03-28T11:05:31Z"
}"#;

let state: ExampleState = serde_json::from_str(json_state).unwrap();
let state: ExampleState = from_json(json_state)?;

assert_eq!(parse_rfc3339("2021-07-01T10:45:02Z")?, state.created_time);
assert_eq!(
state.deleted_time,
Some(parse_rfc3339("2022-03-28T11:05:31Z").unwrap())
Some(parse_rfc3339("2022-03-28T11:05:31Z")?)
);

Ok(())
Expand Down
30 changes: 19 additions & 11 deletions sdk/core/src/error/http_error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{headers, Response, StatusCode};
use crate::{from_json, headers, Response, StatusCode};
use bytes::Bytes;
use serde::Deserialize;
use std::collections::HashMap;

/// An unsuccessful HTTP response
Expand Down Expand Up @@ -103,24 +104,31 @@ fn get_error_code_from_header(headers: &HashMap<String, String>) -> Option<Strin
headers.get(headers::ERROR_CODE.as_str()).cloned()
}

#[derive(Deserialize)]
struct NestedError {
message: Option<String>,
code: Option<String>,
}

#[derive(Deserialize)]
struct ErrorBody {
error: Option<NestedError>,
message: Option<String>,
code: Option<String>,
}

/// Gets the error code if it's present in the body
///
/// For more info, see [here](https://github.com/microsoft/api-guidelines/blob/vNext/azure/Guidelines.md#handling-errors)
pub(crate) fn get_error_code_from_body(body: &[u8]) -> Option<String> {
let json = serde_json::from_slice::<serde_json::Value>(body).ok()?;
let nested = || json.get("error")?.get("code")?.as_str();
let top_level = || json.get("code")?.as_str();
let code = nested().or_else(top_level);
code.map(ToOwned::to_owned)
let decoded: ErrorBody = from_json(body).ok()?;
decoded.error.and_then(|e| e.code).or(decoded.code)
}

/// Gets the error message if it's present in the body
///
/// For more info, see [here](https://github.com/microsoft/api-guidelines/blob/vNext/azure/Guidelines.md#handling-errors)
pub(crate) fn get_error_message_from_body(body: &[u8]) -> Option<String> {
let json = serde_json::from_slice::<serde_json::Value>(body).ok()?;
let nested = || json.get("error")?.get("message")?.as_str();
let top_level = || json.get("message")?.as_str();
let code = nested().or_else(top_level);
code.map(ToOwned::to_owned)
let decoded: ErrorBody = from_json(body).ok()?;
decoded.error.and_then(|e| e.message).or(decoded.message)
}
11 changes: 10 additions & 1 deletion sdk/core/src/http_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use self::reqwest::new_reqwest_client;
use crate::error::ErrorKind;
use async_trait::async_trait;
use bytes::Bytes;
use serde::Serialize;
use serde::{de::DeserializeOwned, Serialize};
use std::sync::Arc;

/// Construct a new `HttpClient`
Expand Down Expand Up @@ -61,3 +61,12 @@ where
{
Ok(Bytes::from(serde_json::to_vec(value)?))
}

/// Reads the XML from bytes.
pub fn from_json<S, T>(body: S) -> crate::Result<T>
where
S: AsRef<[u8]>,
T: DeserializeOwned,
{
serde_json::from_slice(body.as_ref()).map_err(|e| e.into())
}
2 changes: 1 addition & 1 deletion sdk/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub use context::Context;
pub use error::{Error, Result};
#[doc(inline)]
pub use headers::Header;
pub use http_client::{new_http_client, to_json, HttpClient};
pub use http_client::{from_json, new_http_client, to_json, HttpClient};
pub use models::*;
pub use options::*;
pub use pageable::*;
Expand Down
31 changes: 22 additions & 9 deletions sdk/core/src/lro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub fn get_retry_after(headers: &Headers) -> Duration {

pub mod location {
use crate::{
from_json,
headers::{Headers, AZURE_ASYNCOPERATION, LOCATION, OPERATION_LOCATION},
lro::LroStatus,
Url,
Expand All @@ -80,15 +81,18 @@ pub mod location {
}

pub fn get_provisioning_state(body: &[u8]) -> Option<LroStatus> {
let body: serde_json::Value = serde_json::from_slice(body).ok()?;
let provisioning_state = body["status"].as_str()?;
Some(LroStatus::from(provisioning_state))
#[derive(serde::Deserialize)]
struct Body {
status: String,
}
let body: Body = from_json(body).ok()?;
Some(LroStatus::from(body.status.as_str()))
}
}

pub mod body_content {
use crate::{lro::LroStatus, StatusCode};
use serde::Serialize;
use crate::{from_json, lro::LroStatus, to_json, StatusCode};
use serde::{Deserialize, Serialize};

/// Extract the provisioning state based on the status code and response body
///
Expand All @@ -115,13 +119,22 @@ pub mod body_content {
}
}

#[derive(Deserialize)]
#[serde(rename_all = "snake_case")]
struct Properties {
provisioning_state: String,
}

#[derive(Deserialize)]
struct Body {
properties: Properties,
}

fn get_provisioning_state_from_body<S>(body: &S) -> Option<LroStatus>
where
S: Serialize,
{
let body: serde_json::Value =
serde_json::from_str(&serde_json::to_string(body).ok()?).ok()?;
let provisioning_state = body["properties"]["provisioningState"].as_str()?;
Some(LroStatus::from(provisioning_state))
let body: Body = from_json(to_json(&body).ok()?).ok()?;
Some(LroStatus::from(body.properties.provisioning_state.as_str()))
}
}
11 changes: 10 additions & 1 deletion sdk/core/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
use crate::SeekableStream;
use crate::{
headers::{AsHeaders, Headers},
Method, Url,
to_json, Method, Url,
};
use bytes::Bytes;
use serde::Serialize;
use std::fmt::Debug;

/// An HTTP Body.
Expand Down Expand Up @@ -116,6 +117,14 @@ impl Request {
&self.body
}

pub fn set_json<T>(&mut self, data: &T) -> crate::Result<()>
where
T: ?Sized + Serialize,
{
self.set_body(to_json(data)?);
Ok(())
}

pub fn set_body(&mut self, body: impl Into<Body>) {
self.body = body.into();
}
Expand Down
66 changes: 61 additions & 5 deletions sdk/core/src/response.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::error::{ErrorKind, ResultExt};
use crate::headers::Headers;
use crate::StatusCode;
use crate::{
error::{ErrorKind, ResultExt},
from_json,
headers::Headers,
StatusCode,
};
use bytes::Bytes;
use futures::{Stream, StreamExt};
use std::fmt::Debug;
use std::pin::Pin;
use serde::de::DeserializeOwned;
use std::{fmt::Debug, pin::Pin};

#[cfg(not(target_arch = "wasm32"))]
pub(crate) type PinnedStream = Pin<Box<dyn Stream<Item = crate::Result<Bytes>> + Send + Sync>>;
Expand Down Expand Up @@ -46,6 +49,21 @@ impl Response {
pub fn into_body(self) -> ResponseBody {
self.body
}

pub async fn json<T>(self) -> crate::Result<T>
where
T: DeserializeOwned,
{
self.into_body().json().await
}

#[cfg(feature = "xml")]
pub async fn xml<T>(self) -> crate::Result<T>
where
T: DeserializeOwned,
{
self.into_body().xml().await
}
}

impl std::fmt::Debug for Response {
Expand All @@ -66,6 +84,12 @@ pub struct CollectedResponse {
body: Bytes,
}

impl AsRef<[u8]> for CollectedResponse {
fn as_ref(&self) -> &[u8] {
self.body.as_ref()
}
}

impl CollectedResponse {
/// Create a new instance
pub fn new(status: StatusCode, headers: Headers, body: Bytes) -> Self {
Expand Down Expand Up @@ -97,6 +121,21 @@ impl CollectedResponse {
let body = body.collect().await?;
Ok(Self::new(status, headers, body))
}

pub fn json<T>(&self) -> crate::Result<T>
where
T: DeserializeOwned,
{
from_json(&self.body)
}

#[cfg(feature = "xml")]
pub async fn xml<T>(&self) -> crate::Result<T>
where
T: DeserializeOwned,
{
crate::xml::read_xml(&self.body)
}
}

/// A response body stream
Expand Down Expand Up @@ -130,6 +169,23 @@ impl ResponseBody {
)
.map(ToOwned::to_owned)
}

pub async fn json<T>(self) -> crate::Result<T>
where
T: DeserializeOwned,
{
let body = self.collect().await?;
from_json(body)
}

#[cfg(feature = "xml")]
pub async fn xml<T>(self) -> crate::Result<T>
where
T: DeserializeOwned,
{
let body = self.collect().await?;
crate::xml::read_xml(&body)
}
}

impl Stream for ResponseBody {
Expand Down
5 changes: 3 additions & 2 deletions sdk/core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::from_json;
use serde::Serialize;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
Expand All @@ -54,15 +55,15 @@ mod tests {
#[test]
fn deserialize_empty() -> crate::Result<()> {
let bytes = br#"{}"#;
let site_config: SiteConfig = serde_json::from_slice(bytes)?;
let site_config: SiteConfig = from_json(bytes)?;
assert_eq!(Vec::<NameValuePair>::default(), site_config.app_settings);
Ok(())
}

#[test]
fn deserialize_null() -> crate::Result<()> {
let bytes = br#"{ "appSettings": null }"#;
let site_config: SiteConfig = serde_json::from_slice(bytes)?;
let site_config: SiteConfig = from_json(bytes)?;
assert_eq!(Vec::<NameValuePair>::default(), site_config.app_settings);
Ok(())
}
Expand Down
7 changes: 4 additions & 3 deletions sdk/core/src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ mod test {
}

#[test]
fn reading_xml() {
fn reading_xml() -> crate::Result<()> {
#[derive(Deserialize, PartialEq, Debug)]
#[serde(rename = "Foo")]
struct Test {
Expand All @@ -86,15 +86,16 @@ mod test {
x: "Hello, world!".into(),
};
let xml = br#"<?xml version="1.0" encoding="utf-8"?><Foo><x>Hello, world!</x></Foo>"#;
assert_eq!(test, read_xml(xml).unwrap());
assert_eq!(test, read_xml(xml)?);

let error = read_xml::<Test>(&xml[..xml.len() - 2]).unwrap_err();
assert!(format!("{error}").contains("reading_xml::Test"));

let xml = r#"<?xml version="1.0" encoding="utf-8"?><Foo><x>Hello, world!</x></Foo>"#;
assert_eq!(test, read_xml_str(xml).unwrap());
assert_eq!(test, read_xml_str(xml)?);

let error = read_xml_str::<Test>(&xml[..xml.len() - 2]).unwrap_err();
assert!(format!("{error}").contains("reading_xml::Test"));
Ok(())
}
}
20 changes: 11 additions & 9 deletions sdk/data_cosmos/src/operations/create_collection.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::headers::from_headers::*;
use crate::prelude::*;
use crate::resources::collection::{IndexingPolicy, PartitionKey};
use azure_core::headers::{etag_from_headers, session_token_from_headers};
use azure_core::Response as HttpResponse;
use crate::{
headers::from_headers::*,
prelude::*,
resources::collection::{IndexingPolicy, PartitionKey},
};
use azure_core::{
headers::{etag_from_headers, session_token_from_headers},
Response as HttpResponse,
};
use time::OffsetDateTime;

operation! {
Expand Down Expand Up @@ -40,7 +44,7 @@ impl CreateCollectionBuilder {
partition_key: &self.partition_key,
};

request.set_body(serde_json::to_vec(&collection)?);
request.set_json(&collection)?;

let response = self
.client
Expand Down Expand Up @@ -73,10 +77,8 @@ pub struct CreateCollectionResponse {
impl CreateCollectionResponse {
pub async fn try_from(response: HttpResponse) -> azure_core::Result<Self> {
let (_status_code, headers, body) = response.deconstruct();
let body = body.collect().await?;

Ok(Self {
collection: serde_json::from_slice(&body)?,
collection: body.json().await?,
charge: request_charge_from_headers(&headers)?,
activity_id: activity_id_from_headers(&headers)?,
etag: etag_from_headers(&headers)?,
Expand Down
Loading