From 46497bbae37f89e449b7a049663fe774843beb9b Mon Sep 17 00:00:00 2001
From: Teddy Wing
Date: Sat, 12 Jun 2021 18:25:49 +0200
Subject: Switch from 'reqwest' to 'ureq'; Remove async
Remove all async from the project by switching from 'reqwest' to 'ureq'.
This should make the code simpler, and hopefully enable us to try out
multithreading.
---
 src/github.rs | 31 ++++++++++++-------------------
 src/main.rs   | 19 +++----------------
 2 files changed, 15 insertions(+), 35 deletions(-)
(limited to 'src')
diff --git a/src/github.rs b/src/github.rs
index 58f8983..5289a0d 100644
--- a/src/github.rs
+++ b/src/github.rs
@@ -16,7 +16,6 @@
 // along with Reflectub. If not, see .
 
 
-use reqwest::ClientBuilder;
 use serde::Deserialize;
 use thiserror;
 
@@ -30,11 +29,11 @@ const USER_AGENT: &'static str = concat!(
 
 #[derive(Debug, thiserror::Error)]
 pub enum Error {
-    #[error("request error")]
-    Http(#[from] reqwest::Error),
+    #[error("GitHub request error")]
+    Http(#[from] ureq::Error),
 
-    #[error("request header error")]
-    Header(#[from] reqwest::header::InvalidHeaderValue),
+    #[error("GitHub I/O error")]
+    Io(#[from] std::io::Error),
 }
 
 
@@ -61,30 +60,24 @@ impl Repo {
 
 
 /// Fetch all GitHub repositories for the given user.
-pub async fn fetch_repos(github_username: &str) -> Result, Error> {
-    let mut headers = reqwest::header::HeaderMap::new();
-    headers.insert("Accept", "application/vnd.github.v3+json".parse()?);
-
-    let client = ClientBuilder::new()
+pub fn fetch_repos(github_username: &str) -> Result, Error> {
+    let agent = ureq::AgentBuilder::new()
         .user_agent(USER_AGENT)
-        .default_headers(headers)
-        .build()?;
+        .build();
 
     let mut repos = Vec::new();
 
     for i in 1.. {
-        let repo_page = client.request(
-            reqwest::Method::GET,
-            format!(
+        let repo_page: Vec = agent.get(
+            &format!(
                 "https://api.github.com/users/{}/repos?page={}&per_page=100&sort=updated",
                 github_username,
                 i,
             ),
         )
-            .send()
-            .await?
-            .json::>()
-            .await?;
+            .set("Accept", "application/vnd.github.v3+json")
+            .call()?
+            .into_json()?;
 
         if repo_page.is_empty() {
             break;
diff --git a/src/main.rs b/src/main.rs
index 4b36264..4e4d580 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -24,8 +24,6 @@ use filetime;
 use getopts::Options;
 use parse_size::parse_size;
 use rusqlite;
-use tokio;
-use tokio_stream::StreamExt;
 
 use reflectub::{database, git, github};
 
@@ -38,18 +36,7 @@ use std::sync::{Arc, Mutex};
 
 
 fn main() {
-    let rt = tokio::runtime::Builder::new_multi_thread()
-        .enable_io()
-        .enable_time()
-        .worker_threads(4)
-        .build()
-        .unwrap();
-    // let rt = tokio::runtime::Runtime::new()
-    let _rt_guard = rt.enter();
-
-    let result = rt.block_on(run());
-
-    match result {
+    match run() {
         Ok(_) => (),
         Err(e) => {
             eprint!("error");
@@ -72,7 +59,7 @@ fn print_usage(opts: &Options) {
     );
 }
 
-async fn run() -> anyhow::Result<()> {
+fn run() -> anyhow::Result<()> {
     let args: Vec = env::args().collect();
 
     let mut opts = Options::new();
@@ -123,7 +110,7 @@ async fn run() -> anyhow::Result<()> {
     let base_cgitrc = opt_matches.opt_str("cgitrc")
         .map(|s| PathBuf::from(s));
 
-    let repos = github::fetch_repos(username).await?;
+    let repos = github::fetch_repos(username)?;
 
     let mut db = database::Db::connect(&database_file)
         .context("unable to connect to database")?;
-- 
cgit v1.2.3