1use crate::{token::{GetByCategoryQueryReq, GetByCategoryQueryRes, GetByTagQueryReq, GetByTagQueryRes, GetRecentRes, TokenSearchReq, TokenSearchRes}, JupiterClient, JupiterError};
2
3
4
5
6#[derive(Clone)]
7pub struct TokenService<'a>{
8 client: &'a JupiterClient,
9}
10
11
12impl<'a> TokenService<'a> {
13 pub fn new(client: &'a JupiterClient) -> Self {
14 Self { client }
15 }
16
17 pub async fn search(
18 &self,
19 req: &TokenSearchReq,
20 ) -> Result<TokenSearchRes, JupiterError> {
21 let path = "/tokens/v2/search";
22 self.client.get_json_with_query(&path, req).await
23 }
24
25 pub async fn get_by_tag(
26 &self,
27 req: &GetByTagQueryReq,
28 ) -> Result<GetByTagQueryRes, JupiterError> {
29 let path = "/tokens/v2/tag";
30 self.client.get_json_with_query(&path, req).await
31 }
32
33 pub async fn get_by_category(
34 &self,
35 category: String,
36 interval: String,
38 req: &GetByCategoryQueryReq,
39 ) -> Result<GetByCategoryQueryRes, JupiterError> {
40 let path = format!("/tokens/v2/{}/{}", category, interval);
41 self.client.get_json_with_query(&path, req).await
42 }
43
44 pub async fn recent(&self) -> Result<GetRecentRes, JupiterError> {
45 let path = "/tokens/v2/recent";
46 self.client.get_json(&path).await
47 }
48}
49
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[tokio::test]
56 async fn test_token_search() {
57 let client = JupiterClient::new(crate::JupiterConfig::default()).unwrap();
58 let token_service = TokenService::new(&client);
59
60 let res = token_service.search(&TokenSearchReq {
61 query: "trump".to_string(),
62 }).await;
63
64 match res {
65 Ok(res) => {
66 println!("token_search: {}", serde_json::to_string_pretty(&res).unwrap());
68 }
69 Err(e) => {
70 panic!("token_search error: {}", e);
71 }
72 }
73 }
74}
75