fauna-api 1.0.693

The Fauna SDK for Rust: accounts, Drive, groups, calendar, chats, payments, Parrot and live rooms.
Documentation
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);

/// Characters `encodeURIComponent` leaves alone.
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()
}

/// `if (!response.ok) throw new Error(await response.text())`.
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 })
}

/// The WebSocket form of an http(s) route: `http` becomes `ws`, `https` becomes `wss`.
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()
}