surf_disco/lib.rs
1// Copyright (c) 2022 Espresso Systems (espressosys.com)
2// This file is part of the surf-disco library.
3
4// You should have received a copy of the MIT License
5// along with the surf-disco library. If not, see <https://mit-license.org/>.
6
7//! Surf Disco: a client library for [Tide Disco](https://tide-disco.docs.espressosys.com/tide_disco/) applications.
8//!
9//! # Quick Start
10//!
11//! ```
12//! # use surf_disco::{Client, error::ClientError};
13//! # use vbs::version::StaticVersion;
14//! # async fn ex() {
15//! let url = "http://localhost:50000".parse().unwrap();
16//! let client: Client<ClientError, StaticVersion<0,1>> = Client::new(url);
17//! let res: String = client.get("/app/route").send().await.unwrap();
18//! # }
19//! ```
20//!
21use serde::de::DeserializeOwned;
22use std::time::Duration;
23use vbs::version::StaticVersionType;
24
25pub mod client;
26pub mod error;
27pub mod request;
28pub mod socket;
29
30pub use client::{Client, ContentType};
31pub use error::Error;
32pub use request::Request;
33pub use socket::SocketRequest;
34pub use tide_disco::{
35 http::{self, Method},
36 StatusCode, Url,
37};
38
39/// Build an HTTP `GET` request.
40pub fn get<T: DeserializeOwned, E: Error, VER: StaticVersionType>(url: Url) -> Request<T, E, VER> {
41 Client::new(url).get("/")
42}
43
44/// Build an HTTP `POST` request.
45pub fn post<T: DeserializeOwned, E: Error, VER: StaticVersionType>(url: Url) -> Request<T, E, VER> {
46 Client::new(url).post("/")
47}
48
49/// Connect to a server, retrying if the server is not running.
50///
51/// This function will make an HTTP `GET` request to the server's `/healthcheck` endpoint, to test
52/// if the server is available. If this request succeeds, [connect] returns `true`. Otherwise, the
53/// client will continue retrying `/healthcheck` requests until `timeout` has elapsed (or forever,
54/// if `timeout` is `None`). If the timeout expires before a `/healthcheck` request succeeds,
55/// [connect] will return `false`.
56pub async fn connect<E: Error, VER: StaticVersionType>(
57 url: Url,
58 timeout: Option<Duration>,
59) -> bool {
60 Client::<E, VER>::new(url).connect(timeout).await
61}