use std::sync::LazyLock;
use percent_encoding::{AsciiSet, NON_ALPHANUMERIC, utf8_percent_encode};
use crate::error::{Error, Result};
static CLIENT: LazyLock<reqwest::Client> = LazyLock::new(reqwest::Client::new);
const COMPONENT: &AsciiSet = &NON_ALPHANUMERIC
.remove(b'-')
.remove(b'_')
.remove(b'.')
.remove(b'!')
.remove(b'~')
.remove(b'*')
.remove(b'\'')
.remove(b'(')
.remove(b')');
pub(crate) fn client() -> &'static reqwest::Client {
&CLIENT
}
pub(crate) fn encode(value: &str) -> String {
utf8_percent_encode(value, COMPONENT).to_string()
}
pub(crate) async fn ok(response: reqwest::Response) -> Result<reqwest::Response> {
if response.status().is_success() {
return Ok(response);
}
let status = response.status().as_u16();
let body = response.text().await.unwrap_or_default();
Err(Error::Http { status, body })
}
pub(crate) fn websocket(url: &str) -> String {
if let Some(rest) = url.strip_prefix("https://") {
return format!("wss://{rest}");
}
if let Some(rest) = url.strip_prefix("http://") {
return format!("ws://{rest}");
}
url.to_string()
}