1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
pub mod accounts;
pub mod blocks;
pub mod contract;
pub mod proxy;
pub mod token;
pub mod transactions;
pub mod stats;

use crate::{
    error::{CustomErrors, ErrorCause},
    query_handler::QueryBuilder,
};
use reqwest::Client;
use serde::Deserialize;

#[derive(Debug, Deserialize)]
pub struct Response<T: Clone> {
    status: String,
    message: String,
    result: T,
}

impl<T> Response<T>
where
    T: Clone,
{
    pub fn new(status: String, message: String, result: T) -> Response<T> {
        Response {
            status,
            message,
            result,
        }
    }

    pub fn status(&self) -> String {
        self.status.clone()
    }

    pub fn message(&self) -> String {
        self.message.clone()
    }

    pub fn result(self) -> Result<T, CustomErrors> {
        if self.status() != "1" {
            return Err(CustomErrors::new(ErrorCause::BadRequest(self.message())));
        } else {
            Ok(self.result)
        }
    }
}

pub struct BscChainApi {
    api_key: String,
    query: QueryBuilder,
    client: Client,
}

impl BscChainApi {
    pub fn new(api_key: &str) -> BscChainApi {
        BscChainApi {
            api_key: api_key.to_string(),
            query: QueryBuilder::new("api.bscscan.com", "api"),
            client: Client::builder().https_only(true).build().unwrap(),
        }
    }
}