diff options
author | Teddy Wing | 2021-06-12 18:25:49 +0200 |
---|---|---|
committer | Teddy Wing | 2021-06-12 18:25:49 +0200 |
commit | 46497bbae37f89e449b7a049663fe774843beb9b (patch) | |
tree | 89403988f141f61e0e2a78d655730e7cbc19dad8 /src | |
parent | 6a6bacf975156a045cf48f9e58520f0bc0f54429 (diff) | |
download | reflectub-46497bbae37f89e449b7a049663fe774843beb9b.tar.bz2 |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/github.rs | 31 | ||||
-rw-r--r-- | src/main.rs | 19 |
2 files changed, 15 insertions, 35 deletions
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 <https://www.gnu.org/licenses/>. -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<Vec<Repo>, 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<Vec<Repo>, 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<Repo> = agent.get( + &format!( "https://api.github.com/users/{}/repos?page={}&per_page=100&sort=updated", github_username, i, ), ) - .send() - .await? - .json::<Vec<Repo>>() - .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<String> = 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")?; |