1use std::fmt::{Debug, Display, Formatter};
2
3pub use crate::api::default_provider::DefaultProvider;
4use crate::api::util::transact;
5use crate::api::v1::chain::ChainAPI;
6use crate::api::v1::structs::{ClientError, SendTransactionResponse, SendTransactionResponseError};
7use crate::chain::action::Action;
8use crate::chain::private_key::PrivateKey;
9
10pub enum HTTPMethod {
11 GET,
12 POST,
13}
14
15impl Display for HTTPMethod {
16 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
17 match self {
18 HTTPMethod::GET => {
19 write!(f, "GET")
20 }
21 HTTPMethod::POST => {
22 write!(f, "POST")
23 }
24 }
25 }
26}
27
28#[async_trait::async_trait]
31pub trait Provider: Debug + Default + Sync + Send {
32 async fn post(&self, path: String, body: Option<String>) -> Result<String, String>;
33 async fn get(&self, path: String) -> Result<String, String>;
34}
35
36#[derive(Debug, Default, Clone)]
37pub struct APIClient<P: Provider> {
38 pub v1_chain: ChainAPI<P>,
39}
40
41impl<P: Provider> APIClient<P> {
42 pub fn default_provider(base_url: String) -> Result<APIClient<DefaultProvider>, String> {
43 let provider = DefaultProvider::new(base_url).unwrap();
44 APIClient::custom_provider(provider)
45 }
46
47 pub fn custom_provider(provider: P) -> Result<Self, String> {
48 Ok(APIClient {
49 v1_chain: ChainAPI::new(provider),
50 })
51 }
52
53 pub async fn transact(
54 &self,
55 actions: Vec<Action>,
56 private_key: PrivateKey,
57 ) -> Result<SendTransactionResponse, ClientError<SendTransactionResponseError>> {
58 transact(self, actions, private_key).await
59 }
60}