Skip to content

Commit

Permalink
Replace reqwest with ureq
Browse files Browse the repository at this point in the history
Reqwest has rather heavy dependencies, mainly surrounding a non-optional
tokio dependency.

This is inconvenient for build times when using the crate directly in
rust problems.

Closes #17
  • Loading branch information
nuxeh committed Dec 4, 2021
1 parent c04f46a commit 58697c1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ diesel = { version = "1.4.5", features = ["sqlite"], optional = true }
failure = "0.1.8"
html2md = "0.2.10"
regex = "1.4.2"
reqwest = { version = "0.10.9", features = ["blocking"] }
serde = "1.0.117"
serde_derive = "1.0.117"
serde_json = "1.0.59"
tempfile = "3.1.0"
libsqlite3-sys = { version = ">=0.8.0, <0.21.0", features = ["min_sqlite_version_3_7_16", "bundled"], optional = true }
structopt = "0.3.21"
atty = "0.2.14"
ureq = "2.2.0"

[features]
sqlite = ["diesel", "libsqlite3-sys"]
Expand Down
37 changes: 15 additions & 22 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ use crate::{Aoc, Level};
use html2md::parse_html;
use failure::{Error, bail};
use regex::Regex;
use reqwest::blocking::Client;
use reqwest::header;
use std::collections::HashMap;

const BASE: &str = "https://adventofcode.com";

Expand All @@ -19,12 +16,12 @@ fn get_url(aoc: &Aoc) -> Result<String, Error> {
fn get_content(aoc: &Aoc, suffix: &str) -> Result<String, Error> {
let url = format!("{}{}", get_url(aoc)?, suffix);
let cookie = format!("session={}", aoc.cookie);
let input = Client::new()
.get(&url)
.header(header::COOKIE, cookie)
.send()?
.error_for_status()?
.text()?;

let input = ureq::get(&url)
.set("COOKIE", &cookie)
.call()?
.into_string()?;

Ok(input)
}

Expand Down Expand Up @@ -54,21 +51,17 @@ pub fn submit(aoc: &Aoc, solution: &str) -> Result<String, Error> {
let cookie = format!("session={}", aoc.cookie);

let level = match aoc.level {
Level::First => 1,
Level::Second => 2,
Level::First => "1",
Level::Second => "2",
};

let mut params = HashMap::new();
params.insert("level", level.to_string());
params.insert("answer", solution.into());

let resp = Client::new()
.post(&url)
.header(header::COOKIE, cookie)
.form(&params)
.send()?
.error_for_status()?
.text()?;
let resp = ureq::post(&url)
.set("COOKIE", &cookie)
.send_form(&[
("level", level),
("answer", solution),
])?
.into_string()?;

let resp = get_html_section(&resp, "main").unwrap_or_else(|| "".to_string());
let resp = parse_html(&resp);
Expand Down

0 comments on commit 58697c1

Please sign in to comment.