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
//! The Rust SDK for the [HTTP server
//! API](https://centrifugal.dev/docs/server/server_api) of
//! [Centrifugo](https://centrifugal.dev): every API method, including those of
//! Centrifugo PRO; tokens; proxy handlers and the endpoints client SDKs call.
//!
//! use cent::{Client, Config, PublishRequest};
//!
//! # async fn run() -> Result<(), cent::Error> {
//! let client = Client::new(Config::new("http://localhost:8000/api").api_key("secret"))?;
//! let data = cent::json(&serde_json::json!({"text": "hello"}))?;
//! let res = client.publish(PublishRequest::new("news").data(data)).await?;
//! println!("published at offset {}", res.offset);
//! # Ok(()) }
//! ```
//!
//! # The client
//!
//! [`Client`] - and [`blocking::Client`], with the `blocking` feature - has a
//! method per API method, taking its request and returning its result, both
//! generated from Centrifugo's `api.proto`. A request's `new` takes the fields
//! Centrifugo requires; a method per field sets the others:
//! `PublishRequest::new("news").data(data).idempotency_key("k")`.
//!
//! - **Payloads** are JSON, as [`RawValue`]: kept exactly - number text
//! included - in requests and results alike, so a payload from a reply goes
//! back out as the same JSON value. JSON `null` is `Some` of a null; unset is
//! `None`. `serde_json` writes NaN and infinities as `null`: check floats
//! before encoding them. Write integers which may pass 2^53 as JSON
//! strings: browsers decode JSON numbers as doubles. [`json`] encodes any
//! serde type as a payload, and [`PayloadExt::decode`] decodes one:
//! `.data(cent::json(&message)?)`, `publication.data.decode::<Message>()?`.
//! - **One deadline per call**: [`Config::request_timeout`], or the call's own
//! [`Client::with_timeout`], which replaces it; a [`Client::with_deadline`]
//! only shortens it, never extends it. Every timeout is [`Error::Timeout`].
//! - **Every call returns an error unless it did everything asked.** A
//! broadcast or batch in which some parts failed returns
//! [`Error::BroadcastFailedChannels`] or [`Error::BatchFailedCommands`],
//! holding every part's outcome - see [`Client::broadcast`] and [`Batch`].
//! - **Retries**: [`retryable`] says whether a new attempt may succeed; retry
//! a publication only with the same `idempotency_key`, so that it is not
//! published twice. [`Client::broadcast_with_retry`] retries only the
//! channels which may still succeed. Centrifugo remembers a key per
//! channel for five minutes by default, and a second publication under the
//! same key is not published: a key derived from an entity must name the
//! event too, `order-42-paid` rather than `order-42`. The memory, Redis and
//! PostgreSQL brokers keep keys; the NATS broker ignores them.
//! - **Automatic batching**: [`AutoBatch`] sends concurrent calls together
//! once enough requests are in flight, never waiting for a timer.
//! - **Shutdown**: [`Client::close`] refuses new calls and waits for those in
//! progress.
//! - **Tests** of code using the client need no Centrifugo: give it an
//! [`HttpClient`] answering as Centrifugo does - see the README.
//!
//! # Tokens, proxy handlers, endpoints
//!
//! [`token`] signs connection, subscription and Centrifugo PRO server API
//! tokens, and builds JWKS. [`proxy`] answers Centrifugo's HTTP proxy;
//! [`endpoints`] serves what the `getToken` and `getSignature` callbacks of
//! client SDKs call, and JWKS; [`sharedpoll`] signs shared poll
//! subscriptions. Each endpoint is a [`Handler`] - one function from an
//! `http::Request<Bytes>` to a response, served from any framework, and a
//! `tower::Service` with the `tower` feature. [`Command`] writes API calls
//! for Centrifugo's async consumers.
//!
//! # Features
//!
//! | Feature | Default | What |
//! |---|---|---|
//! | `client` | yes | The async [`Client`], on tokio, with hyper and rustls |
//! | `token` | yes | [`token`], [`sharedpoll`], and the JWKS endpoint |
//! | `blocking` | | [`blocking::Client`] |
//! | `tower` | | [`Handler`] as a `tower::Service` |
//!
//! cent implements the [Centrifugo server SDK
//! specification](https://github.com/centrifugal/gocent/blob/master/spec.md),
//! version 0.4.
pub use *;
pub use RawValue;
pub use ;
pub use ;
pub use ;
pub use Command;
pub use ;
pub use Handler;
pub use ;
pub use Pager;
pub use ;
pub use ;
pub use Request;
pub use ;
pub use Expiry;
// The README's examples compile and run as doctests.
;