Clean up mbedtls example

This commit is contained in:
Martin Algesten
2022-01-31 09:44:17 +01:00
parent 5dbaa9a256
commit 4f3ea15523

View File

@@ -1,39 +1,12 @@
use std::io::{self, Read};
use std::io::Read;
use std::sync::Arc;
use std::time::Duration;
use std::{env, error, fmt, result};
pub mod mbedtls_connector;
mod mbedtls_connector;
use log::{error, info};
use ureq;
#[derive(Debug)]
struct Oops(String);
impl From<io::Error> for Oops {
fn from(e: io::Error) -> Oops {
Oops(e.to_string())
}
}
impl From<ureq::Error> for Oops {
fn from(e: ureq::Error) -> Oops {
Oops(e.to_string())
}
}
impl error::Error for Oops {}
impl fmt::Display for Oops {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
type Result<T> = result::Result<T, Oops>;
fn get(agent: &ureq::Agent, url: &str) -> Result<Vec<u8>> {
fn get(agent: &ureq::Agent, url: &str) -> Result<Vec<u8>, ureq::Error> {
let response = agent.get(url).call()?;
let mut reader = response.into_reader();
let mut bytes = vec![];
@@ -42,17 +15,14 @@ fn get(agent: &ureq::Agent, url: &str) -> Result<Vec<u8>> {
}
fn get_and_write(agent: &ureq::Agent, url: &str) {
info!("🕷️ {}", url);
println!("🕷️ {}", url);
match get(agent, url) {
Ok(_) => info!("Good: ✔️ {}\n", url),
Err(e) => error!("Bad: ⚠️ {} {}\n", url, e),
Ok(_) => println!("Good: ✔️ {}\n", url),
Err(e) => println!("Bad: ⚠️ {} {}\n", url, e),
}
}
fn main() -> Result<()> {
let _args = env::args();
env_logger::init();
fn main() -> Result<(), ureq::Error> {
let agent = ureq::builder()
.tls_connector(Arc::new(mbedtls_connector::MbedTlsConnector::new(
mbedtls::ssl::config::AuthMode::None,
@@ -61,7 +31,7 @@ fn main() -> Result<()> {
.timeout(Duration::from_secs(20))
.build();
get_and_write(&agent, "https://example.com/");
get_and_write(&agent, "https://httpbin.org/get");
Ok(())
}