cnet 0.0.2

A reqwest wrapper providing fast dirty connections
Documentation
use reqwest;

use serde_json::{json, Value};
use std::io::Read;

pub fn getstr(url: &str) -> Option<String> {
    reqwest::blocking::get(url).ok().and_then(|mut r| {
        let mut s = String::new();
        r.read_to_string(&mut s).ok().and_then(|_| Some(s))
    })
}

pub fn getjson(url: &str) -> Option<Value> {
    getstr(url).and_then(|s| serde_json::from_str(&s).ok())
}

fn bitly(url: &str, token: &str) -> Value {
    // bitly api
    let client = reqwest::blocking::Client::new();
    let api = "https://api-ssl.bitly.com/v3/shorten";
    let params = [("access_token", token), ("longUrl", url)];
    client
        .post(api)
        .form(&params)
        .send()
        .ok()
        .and_then(|mut r| {
            let mut s = String::new();
            r.read_to_string(&mut s).ok().and_then(|_| Some(s))
        })
        .and_then(|s| serde_json::from_str(&s).ok())
        .unwrap_or_else(|| json!({"error": "failed to get"}))
}