cometd_client/lib.rs
1#![deny(unused_must_use)]
2#![warn(
3 rust_2018_idioms,
4 rust_2021_compatibility,
5 missing_docs,
6 missing_debug_implementations,
7 clippy::expect_used,
8 clippy::missing_panics_doc,
9 clippy::panic_in_result_fn,
10 clippy::panicking_unwrap,
11 clippy::unwrap_used,
12 clippy::if_let_mutex,
13 clippy::std_instead_of_core,
14 clippy::missing_const_for_fn,
15 clippy::str_to_string,
16 clippy::clone_on_ref_ptr,
17 clippy::panic,
18 clippy::explicit_iter_loop,
19 clippy::pattern_type_mismatch,
20 clippy::indexing_slicing,
21 clippy::use_debug,
22 clippy::unnested_or_patterns,
23 clippy::return_self_not_must_use,
24 clippy::map_unwrap_or,
25 clippy::items_after_statements,
26 clippy::needless_pass_by_value,
27 clippy::if_not_else,
28 clippy::option_if_let_else
29)]
30
31//! This crate aims to make a client for CometD protocol.
32//!
33//! This project is in progress and might change a lot from version to version.
34//!
35//! # Table of contents
36//! - [Connect endpoints](#connect-endpoints)
37//! - [Authentication](#authentication)
38//! - [Authentication through authorization header](#authorization-authentication)
39//! - [Authentication through cookie](#cookie-authentication)
40//! - [How to interact with client?](#interaction-with-client)
41//!
42//! # Connect endpoints
43//!
44//! Client has ability to customize endpoints base paths:
45//! 1) [`CometdClientBuilder::handshake_base_path`];
46//! 2) [`CometdClientBuilder::subscribe_base_path`];
47//! 3) [`CometdClientBuilder::connect_base_path`];
48//! 4) [`CometdClientBuilder::disconnect_base_path`];
49//!
50//! For example to change handshake base path and
51//! get `http://[::1]:1025/notifications/node/0/handshake`
52//! you can do this:
53//! ```rust,no_run
54//! use cometd_client::CometdClientBuilder;
55//!
56//! # || -> Result<(), Box<dyn std::error::Error>> {
57//! let client = CometdClientBuilder::new(&"http://[::1]:1025/notifications/".parse()?)
58//! .handshake_base_path("hand/")
59//! .build()?;
60//! # let _client: cometd_client::CometdClient<()> = client;
61//! # Ok(())
62//! # };
63//! ```
64//!
65//! Same for others endpoints.
66//!
67//! # Authentication
68//!
69//! There is 2 options to authenticate on server,
70//! through `authorization` header and cookie.
71//!
72//! # Authentication through authorization header
73//!
74//! To use access token with `authorization` header,
75//! you must set it through [`CometdClientBuilder::access_token`].
76//! This library provide 2 default structs:
77//! [`types::access_token::Bearer`] and [`types::access_token::Basic`].
78//! ```rust,no_run
79//! use cometd_client::{CometdClientBuilder, types::access_token::{Bearer, Basic}};
80//!
81//! # || -> Result<(), Box<dyn std::error::Error>> {
82//! // let access_token = Bearer::new("access-token");
83//! let access_token = Basic::create("username", Some("optional password"))?;
84//!
85//! let client = CometdClientBuilder::new(&"http://[::1]:1025/notifications/".parse()?)
86//! .access_token(access_token)
87//! .build()?;
88//! # let _client: cometd_client::CometdClient<()> = client;
89//! # Ok(())
90//! # };
91//! ```
92//!
93//! But you can make you own access token for `authorization` header:
94//! ```rust,no_run
95//! use core::fmt::{Formatter, Debug};
96//! use cometd_client::types::AccessToken;
97//!
98//! struct OAuth(String);
99//!
100//! impl Debug for OAuth {
101//! fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
102//! write!(f, "OAuth(***)")
103//! }
104//! }
105//!
106//! impl OAuth {
107//! pub fn new(access_token: &str) -> Self {
108//! Self(format!("OAuth {access_token}"))
109//! }
110//! }
111//!
112//! impl AccessToken for OAuth {
113//! fn get_authorization_token(&self) -> &str {
114//! &self.0
115//! }
116//! }
117//! ```
118//!
119//! # Authentication through cookie
120//!
121//! If you use session cookies for authentication (or other reasons) you can set it (or them)
122//! through [`CometdClientBuilder::cookie`] or [`CometdClientBuilder::cookies`].
123//! ```rust,no_run
124//! use cometd_client::CometdClientBuilder;
125//!
126//! # || -> Result<(), Box<dyn std::error::Error>> {
127//! let client = CometdClientBuilder::new(&"http://[::1]:1025/notifications/".parse()?)
128//! .cookie("cookie0-name", "cookie0-value")
129//! .cookies([
130//! ("cookie1-name", "cookie1-value"),
131//! ("cookie2-name", "cookie2-value")
132//! ])
133//! .build()?;
134//! # let _client: cometd_client::CometdClient<()> = client;
135//! # Ok(())
136//! # };
137//! ```
138//!
139//! # How to interact with client?
140//!
141//! Client use MPMC channel to send messages and errors.
142//! [`CometdClientBuilder::build`] spawn task which do handshake and start wait for messages.
143//! If handshake request was unsuccessful with [`types::Reconnect::Handshake`] or [`types::Reconnect::Retry`] advice from server,
144//! then client tries redo it by [`CometdClientBuilder::number_of_retries`] times.
145//! In other cases task send error to event channel and stops.
146//!
147//! After successful handshake task start listen messages coming from server.
148//! If during that requests occurs error with [`types::Reconnect::Handshake`] advice,
149//! then client will tries redo handshake (look above).
150//! If error will be with [`types::Reconnect::Retry`] advice, then it will try redo it
151//! by [`CometdClientBuilder::number_of_retries`] times.
152//!
153//! To send subscribe command you must use [`CometdClient::subscribe`].
154//! If error occurs it will be redone by same scheme as for connect (look above).
155//!
156//! To get event channel receiver use [`CometdClient::rx`].
157//!
158//! ```rust,no_run
159//! use cometd_client::{types::CometdClientEvent, CometdClientBuilder};
160//!
161//! # async fn _test() -> Result<(), Box<dyn std::error::Error>> {
162//! let client = CometdClientBuilder::new(&"http://[::0]:1025/notifications/".parse()?)
163//! .build()?;
164//! # let client: cometd_client::CometdClient<()> = client;
165//!
166//! let mut rx = client.rx();
167//!
168//! tokio::spawn(async move {
169//! while let Some(event) = rx.recv().await {
170//! match event {
171//! CometdClientEvent::Message(messages) => println!("got messages: `{messages:?}`."),
172//! CometdClientEvent::Error(error) => eprintln!("got error: `{error:?}`."),
173//! }
174//! }
175//! });
176//!
177//! # Ok(())
178//! # }
179//! ```
180//!
181
182mod client;
183mod common;
184mod consts;
185mod ext;
186mod sugar;
187
188/// Contains various structs, enums and traits.
189pub mod types;
190
191pub use client::*;
192
193pub(crate) use {ext::*, sugar::*};