<commit_before>#![allow(dead_code)]
use std::{result};
use std::io::Read;
use std::collections::HashMap;

use reqwest;
use reqwest::{Url, Response};
use reqwest::header::{ContentType};
use serde_json;
use reqwest::Method;

use errors::Error;
use matrix_types::*;


type Result<T> = result::Result<T, Error>;


pub struct MatrixClient {
    base_url: String,
    encoding: String,

    access_token: Option<String>,
    device_id: Option<String>,
    user_id: Option<String>,

    transaction_id: u64,
    client: reqwest::Client,
}


impl MatrixClient {
    pub fn new(base_url: &str) -> Self {
        MatrixClient {
            base_url: String::from(base_url),
            encoding: String::from("utf-8"),
            access_token: None,
            device_id: None,
            user_id: None,
            transaction_id: 0,
            client: reqwest::Client::new(),
        }
    }

    fn get_transaction_id(&mut self) -> u64 {
        self.transaction_id += 1;

        self.transaction_id
    }

    pub fn query(&self,
             method: Method,
             path: &str,
             params: Option<&HashMap<&str, &str>>,
             data: Option<&HashMap<&str, &str>>) -> Result<Response> {

        // Concat the path to the base url and constant string
        let mut uri = self.base_url.clone();
        uri += "/_matrix/client/r0";
        uri += path;

        let url = match params {
            // TODO: an unwrap ok here?
            Some(v) => Url::parse_with_params(&uri, v).unwrap().into_string(),
            None => uri
        };

        let nothing = HashMap::new();

        let mut builder = self.client.request(method.clone(), &url);

        let request = match method {
            Method::Post | Method::Put => {
                builder.header(ContentType::json())
                       .json(data.unwrap_or(&nothing))
            },
            _ => &mut builder,
        };

        Ok(request.send()?)

            /*
        match method {
            Method::Get => {
                client.get(&url)?.send()
            },
            POST => {
                client.post(&url)?
                      .header(ContentType::json())
                      .json(data.unwrap_or(&nothing)).unwrap().send()
            }
            PUT => {
                client.put(&url)?
                      .header(ContentType::json())
                      .json(data.unwrap_or(&nothing)).unwrap().send()
            }
        }
        */
    }

    pub fn auth_query(&self,
                  method: Method,
                  path: &str,
                  params: Option<HashMap<&str, &str>>,
                  data: Option<&HashMap<&str, &str>>) -> Result<Response> {

        let mut real_params = match params {
            Some(v) => v,
            None => HashMap::new(),
        };

        match self.access_token {
            Some(ref v) => {
                real_params.insert("access_token", v);
                self.query(method, path, Some(&real_params), data)
            },
            None => {
                Err(Error::Generic("User must be authenticated first.".to_string()))
            },
        }
    }

    pub fn get(&self, path: &str,
               params: Option<&HashMap<&str, &str>>) -> Result<Response> {
        self.query(Method::Get, path, params, None)
    }

    pub fn auth_get(&self, path: &str,
                    params: Option<HashMap<&str, &str>>) -> Result<Response> {
        self.auth_query(Method::Get, path, params, Option::None)
    }

    pub fn login(&mut self, username: &str, password: &str) -> Result<()> {
        let map = hashmap!{
            "user"     => username,
            "password" => password,
            "type"     => "m.login.password",<commit_msg>Remove unnecessary Option:: namespace prefix<commit_after>#![allow(dead_code)]
use std::{result};
use std::io::Read;
use std::collections::HashMap;

use reqwest;
use reqwest::{Url, Response};
use reqwest::header::{ContentType};
use serde_json;
use reqwest::Method;

use errors::Error;
use matrix_types::*;


type Result<T> = result::Result<T, Error>;


pub struct MatrixClient {
    base_url: String,
    encoding: String,

    access_token: Option<String>,
    device_id: Option<String>,
    user_id: Option<String>,

    transaction_id: u64,
    client: reqwest::Client,
}


impl MatrixClient {
    pub fn new(base_url: &str) -> Self {
        MatrixClient {
            base_url: String::from(base_url),
            encoding: String::from("utf-8"),
            access_token: None,
            device_id: None,
            user_id: None,
            transaction_id: 0,
            client: reqwest::Client::new(),
        }
    }

    fn get_transaction_id(&mut self) -> u64 {
        self.transaction_id += 1;

        self.transaction_id
    }

    pub fn query(&self,
             method: Method,
             path: &str,
             params: Option<&HashMap<&str, &str>>,
             data: Option<&HashMap<&str, &str>>) -> Result<Response> {

        // Concat the path to the base url and constant string
        let mut uri = self.base_url.clone();
        uri += "/_matrix/client/r0";
        uri += path;

        let url = match params {
            // TODO: an unwrap ok here?
            Some(v) => Url::parse_with_params(&uri, v).unwrap().into_string(),
            None => uri
        };

        let nothing = HashMap::new();

        let mut builder = self.client.request(method.clone(), &url);

        let request = match method {
            Method::Post | Method::Put => {
                builder.header(ContentType::json())
                       .json(data.unwrap_or(&nothing))
            },
            _ => &mut builder,
        };

        Ok(request.send()?)

            /*
        match method {
            Method::Get => {
                client.get(&url)?.send()
            },
            POST => {
                client.post(&url)?
                      .header(ContentType::json())
                      .json(data.unwrap_or(&nothing)).unwrap().send()
            }
            PUT => {
                client.put(&url)?
                      .header(ContentType::json())
                      .json(data.unwrap_or(&nothing)).unwrap().send()
            }
        }
        */
    }

    pub fn auth_query(&self,
                  method: Method,
                  path: &str,
                  params: Option<HashMap<&str, &str>>,
                  data: Option<&HashMap<&str, &str>>) -> Result<Response> {

        let mut real_params = match params {
            Some(v) => v,
            None => HashMap::new(),
        };

        match self.access_token {
            Some(ref v) => {
                real_params.insert("access_token", v);
                self.query(method, path, Some(&real_params), data)
            },
            None => {
                Err(Error::Generic("User must be authenticated first.".to_string()))
            },
        }
    }

    pub fn get(&self, path: &str,
               params: Option<&HashMap<&str, &str>>) -> Result<Response> {
        self.query(Method::Get, path, params, None)
    }

    pub fn auth_get(&self, path: &str,
                    params: Option<HashMap<&str, &str>>) -> Result<Response> {
        self.auth_query(Method::Get, path, params, None)
    }

    pub fn login(&mut self, username: &str, password: &str) -> Result<()> {
        let map = hashmap!{
            "user"     => username,
            "password" => password,
            "type"     => "m.login.password",
{'fraction_non_alphanumeric': 0.1437491696559054, 'fraction_numerical': 0.002125680882157566, 'mean_word_length': 1.6723464678736244, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3894784', 'n_tokens_mistral': 2200, 'n_tokens_neox': 2135, 'n_words': 467}