From bc87f7e346de79f7b00de347c8ea0ff4cf664982 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Tue, 30 Apr 2019 02:19:58 -0400 Subject: [PATCH] move get url into own function --- Cargo.lock | 2 ++ src/main.rs | 16 ++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9f2cf25..1ac8609 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/src/main.rs b/src/main.rs index 55d4ab4..0490e53 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> { 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> { Ok(()) } +fn get_url(base: &String, path: &String, port: Option) -> Result { + 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, String) { let path = endpoint.endpoint.unwrap_or_default(); let label = endpoint.label.unwrap_or_else(|| path.clone());