1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
pub mod common;
pub mod credentials_module;
pub mod types;
pub mod versions_module;
mod client;
mod context;
mod error;
mod party;
mod party_store;
pub use {
client::Client,
common::*,
context::Context,
credentials_module::CredentialsModule,
error::{ClientError, Error, HubError, ServerError},
party::Party,
party_store::PartyStore,
versions_module::VersionsModule,
};
use {std::borrow::Cow, url::Url};
#[cfg(feature = "warp")]
pub mod warp {
pub use super::context::warp_extensions::*;
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(serde::Serialize)]
pub struct Response<T>
where
T: serde::Serialize,
{
#[serde(skip)]
pub http_status: http::StatusCode,
#[serde(rename = "status_code")]
pub code: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<T>,
#[serde(rename = "status_message", skip_serializing_if = "Option::is_none")]
pub message: Option<Cow<'static, str>>,
pub timestamp: types::DateTime,
}
impl<T> Response<T>
where
T: serde::Serialize,
{
pub fn into_http<B>(self) -> http::Response<B>
where
B: From<Vec<u8>>,
{
let http_status = self.http_status;
let body = serde_json::to_vec(&self).expect("Serializing reply");
http::Response::builder()
.status(http_status)
.header("content-type", "application/json")
.body(body.into())
.expect("Creating OCPI Response")
}
pub fn from_err(err: Error) -> Self {
Self {
http_status: err.http_status_code(),
code: err.code(),
data: None,
message: Some(Cow::Owned(err.to_string())),
timestamp: types::DateTime::now(),
}
}
}
impl<T> From<Result<T>> for Response<T>
where
T: serde::Serialize,
{
fn from(res: Result<T>) -> Self {
match res {
Ok(data) => Response {
http_status: http::StatusCode::OK,
code: 1000,
data: Some(data),
message: Some(Cow::Borrowed("Success")),
timestamp: types::DateTime::now(),
},
Err(err) => Response::from_err(err),
}
}
}
#[derive(Clone)]
pub struct Cpo<DB>
where
DB: Store,
{
base_url: Url,
db: DB,
client: Client,
}
impl<DB> Cpo<DB>
where
DB: Store,
{
pub fn new(base_url: Url, db: DB, client: Client) -> Self {
if base_url.cannot_be_a_base() {
panic!("Invalid base Url `{}`", base_url);
}
Self {
base_url,
db,
client,
}
}
pub fn base_url(&self) -> Url {
self.base_url.clone()
}
pub(crate) fn url_path(&self, path: &[impl AsRef<str>]) -> types::Url {
let mut url = self.base_url.clone();
url.path_segments_mut()
.unwrap()
.extend(path);
url
}
}
pub enum Authorized<P, R> {
Party(P),
Registration(R),
}
impl<P, R> Authorized<P, R>
where
P: Party,
{
fn party(self) -> Result<P> {
match self {
Self::Party(party) => Ok(party),
_ => Err(Error::unauthorized("Invalid Token")),
}
}
fn registration(self) -> Result<R> {
match self {
Self::Registration(temp) => Ok(temp),
_ => Err(Error::unauthorized("Invalid Token")),
}
}
}
#[async_trait::async_trait]
pub trait Store
where
Self: Clone + Send + Sync + 'static,
{
type PartyModel: Party;
type RegistrationModel;
async fn get_authorized(
&self,
token: types::CredentialsToken,
) -> Result<Authorized<Self::PartyModel, Self::RegistrationModel>>;
}