Last active
January 23, 2023 10:31
-
-
Save Enigo/186de9a30310cade078fa6d6abf3b272 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use log::{error, info}; | |
use serde::de::DeserializeOwned; | |
use crate::model::mint::Mint; | |
const MINTS_URL: &str = "https://api.x.immutable.com/v1/mints?token_address=0x9e0d99b864e1ac12565125c5a82b59adea5a09cd&page_size=200"; | |
#[tokio::main] | |
pub async fn read() { | |
let mut cursor = None; | |
loop { | |
cursor = execute_and_get_cursor(cursor).await; | |
if cursor.is_none() { | |
break; | |
} else { | |
info!("Current cursor: {}", cursor.clone().unwrap()); | |
} | |
} | |
} | |
async fn execute_and_get_cursor(cursor: Option<String>) -> Option<String> { | |
let url = if cursor.is_some() { MINTS_URL.to_owned() + "&cursor=" + cursor.unwrap().as_str() } else { String::from(MINTS_URL) }; | |
let response = fetch_api_response::<Mint>(url.as_str()).await; | |
match response { | |
Ok(mint) => { | |
info!("Processing mint response"); | |
if !mint.result.is_empty() { | |
// will be saved to DB later | |
info!("{:?}", mint.result); | |
} | |
if !mint.cursor.is_empty() { | |
return Some(mint.cursor); | |
} | |
None | |
} | |
Err(e) => { | |
error!("Mints API response cannot be parsed! {}", e); | |
None | |
} | |
} | |
} | |
async fn fetch_api_response<T: DeserializeOwned>(endpoint: &str) -> reqwest::Result<T> { | |
let result = reqwest::get(endpoint) | |
.await?.json::<T>() | |
.await?; | |
return Ok(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment