Skip to main content

Module client

Module client 

Source
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 UrlPolicy that says no to plain HTTP, loopback and private addresses by default. Credentials.url, Endpoint.url and every response_url are attacker-influenced inputs; a client that fetches them unconditionally is an SSRF proxy.
  • It validates what it sends. ClientConfig::validate_outgoing is 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 GET is retried.
  • It never logs the token. The tracing spans carry the request and correlation IDs and the routing parties; CredentialsToken redacts itself in any case.

Structs§

CdrsClient
Pulls CDRs from a CPO, and pushes them to an eMSP.
ChargingProfilesClient
Drives a CPO’s Charging Profiles Receiver interface, as an eMSP or SCSP.
Check
One thing that was checked, and what the peer did.
ClientConfig
How the client behaves.
CommandsClient
Sends commands to a CPO.
Conformance
Drives a peer through the checks.
Discovered
Step 1: the peer’s supported versions are known.
HubClientInfoClient
Reads and pushes ClientInfo, the hub’s view of who is connected.
LocationsReceiver
Pushes Locations to an eMSP or NSP.
LocationsSender
Pulls Locations from a CPO.
ModuleClient
The shared plumbing of every module client.
OcpiClient
The entry point: an HTTP client plus the configuration every request uses.
OcpiRequest
One outgoing OCPI request, before it is sent.
PageStream
An asynchronous crawl over every page of a list endpoint.
PaymentsClient
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.
PeerBuilder
Builds a Peer from 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.
ResyncPlan
What to pull, and when to pull again.
RetryPolicy
How a failed GET is retried.
Selected
Step 2: the peer’s endpoints for the chosen version are known, and can be checked before anything is sent.
SessionsReceiver
Pushes Sessions to an eMSP.
SessionsSender
Pulls Sessions from a CPO, and sets a driver’s charging preferences.
TariffsReceiver
Pushes Tariffs to an eMSP.
TariffsSender
Pulls Tariffs from a CPO.
TokensReceiver
Pushes Tokens to a CPO.
TokensSender
Real-time authorization and Token pulls, on the eMSP’s Sender interface.
Transport
Sends OCPI requests and decodes the envelope.

Enums§

Outcome
How one check came out.
PeerState
Where a connection with one peer stands.

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: RequestIds for a caller that wants to correlate several requests.