1#![doc = include_str!("../README.md")]
4use std::str::FromStr;
5
6use error::NodelessError;
7use reqwest::{Client, Url};
8use serde::{Deserialize, Serialize};
9use serde_json::Value;
10
11pub mod error;
12pub mod paywall;
13pub mod paywall_webhook;
14pub mod serde_utils;
15pub mod store;
16pub mod store_webhook;
17pub mod transaction;
18pub mod webhook;
19
20#[derive(Debug, Clone)]
21pub struct Nodeless {
22 api_key: String,
23 base_url: Url,
24 client: Client,
25}
26
27impl Nodeless {
28 pub fn new(api_key: &str, api_url: Option<String>) -> Result<Self, NodelessError> {
42 let base_url = match api_url {
43 Some(url) => Url::from_str(&url)?,
44 None => Url::from_str("https://nodeless.io")?,
45 };
46
47 let client = reqwest::Client::builder().build()?;
48
49 Ok(Self {
50 api_key: api_key.to_string(),
51 base_url,
52 client,
53 })
54 }
55
56 async fn make_get(&self, url: Url) -> Result<Value, NodelessError> {
57 Ok(self
58 .client
59 .get(url)
60 .header("Authorization", format!("Bearer {}", self.api_key))
61 .header("Content-Type", "application/json")
62 .header("Accept", "application/json")
63 .send()
64 .await?
65 .json::<Value>()
66 .await?)
67 }
68
69 async fn make_post(&self, url: Url, data: Option<Value>) -> Result<Value, NodelessError> {
70 Ok(self
71 .client
72 .post(url)
73 .header("Authorization", format!("Bearer {}", self.api_key))
74 .header("Content-Type", "application/json")
75 .header("Accept", "application/json")
76 .json(&data)
77 .send()
78 .await?
79 .json::<Value>()
80 .await?)
81 }
82
83 async fn make_put(&self, url: Url, data: Option<Value>) -> Result<Value, NodelessError> {
84 let res = self
85 .client
86 .put(url)
87 .header("Authorization", format!("Bearer {}", self.api_key))
88 .header("Content-Type", "application/json")
89 .header("Accept", "application/json")
90 .json(&data)
91 .send()
92 .await?;
93 let res = res.json::<Value>().await?;
94 Ok(res)
95 }
96
97 async fn make_delete(&self, url: Url) -> Result<Value, NodelessError> {
98 let res = self
99 .client
100 .delete(url)
101 .header("Authorization", format!("Bearer {}", self.api_key))
102 .header("Content-Type", "application/json")
103 .header("Accept", "application/json")
104 .send()
105 .await?;
106
107 let res = res.json::<Value>().await?;
108 Ok(res)
109 }
110
111 pub async fn get_server_status(&self) -> Result<ServerStatusResponse, NodelessError> {
113 let url = self.base_url.join("api/v1/status")?;
114
115 let res = self.make_get(url).await?;
116 Ok(serde_json::from_value(res["data"].clone())?)
117 }
118}
119
120#[derive(Clone, Debug, Deserialize, Serialize)]
121#[serde(rename_all = "camelCase")]
122pub struct ServerStatusResponse {
123 pub code: u32,
124 pub status: String,
125 pub node: String,
126}