Available on crate feature
client only.Expand description
An async OCPI client: registration handshake, typed module clients, paginated crawls.
use ocpi_kit::client::{OcpiClient, Registration};
use ocpi_kit::transport::{CredentialsToken, PageQuery};
use ocpi_kit::types::{PartyRef, Url};
use ocpi_kit::{InterfaceRole, ModuleId};
let client = OcpiClient::new()?;
let me = PartyRef::new("NL", "TNM")?;
// The registration handshake, in the order the specification defines it.
let peer = Registration::new(
Url::new("https://cpo.example.com/ocpi/versions")?,
CredentialsToken::new("token-a-received-out-of-band")?,
)
.discover(client.transport()).await?
.select_best(client.transport()).await?;
// Refuse to register with a peer that does not implement what we need — before POSTing.
peer.require(&[(ModuleId::Locations, InterfaceRole::Sender)])?;
let peer = peer.register(client.transport(), &my_credentials()).await?;
// Then pull, following every `Link: rel="next"`.
let mut locations = peer.locations(client.transport(), me).list(PageQuery::new())?;
while let Some(location) = locations.next().await? {
println!("{} {}", location.id, location.name.as_deref().unwrap_or(""));
}§What this client does that a hand-rolled one usually does not
- It refuses to call a URL it should not. Every request is checked against a
UrlPolicythat says no to plain HTTP, loopback and private addresses by default.Credentials.url,Endpoint.urland everyresponse_urlare attacker-influenced inputs; a client that fetches them unconditionally is an SSRF proxy. - It validates what it sends.
ClientConfig::validate_outgoingis on by default, so a non-conformant object is caught here rather than at the partner’s support desk. - It only retries what it may. “OCPI messages SHOULD NOT be queued. When a client does a
POST, PUT or PATCH request and that request fails or times out, the client should not queue
the message and retry.” Only
GETis retried. - It never logs the token. The
tracingspans carry the request and correlation IDs and the routing parties;CredentialsTokenredacts itself in any case.
Structs§
- Cdrs
Client - Pulls CDRs from a CPO, and pushes them to an eMSP.
- Charging
Profiles Client - Drives a CPO’s Charging Profiles Receiver interface, as an eMSP or SCSP.
- Check
- One thing that was checked, and what the peer did.
- Client
Config - How the client behaves.
- Commands
Client - Sends commands to a CPO.
- Conformance
- Drives a peer through the checks.
- Discovered
- Step 1: the peer’s supported versions are known.
- HubClient
Info Client - Reads and pushes
ClientInfo, the hub’s view of who is connected. - Locations
Receiver - Pushes Locations to an eMSP or NSP.
- Locations
Sender - Pulls Locations from a CPO.
- Module
Client - The shared plumbing of every module client.
- Ocpi
Client - The entry point: an HTTP client plus the configuration every request uses.
- Ocpi
Request - One outgoing OCPI request, before it is sent.
- Page
Stream - An asynchronous crawl over every page of a list endpoint.
- Payments
Client - The Payments module, from either side.
- Peer
- A connected platform: the version agreed with it, where its endpoints are, and the token to authenticate with.
- Peer
Builder - Builds a
Peerfrom stored registration state. - Registration
- Step 0: what was agreed out of band.
- Report
- Everything the runner found.
- Resync
- Builds the pull that brings a receiver back in sync.
- Resync
Plan - What to pull, and when to pull again.
- Retry
Policy - How a failed
GETis retried. - Selected
- Step 2: the peer’s endpoints for the chosen version are known, and can be checked before anything is sent.
- Sessions
Receiver - Pushes Sessions to an eMSP.
- Sessions
Sender - Pulls Sessions from a CPO, and sets a driver’s charging preferences.
- Tariffs
Receiver - Pushes Tariffs to an eMSP.
- Tariffs
Sender - Pulls Tariffs from a CPO.
- Tokens
Receiver - Pushes Tokens to a CPO.
- Tokens
Sender - Real-time authorization and Token pulls, on the eMSP’s Sender interface.
- Transport
- Sends OCPI requests and decodes the envelope.
Enums§
Constants§
- DEFAULT_
MAX_ PAGES - The number of pages a crawl will fetch before giving up, unless configured otherwise.
Functions§
- check_
outgoing - Validates an object before it goes on the wire, when the configuration asks for it.
- correlated_
ids - Convenience:
RequestIdsfor a caller that wants to correlate several requests.