Available on crate feature
server only.Expand description
An OCPI server: one trait per module and interface, mounted onto an axum::Router.
use std::sync::Arc;
use ocpi_kit::server::{InMemoryTokenStore, OcpiRouter};
use ocpi_kit::types::Url;
use ocpi_kit::VersionNumber;
let tokens = Arc::new(InMemoryTokenStore::new());
let app = OcpiRouter::new(
VersionNumber::V2_3_0,
Url::new("https://cpo.example.com/ocpi/cpo/2.3.0")?,
tokens,
)
.credentials(credentials)
.locations_sender(locations)
.build();
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await?;
axum::serve(listener, app).await?;§What the router takes care of
- The status code rules. Only five situations get an HTTP error status; everything that
reached the OCPI layer is a
200 OKwith a four-digit code in the body. A handler returnsOcpiErrorand the mapping happens once, correctly. - Authentication, and the
CREDENTIALS_TOKEN_Ascope. A bootstrap token used on any module other thancredentialsandversionsgets a 401, as the specification requires. - Ownership of client-owned objects. A platform writing under a
country_code/party_idthat is not one of its own roles gets a 404 — “this way blocking client access to objects that do not belong to them” — and the handler is never called. X-Request-IDandX-Correlation-ID. Echoed on every response, generated when the peer forgot them.- Version details.
/versionsand the version-details endpoint are generated from exactly what was mounted, so discovery cannot disagree with reality. - The PATCH rule. A patch without
last_updatednever reaches a handler; it is the specification’s own example of a2001.
§What it deliberately leaves to you
Persistence, and the two credentials 405 rules — only the implementation knows whether a peer
is already registered. PeerState has the predicates for those.
Structs§
- Auth
- Extracts and authenticates the credentials token.
- Authenticated
Peer - The party a request was authenticated as.
- Callback
Urls - Builds the
response_urls that reach the callbacks this crate’s router mounts. - Http
Status Code - Re-exported for handlers that build their own responses.
An HTTP status code (
status-codein RFC 9110 et al.). - Ids
- The
X-Request-IDandX-Correlation-IDof the request. - InMemory
Token Store - A
TokenStoreheld in memory, for tests, small deployments and getting started. - Mounted
Modules - Which modules and interfaces this server implements, for generating version details.
- Ocpi
Error Response - The HTTP response an
OcpiErrorbecomes. - Ocpi
Json - A JSON body, decoded with the path to any offending value.
- Ocpi
Patch - A JSON Merge Patch body.
- Ocpi
Reply - An OCPI response on its way out: the envelope, plus the headers that must accompany it.
- Ocpi
Router - Builds the
axum::Routerthat serves one OCPI version. - Ocpi
State - Everything the extractors and handlers share.
- Owner
- The
{country_code}/{party_id}pair of a client-owned-object URL. - Page
- The pagination query parameters.
- Peer
Registry - A registry of the peers this server has registered, keyed by peer id.
- Request
Context - The pieces of a request a handler is given.
- Routing
- The
OCPI-to-*andOCPI-from-*headers, when present. - Server
Config - How the server behaves.
Enums§
- Created
- Whether a
PUTcreated the object or replaced one.
Traits§
- Auth
State - What
Authneeds from the router’s state. - Cdrs
Receiver - The eMSP side of the CDRs module.
- Cdrs
Sender - The CPO side of the CDRs module.
- Charging
Profiles Receiver - The CPO side of the Charging Profiles module: accepting profiles for a running session.
- Charging
Profiles Sender - The eMSP/SCSP side of the Charging Profiles module: receiving what the Charge Point decided.
- Commands
Receiver - The CPO side of the Commands module: receiving commands from an eMSP.
- Commands
Sender - The eMSP side of the Commands module: receiving the asynchronous result.
- Content
Type Policy - What
OcpiJsonneeds from the router’s state. - Credentials
Handler - The credentials module, which every implementation must serve.
- HubClient
Info Receiver - The client side of the Hub Client Info module.
- HubClient
Info Sender - The hub side of the Hub Client Info module.
- Locations
Receiver - The eMSP side of the Locations module: receiving the Locations another party owns.
- Locations
Sender - The CPO side of the Locations module: serving the Locations this party owns.
- Page
Policy - What
Pageneeds from the router’s state. - Payments
Receiver - The CPO side of the Payments module: the PTP’s terminals as the CPO stores them.
- Payments
Sender - The PTP side of the Payments module: the terminals this Payment Terminal Provider owns.
- Sessions
Receiver - The eMSP side of the Sessions module.
- Sessions
Sender - The CPO side of the Sessions module.
- Tariffs
Receiver - The eMSP side of the Tariffs module.
- Tariffs
Sender - The CPO side of the Tariffs module.
- Token
Store - Resolves an incoming credentials token to the platform that holds it.
- Tokens
Receiver - The CPO side of the Tokens module: receiving the Tokens an eMSP owns.
- Tokens
Sender - The eMSP side of the Tokens module: serving Tokens and answering real-time authorizations.
Functions§
- accepts_
json - The default reading of the
Content-Typerule. - echo_
ids - Copies the request and correlation IDs onto a response, generating what is missing.
- reject
- Renders a rejection, so a handler can return one directly.
Type Aliases§
- Handled
- The result every handler method returns.