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