move get url into own function

master
Edward Shen 2019-04-30 02:19:58 -04:00
parent 872735d944
commit bc87f7e346
Signed by: edward
GPG Key ID: F350507060ED6C90
2 changed files with 12 additions and 6 deletions

2
Cargo.lock generated
View File

@ -1,3 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "adler32"
version = "1.0.3"

View File

@ -2,7 +2,7 @@ extern crate reqwest;
extern crate ron;
extern crate serde;
use reqwest::{Client, Url};
use reqwest::{Client, Url, UrlError};
use ron::de::from_str;
use serde::Deserialize;
use std::error::Error;
@ -47,11 +47,7 @@ fn main() -> Result<(), Box<Error>> {
for website_conf in config.websites {
for endpoint in website_conf.endpoints {
let (label, path, port, code, body) = get_endpoint_info(endpoint);
let mut url = Url::parse(&website_conf.base)?.join(&path)?;
if let Err(e) = url.set_port(port) {
println!("{:?}", e);
}
let url = url.into_string();
let url = get_url(&website_conf.base, &path, port)?;
let mut res = client.get(&url).send()?;
let does_code_match = res.status() == code;
@ -93,6 +89,14 @@ fn main() -> Result<(), Box<Error>> {
Ok(())
}
fn get_url(base: &String, path: &String, port: Option<u16>) -> Result<String, UrlError> {
let mut url = Url::parse(base)?.join(path)?;
if let Err(e) = url.set_port(port) {
println!("{:?}", e);
}
Ok(url.into_string())
}
fn get_endpoint_info(endpoint: EndpointConf) -> (String, String, Option<u16>, u16, String) {
let path = endpoint.endpoint.unwrap_or_default();
let label = endpoint.label.unwrap_or_else(|| path.clone());