From 5c87f3368b174f8e8525f6a522fbaa07e03c98f5 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 23 Feb 2019 16:10:49 +0100 Subject: Fetch stations, dump response json to stdout. --- src/lib.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e4ca76d..c65fce1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,15 +16,34 @@ along with this program. If not, see . */ +use reqwest; + +const API_BASE: &'static str = "https://oslobysykkel.no/api/v1"; + +fn url_for(endpoint: &str) -> String { + [API_BASE, endpoint].join("/") +} + pub struct Api { + api_key: String, + client: reqwest::Client, } impl Api { - pub fn new(_api_key: String) -> Api { - Api { } + pub fn new(api_key: String) -> Api { + let mut hdrs = reqwest::header::HeaderMap::new(); + hdrs.insert("client-identifier", reqwest::header::HeaderValue::from_str(&api_key).unwrap()); + + let client = reqwest::Client::builder() + .default_headers(hdrs) + .build().unwrap(); + + Api { api_key, client } } pub fn stations(&self) -> String { - String::from("Hey, bop - alula!") + self.client.get(&url_for("stations")) + .send().unwrap() + .text().unwrap() } } -- cgit v1.2.3