Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
make openadr-vtn a dev dependency of openadr-client
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Aug 19, 2024
1 parent 8125b16 commit 7ee3f8e
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ thiserror = "1.0.61"
validator = {version = "0.18.1", features = ["derive"] }
uuid = { version = "1.8.0", features = ["v4"] }
url = "2.5.0"
http = "^1.0.0"
mime = "0.3"
tower-http = { version = "0.5.2" , features = ["trace"]}
http-body-util = "0.1.0"
Expand Down
1 change: 0 additions & 1 deletion openadr-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ rust-version.workspace = true

[dependencies]
openadr-wire.workspace = true
openadr-vtn.workspace = true

serde.workspace = true
serde_json.workspace = true
Expand Down
6 changes: 3 additions & 3 deletions openadr-client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub enum Error {
Reqwest(reqwest::Error),
Serde(serde_json::Error),
UrlParseError(url::ParseError),
Problem(openadr_vtn::Problem),
Problem(openadr_wire::problem::Problem),
AuthProblem(openadr_wire::oauth::OAuthError),
OAuthTokenNotBearer,
ObjectNotFound,
Expand All @@ -31,8 +31,8 @@ impl From<url::ParseError> for Error {
}
}

impl From<openadr_vtn::Problem> for Error {
fn from(err: openadr_vtn::Problem) -> Self {
impl From<openadr_wire::problem::Problem> for Error {
fn from(err: openadr_wire::problem::Problem) -> Self {
Error::Problem(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion openadr-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl ReqwestClientRef {

// handle any errors returned by the server
if !res.status().is_success() {
let problem = res.json::<openadr_vtn::Problem>().await?;
let problem = res.json::<openadr_wire::problem::Problem>().await?;
return Err(crate::error::Error::from(problem));
}

Expand Down
72 changes: 10 additions & 62 deletions openadr-vtn/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;
use axum_extra::extract::QueryRejection;
use openadr_wire::problem::Problem;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use tracing::{error, trace, warn};
use uuid::Uuid;

Expand All @@ -28,8 +28,8 @@ pub enum AppError {
Auth(String),
}

impl IntoResponse for AppError {
fn into_response(self) -> Response {
impl AppError {
fn into_problem(self) -> Problem {
let reference = Uuid::new_v4();

match self {
Expand Down Expand Up @@ -134,7 +134,13 @@ impl IntoResponse for AppError {
}
}
}
.into_response()
}
}

impl IntoResponse for AppError {
fn into_response(self) -> Response {
let problem = self.into_problem();
(problem.status, Json(problem)).into_response()
}
}

Expand All @@ -146,61 +152,3 @@ impl Default for ProblemUri {
Self("about:blank".to_string())
}
}

/// Reusable error response. From <https://opensource.zalando.com/problem/schema.yaml>.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
#[skip_serializing_none]
#[serde(rename_all = "camelCase")]
pub struct Problem {
/// An absolute URI that identifies the problem type.
/// When dereferenced, it SHOULD provide human-readable documentation for the problem type
/// (e.g., using HTML).
#[serde(default)]
pub r#type: ProblemUri,
/// A short, summary of the problem type.
/// Written in english and readable for engineers
/// (usually not suited for non-technical stakeholders and not localized);
/// example: Service Unavailable.
pub title: Option<String>,
/// The HTTP status code generated by the origin server for this occurrence of the problem.
#[serde(with = "status_code_serialization")]
pub status: StatusCode,
/// A human-readable explanation specific to this occurrence of the problem.
pub detail: Option<String>,
/// An absolute URI that identifies the specific occurrence of the problem.
/// It may or may not yield further information if dereferenced.
pub instance: Option<String>,
}

mod status_code_serialization {
use reqwest::StatusCode;
use serde::de::Unexpected;
use serde::{Deserialize, Deserializer, Serializer};

pub fn serialize<S>(code: &StatusCode, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u16(code.as_u16())
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<StatusCode, D::Error>
where
D: Deserializer<'de>,
{
u16::deserialize(deserializer).and_then(|code| {
StatusCode::from_u16(code).map_err(|_| {
serde::de::Error::invalid_value(
Unexpected::Unsigned(code as u64),
&"Valid http status code",
)
})
})
}
}

impl IntoResponse for Problem {
fn into_response(self) -> Response {
(self.status, Json(self)).into_response()
}
}
2 changes: 0 additions & 2 deletions openadr-vtn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ pub mod data_source;
mod error;
pub mod jwt;
pub mod state;

pub use error::Problem;
1 change: 1 addition & 0 deletions openadr-wire/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ serde_with.workspace = true
uuid.workspace = true
iso8601-duration.workspace = true
thiserror.workspace = true
http.workspace = true

[dev-dependencies]
serde_json.workspace = true
Expand Down
1 change: 1 addition & 0 deletions openadr-wire/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub use report::Report;
pub mod event;
pub mod interval;
pub mod oauth;
pub mod problem;
pub mod program;
pub mod report;
pub mod target;
Expand Down
65 changes: 65 additions & 0 deletions openadr-wire/src/problem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use http::StatusCode;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct ProblemUri(String);

impl Default for ProblemUri {
fn default() -> Self {
Self("about:blank".to_string())
}
}

/// Reusable error response. From <https://opensource.zalando.com/problem/schema.yaml>.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
#[skip_serializing_none]
#[serde(rename_all = "camelCase")]
pub struct Problem {
/// An absolute URI that identifies the problem type.
/// When dereferenced, it SHOULD provide human-readable documentation for the problem type
/// (e.g., using HTML).
#[serde(default)]
pub r#type: ProblemUri,
/// A short, summary of the problem type.
/// Written in english and readable for engineers
/// (usually not suited for non-technical stakeholders and not localized);
/// example: Service Unavailable.
pub title: Option<String>,
/// The HTTP status code generated by the origin server for this occurrence of the problem.
#[serde(with = "status_code_serialization")]
pub status: StatusCode,
/// A human-readable explanation specific to this occurrence of the problem.
pub detail: Option<String>,
/// An absolute URI that identifies the specific occurrence of the problem.
/// It may or may not yield further information if dereferenced.
pub instance: Option<String>,
}

mod status_code_serialization {
use super::*;

use serde::de::Unexpected;
use serde::{Deserializer, Serializer};

pub fn serialize<S>(code: &StatusCode, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u16(code.as_u16())
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<StatusCode, D::Error>
where
D: Deserializer<'de>,
{
u16::deserialize(deserializer).and_then(|code| {
StatusCode::from_u16(code).map_err(|_| {
serde::de::Error::invalid_value(
Unexpected::Unsigned(code as u64),
&"Valid http status code",
)
})
})
}
}

0 comments on commit 7ee3f8e

Please sign in to comment.