Skip to content

Instantly share code, notes, and snippets.

@Enigo
Created February 2, 2023 03:17
Show Gist options
  • Save Enigo/cf414d46457ccf4be3000b45028e5a8c to your computer and use it in GitHub Desktop.
Save Enigo/cf414d46457ccf4be3000b45028e5a8c to your computer and use it in GitHub Desktop.
const MINTS_URL: &str = "https://api.x.immutable.com/v1/mints?token_address=0x9e0d99b864e1ac12565125c5a82b59adea5a09cd&page_size=200";
const ASSETS_URL: &str = "https://api.x.immutable.com/v1/assets?collection=0x9e0d99b864e1ac12565125c5a82b59adea5a09cd&page_size=200";
#[tokio::main]
pub async fn read() {
read_with_cursor_as::<Mint>(MINTS_URL).await;
read_with_cursor_as::<Asset>(ASSETS_URL).await;
}
async fn read_with_cursor_as<T: DeserializeOwned>(
url: &str,
) {
let pool = db_handler::open_connection().await;
let mut cursor = None;
loop {
cursor = execute_and_get_cursor::<T>(url, cursor, &pool).await;
if cursor.is_none() {
break;
} else {
info!("Current cursor: {}", cursor.clone().unwrap());
}
}
db_handler::close_connection(pool).await;
}
async fn execute_and_get_cursor<T: DeserializeOwned>(
url: &str,
cursor: Option<String>,
pool: &Pool<Postgres>,
) -> Option<String> {
let url = if cursor.is_some() {
url.to_owned() + "&cursor=" + cursor.unwrap().as_str()
} else {
String::from(url)
};
let response = fetch_api_response::<T>(url.as_str()).await;
match response {
Ok(result) => {
info!("Processing response");
// if !mint.result.is_empty() {
// db_handler::save_mints(mint.result, pool).await;
// }
// if !mint.cursor.is_empty() {
// return Some(mint.cursor);
// }
None
}
Err(e) => {
error!("{} API response cannot be parsed! {}", url, 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