Skip to main content

fauna_api/
lib.rs

1//! Rust mirror of the `fauna-api` SDK in `api/node`. Every public item here
2//! matches an export there; `npm run sdk:parity` checks the two stay in sync.
3
4mod error;
5mod http;
6mod route;
7
8pub mod ant;
9pub mod authentification;
10pub mod billing;
11pub mod calendar;
12pub mod chats;
13pub mod cicada;
14pub mod drive;
15pub mod extensions;
16pub mod friends;
17pub mod groups;
18pub mod inbox;
19pub mod integrations;
20pub mod magpie;
21pub mod maps;
22pub mod marmot;
23pub mod parrot;
24pub mod payments;
25#[doc(hidden)]
26pub mod private;
27pub mod rooms;
28pub mod shared;
29pub mod termite;
30pub mod tickets;
31pub mod travel;
32pub mod view;
33pub mod webhooks;
34
35use std::future::Future;
36use std::time::Instant;
37
38use futures_util::StreamExt;
39use futures_util::stream;
40
41pub use error::{Error, Result};
42
43static ROUTE: route::Cell = route::cell();
44
45pub fn set_route(route: &str) {
46    route::set(&ROUTE, route);
47}
48
49pub fn get_ip() -> String {
50    route::ip(&ROUTE)
51}
52
53pub fn get_port() -> u16 {
54    route::port(&ROUTE)
55}
56
57pub fn get_route() -> String {
58    route::route(&ROUTE)
59}
60
61pub fn get_url() -> String {
62    route::url(&ROUTE)
63}
64
65/// Milliseconds for one round trip to the gateway root.
66pub async fn ping() -> Result<u64> {
67    let start = Instant::now();
68    http::client().get(format!("{}/", get_url())).send().await?;
69    Ok(start.elapsed().as_millis() as u64)
70}
71
72pub use shared::server as live;
73
74pub use shared::schema::Schema;
75
76/// Runs `tasks` with at most `max` in flight and returns their results in task order.
77pub async fn throttling_promise<T, F, Fut>(tasks: Vec<F>, max: usize) -> Vec<T>
78where
79    F: FnOnce() -> Fut,
80    Fut: Future<Output = T>,
81{
82    if max == 0 {
83        return Vec::new();
84    }
85    stream::iter(tasks.into_iter().map(|task| task()))
86        .buffered(max)
87        .collect()
88        .await
89}