use core::future::Future;
use crate::transport::{OcpiError, Page, PageQuery, Patch};
use crate::types::PartyRef;
use super::extract::RequestContext;
pub type Handled<T> = Result<T, OcpiError>;
pub trait LocationsSender: Send + Sync + 'static {
fn list(
&self,
query: PageQuery,
context: RequestContext,
) -> impl Future<Output = Handled<Page<crate::v2_3_0::locations::Location>>> + Send;
fn location(
&self,
location_id: String,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::locations::Location>> + Send;
fn evse(
&self,
location_id: String,
evse_uid: String,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::locations::Evse>> + Send;
fn connector(
&self,
location_id: String,
evse_uid: String,
connector_id: String,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::locations::Connector>> + Send;
}
pub trait LocationsReceiver: Send + Sync + 'static {
fn location(
&self,
owner: PartyRef,
location_id: String,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::locations::Location>> + Send;
fn put_location(
&self,
owner: PartyRef,
location: crate::v2_3_0::locations::Location,
context: RequestContext,
) -> impl Future<Output = Handled<Created>> + Send;
fn put_evse(
&self,
owner: PartyRef,
location_id: String,
evse: crate::v2_3_0::locations::Evse,
context: RequestContext,
) -> impl Future<Output = Handled<Created>> + Send;
fn put_connector(
&self,
owner: PartyRef,
location_id: String,
evse_uid: String,
connector: crate::v2_3_0::locations::Connector,
context: RequestContext,
) -> impl Future<Output = Handled<Created>> + Send;
fn patch(
&self,
owner: PartyRef,
location_id: String,
evse_uid: Option<String>,
connector_id: Option<String>,
patch: Patch<serde_json::Value>,
context: RequestContext,
) -> impl Future<Output = Handled<()>> + Send;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Created {
Yes,
No,
}
impl Created {
#[must_use]
pub const fn http_status(self) -> u16 {
match self {
Self::Yes => 201,
Self::No => 200,
}
}
}
impl From<bool> for Created {
fn from(created: bool) -> Self {
if created { Self::Yes } else { Self::No }
}
}
pub trait TokensSender: Send + Sync + 'static {
fn list(
&self,
query: PageQuery,
context: RequestContext,
) -> impl Future<Output = Handled<Page<crate::v2_3_0::tokens::Token>>> + Send;
fn authorize(
&self,
token_uid: String,
token_type: Option<crate::v2_3_0::tokens::TokenType>,
location: Option<crate::v2_3_0::tokens::LocationReferences>,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::tokens::AuthorizationInfo>> + Send;
}
pub trait TokensReceiver: Send + Sync + 'static {
fn token(
&self,
owner: PartyRef,
token_uid: String,
token_type: Option<crate::v2_3_0::tokens::TokenType>,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::tokens::Token>> + Send;
fn put_token(
&self,
owner: PartyRef,
token: crate::v2_3_0::tokens::Token,
context: RequestContext,
) -> impl Future<Output = Handled<Created>> + Send;
fn patch_token(
&self,
owner: PartyRef,
token_uid: String,
token_type: Option<crate::v2_3_0::tokens::TokenType>,
patch: Patch<crate::v2_3_0::tokens::Token>,
context: RequestContext,
) -> impl Future<Output = Handled<()>> + Send;
}
pub trait CdrsSender: Send + Sync + 'static {
fn list(
&self,
query: PageQuery,
context: RequestContext,
) -> impl Future<Output = Handled<Page<crate::v2_3_0::cdrs::Cdr>>> + Send;
}
pub trait CdrsReceiver: Send + Sync + 'static {
fn cdr(
&self,
cdr_id: String,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::cdrs::Cdr>> + Send;
fn post_cdr(
&self,
cdr: crate::v2_3_0::cdrs::Cdr,
context: RequestContext,
) -> impl Future<Output = Handled<crate::types::Url>> + Send;
}
pub trait SessionsSender: Send + Sync + 'static {
fn list(
&self,
query: PageQuery,
context: RequestContext,
) -> impl Future<Output = Handled<Page<crate::v2_3_0::sessions::Session>>> + Send;
fn set_charging_preferences(
&self,
session_id: String,
preferences: crate::v2_3_0::sessions::ChargingPreferences,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::sessions::ChargingPreferencesResponse>> + Send;
}
pub trait SessionsReceiver: Send + Sync + 'static {
fn session(
&self,
owner: PartyRef,
session_id: String,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::sessions::Session>> + Send;
fn put_session(
&self,
owner: PartyRef,
session: crate::v2_3_0::sessions::Session,
context: RequestContext,
) -> impl Future<Output = Handled<Created>> + Send;
fn patch_session(
&self,
owner: PartyRef,
session_id: String,
patch: Patch<crate::v2_3_0::sessions::Session>,
context: RequestContext,
) -> impl Future<Output = Handled<()>> + Send;
}
pub trait TariffsSender: Send + Sync + 'static {
fn list(
&self,
query: PageQuery,
context: RequestContext,
) -> impl Future<Output = Handled<Page<crate::v2_3_0::tariffs::Tariff>>> + Send;
}
pub trait TariffsReceiver: Send + Sync + 'static {
fn tariff(
&self,
owner: PartyRef,
tariff_id: String,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::tariffs::Tariff>> + Send;
fn put_tariff(
&self,
owner: PartyRef,
tariff: crate::v2_3_0::tariffs::Tariff,
context: RequestContext,
) -> impl Future<Output = Handled<Created>> + Send;
fn delete_tariff(
&self,
owner: PartyRef,
tariff_id: String,
context: RequestContext,
) -> impl Future<Output = Handled<()>> + Send;
}
pub trait CommandsReceiver: Send + Sync + 'static {
fn command(
&self,
command: crate::v2_3_0::commands::Command,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::commands::CommandResponse>> + Send;
}
pub trait CommandsSender: Send + Sync + 'static {
fn command_result(
&self,
unique_id: String,
result: crate::v2_3_0::commands::CommandResult,
context: RequestContext,
) -> impl Future<Output = Handled<()>> + Send;
}
pub trait CredentialsHandler: Send + Sync + 'static {
fn get(
&self,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::credentials::Credentials>> + Send;
fn post(
&self,
credentials: crate::v2_3_0::credentials::Credentials,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::credentials::Credentials>> + Send;
fn put(
&self,
credentials: crate::v2_3_0::credentials::Credentials,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::credentials::Credentials>> + Send;
fn delete(&self, context: RequestContext) -> impl Future<Output = Handled<()>> + Send;
}
pub trait HubClientInfoSender: Send + Sync + 'static {
fn list(
&self,
query: PageQuery,
context: RequestContext,
) -> impl Future<Output = Handled<Page<crate::v2_3_0::hub_client_info::ClientInfo>>> + Send;
}
pub trait HubClientInfoReceiver: Send + Sync + 'static {
fn client_info(
&self,
party: PartyRef,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::hub_client_info::ClientInfo>> + Send;
fn put_client_info(
&self,
party: PartyRef,
info: crate::v2_3_0::hub_client_info::ClientInfo,
context: RequestContext,
) -> impl Future<Output = Handled<Created>> + Send;
}
pub trait ChargingProfilesReceiver: Send + Sync + 'static {
fn active_charging_profile(
&self,
session_id: String,
duration_seconds: u64,
response_url: crate::types::Url,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::charging_profiles::ChargingProfileResponse>> + Send;
fn set_charging_profile(
&self,
session_id: String,
request: crate::v2_3_0::charging_profiles::SetChargingProfile,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::charging_profiles::ChargingProfileResponse>> + Send;
fn clear_charging_profile(
&self,
session_id: String,
response_url: crate::types::Url,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::charging_profiles::ChargingProfileResponse>> + Send;
}
pub trait ChargingProfilesSender: Send + Sync + 'static {
fn active_charging_profile_result(
&self,
unique_id: String,
result: crate::v2_3_0::charging_profiles::ActiveChargingProfileResult,
context: RequestContext,
) -> impl Future<Output = Handled<()>> + Send;
fn charging_profile_result(
&self,
unique_id: String,
result: crate::v2_3_0::charging_profiles::ChargingProfileResult,
context: RequestContext,
) -> impl Future<Output = Handled<()>> + Send;
fn clear_profile_result(
&self,
unique_id: String,
result: crate::v2_3_0::charging_profiles::ClearProfileResult,
context: RequestContext,
) -> impl Future<Output = Handled<()>> + Send;
fn put_active_charging_profile(
&self,
session_id: String,
profile: crate::v2_3_0::charging_profiles::ActiveChargingProfile,
context: RequestContext,
) -> impl Future<Output = Handled<()>> + Send;
}
pub trait PaymentsSender: Send + Sync + 'static {
fn terminals(
&self,
query: PageQuery,
context: RequestContext,
) -> impl Future<Output = Handled<Page<crate::v2_3_0::payments::Terminal>>> + Send;
fn terminal(
&self,
terminal_id: String,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::payments::Terminal>> + Send;
fn put_terminal(
&self,
terminal_id: String,
terminal: crate::v2_3_0::payments::Terminal,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::payments::Terminal>> + Send;
fn patch_terminal(
&self,
terminal_id: String,
patch: Patch<crate::v2_3_0::payments::Terminal>,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::payments::Terminal>> + Send;
fn activate_terminal(
&self,
terminal: Patch<crate::v2_3_0::payments::Terminal>,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::payments::Terminal>> + Send;
fn deactivate_terminal(
&self,
terminal_id: String,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::payments::Terminal>> + Send;
fn financial_advice_confirmations(
&self,
query: PageQuery,
context: RequestContext,
) -> impl Future<Output = Handled<Page<crate::v2_3_0::payments::FinancialAdviceConfirmation>>> + Send;
fn financial_advice_confirmation(
&self,
id: String,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::payments::FinancialAdviceConfirmation>> + Send;
}
pub trait PaymentsReceiver: Send + Sync + 'static {
fn terminal(
&self,
terminal_id: String,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::payments::Terminal>> + Send;
fn post_terminal(
&self,
terminal: crate::v2_3_0::payments::Terminal,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::payments::Terminal>> + Send;
fn financial_advice_confirmation(
&self,
id: String,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::payments::FinancialAdviceConfirmation>> + Send;
fn post_financial_advice_confirmation(
&self,
confirmation: crate::v2_3_0::payments::FinancialAdviceConfirmation,
context: RequestContext,
) -> impl Future<Output = Handled<crate::v2_3_0::payments::FinancialAdviceConfirmation>> + Send;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn created_maps_to_the_status_the_spec_gives() {
assert_eq!(Created::Yes.http_status(), 201);
assert_eq!(Created::No.http_status(), 200);
assert_eq!(Created::from(true), Created::Yes);
assert_eq!(Created::from(false), Created::No);
}
}