Created
February 2, 2023 03:30
-
-
Save Enigo/770de3072b68ea32a27127b08d19957a 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
#[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 + PaginatedApi>( | |
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 + PaginatedApi>( | |
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 result.has_results() { | |
// db_handler::save_mints(mint.result, pool).await; | |
} | |
if !result.get_cursor().is_empty() { | |
return Some(result.get_cursor()); | |
} | |
None | |
} | |
Err(e) => { | |
error!("{} API response cannot be parsed! {}", url, e); | |
None | |
} | |
} | |
} | |
async fn fetch_api_response<T: DeserializeOwned + PaginatedApi>( | |
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