essential_rest_client/
lib.rs

1//! Client libraries for interacting with the Essential builder and the Essential node.
2
3#![deny(missing_docs)]
4
5use anyhow::Context;
6use std::path::Path;
7
8use essential_types::{Contract, Program};
9
10/// Client library for sending requests to the Essential builder.
11pub mod builder_client;
12/// Client library for sending requests to the Essential node.
13pub mod node_client;
14
15/// A helper for reading a [`Contract`] and its [`Program`]s from a JSON file at the given path.
16///
17/// Specifically, expects the JSON to be the serialized form of `(Contract, Vec<Program>)`.
18pub async fn contract_from_path(contract_path: &Path) -> anyhow::Result<(Contract, Vec<Program>)> {
19    let contract_string = tokio::fs::read_to_string(&contract_path)
20        .await
21        .with_context(|| format!("failed to read contract from file {contract_path:?}"))?;
22    let (contract, programs): (Contract, Vec<Program>) = serde_json::from_str(&contract_string)
23        .with_context(|| {
24            format!("failed to parse contract and/or its programs from JSON at {contract_path:?}")
25        })?;
26    Ok((contract, programs))
27}
28
29/// Map `reqwest::Response` into `anyhow::Result`.
30async fn handle_response(response: reqwest::Response) -> anyhow::Result<reqwest::Response> {
31    let status = response.status();
32    if status.is_success() {
33        Ok(response)
34    } else {
35        let text = response.text().await?;
36        Err(anyhow::anyhow!("{}: {}", status, text))
37    }
38}