Created
February 2, 2023 03:49
-
-
Save Enigo/738f3c6ddb0674970f66bd27b4a2ce16 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, &MintSaver).await; | |
read_with_cursor_as::<Asset>(ASSETS_URL, &AssetSaver).await; | |
} | |
async fn read_with_cursor_as<T: DeserializeOwned + PaginatedApi>( | |
url: &str, | |
persistable: &dyn Persistable<T>, | |
) { | |
let pool = db_handler::open_connection().await; | |
let mut cursor = None; | |
loop { | |
cursor = execute_and_get_cursor::<T>(url, cursor, persistable, &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>, | |
persistable: &dyn Persistable<T>, | |
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() { | |
persistable.persist_one(&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 | |
} | |
} | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment