chore: refactor to separate HTTP client ops

This commit is contained in:
Aravinth Manivannan 2023-03-02 17:59:15 +05:30
parent 61a0a7bc11
commit 540026c61e
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88

View file

@ -33,18 +33,15 @@ use crate::{errors::*, WebDB};
const LIMIT: u32 = 50; const LIMIT: u32 = 50;
impl Ctx { impl Ctx {
pub async fn import_forges( async fn client_get_forges(
&self, &self,
starchart_url: Url, starchart_url: &Url,
db: &Box<dyn SCDatabase>, page: usize,
) -> ServiceResult<()> { ) -> ServiceResult<Vec<Forge>> {
let mut page = 1;
loop {
let clean_starchart_url = clean_url(&starchart_url);
let mut url = starchart_url.clone(); let mut url = starchart_url.clone();
url.set_path(ROUTES.forges); url.set_path(ROUTES.forges);
url.set_query(Some(&format!("page={page}"))); url.set_query(Some(&format!("page={page}")));
let mut forges: Vec<Forge> = self Ok(self
.client .client
.get(url) .get(url)
.send() .send()
@ -52,7 +49,18 @@ impl Ctx {
.unwrap() .unwrap()
.json() .json()
.await .await
.unwrap(); .unwrap())
}
pub async fn import_forges(
&self,
starchart_url: Url,
db: &Box<dyn SCDatabase>,
) -> ServiceResult<()> {
let clean_starchart_url = clean_url(&starchart_url);
let mut page = 1;
loop {
let mut forges = self.client_get_forges(&starchart_url, page).await?;
page += 1; page += 1;
if forges.is_empty() { if forges.is_empty() {
break; break;
@ -69,35 +77,50 @@ impl Ctx {
} }
Ok(()) Ok(())
} }
pub async fn bootstrap(&self, db: &Box<dyn SCDatabase>) -> ServiceResult<()> {
let mut known_starcharts = HashSet::with_capacity(self.settings.introducer.nodes.len()); async fn client_get_introducions(
for starchart in self.settings.introducer.nodes.iter() { &self,
let mut page = 1; mut starchart_url: Url,
loop { page: usize,
let mut url = starchart.clone(); ) -> ServiceResult<Vec<Starchart>> {
url.set_path(ROUTES.introducer.list); starchart_url.set_path(ROUTES.introducer.list);
url.set_query(Some(&format!("page={page}"))); starchart_url.set_query(Some(&format!("page={page}")));
let mut nodes: Vec<Starchart> = self Ok(self
.client .client
.get(url) .get(starchart_url)
.send() .send()
.await .await
.unwrap() .unwrap()
.json() .json()
.await .await
.unwrap(); .unwrap())
}
let mut introduce_url = starchart.clone(); pub async fn client_introduce_starchart(&self, mut starchart_url: Url) -> ServiceResult<()> {
introduce_url.set_path(ROUTES.introducer.introduce); starchart_url.set_path(ROUTES.introducer.introduce);
let introduction_payload = Starchart { let introduction_payload = Starchart {
instance_url: self.settings.introducer.public_url.to_string(), instance_url: self.settings.introducer.public_url.to_string(),
}; };
self.client self.client
.post(introduce_url) .post(starchart_url)
.json(&introduction_payload) .json(&introduction_payload)
.send() .send()
.await .await
.unwrap(); .unwrap();
Ok(())
}
pub async fn bootstrap(&self, db: &Box<dyn SCDatabase>) -> ServiceResult<()> {
let mut known_starcharts = HashSet::with_capacity(self.settings.introducer.nodes.len());
for starchart in self.settings.introducer.nodes.iter() {
let mut page = 1;
loop {
let mut nodes = self
.client_get_introducions(starchart.clone(), page)
.await?;
self.client_introduce_starchart(starchart.clone()).await?;
if nodes.is_empty() { if nodes.is_empty() {
break; break;
} }