Skip to main content

whatsapp_rust/
http.rs

1pub use wacore::net::{HttpClient, HttpRequest, HttpResponse};
2
3/// An HTTP exchange that completed, carrying the status the client refused it
4/// on.
5///
6/// Attached as the `source` of the errors the media paths return, so a consumer
7/// recovers the status by type — [`ErrorChainExt::http_status`] — instead of
8/// parsing a message. The same role
9/// [`wacore::request::ServerErrorCode`] plays for IQ rejections, and embedded
10/// the same way: `anyhow::Error::new(HttpStatusError { .. }).context(..)`.
11///
12/// Deliberately a different question from an IQ `code`. A CDN refusing a byte
13/// range and the chat server refusing a stanza are different layers with
14/// different remedies, so one accessor answering for both would tell a consumer
15/// the number but not which thing to retry. [`ErrorChainExt`] keeps them apart
16/// for the reason it already keeps `MexError`'s GraphQL code out of
17/// `server_rejection` — see the [module docs](crate::error).
18///
19/// [`ErrorChainExt`]: crate::error::ErrorChainExt
20/// [`ErrorChainExt::http_status`]: crate::error::ErrorChainExt::http_status
21#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
22#[error("HTTP status {status}")]
23pub struct HttpStatusError {
24    /// The status the server answered with.
25    pub status: u16,
26}
27
28impl HttpStatusError {
29    /// Wraps `context` around this status so the message reads as the operation
30    /// that failed while the status stays recoverable underneath it.
31    pub(crate) fn into_error(self, context: String) -> anyhow::Error {
32        anyhow::Error::new(self).context(context)
33    }
34}
35
36pub(crate) const HTTP_STATUS_OK: u16 = 200;
37pub(crate) const HTTP_STATUS_REDIRECTION_START: u16 = 300;
38pub(crate) const HTTP_STATUS_UNAUTHORIZED: u16 = 401;
39pub(crate) const HTTP_STATUS_FORBIDDEN: u16 = 403;
40pub(crate) const HTTP_STATUS_NOT_FOUND: u16 = 404;
41pub(crate) const HTTP_STATUS_GONE: u16 = 410;
42
43#[cfg(feature = "ureq-client")]
44pub use whatsapp_rust_ureq_http_client::UreqHttpClient;