/* 
 * Vault
 *
 * Hashicorp Vault API
 *
 * OpenAPI spec version: 0.7.2
 * 
 * Generated by: https://github.com/swagger-api/swagger-codegen.git
 */

use std::rc::Rc;
use std::borrow::Borrow;

use hyper;
use serde_json;
use futures;
use futures::{Future, Stream};

use super::{Error, configuration};

pub struct PkiBackendApiClient<C: hyper::client::Connect> {
    configuration: Rc<configuration::Configuration<C>>,
}

impl<C: hyper::client::Connect> PkiBackendApiClient<C> {
    pub fn new(configuration: Rc<configuration::Configuration<C>>) -> PkiBackendApiClient<C> {
        PkiBackendApiClient {
            configuration: configuration,
        }
    }
}

pub trait PkiBackendApi {
    fn generate_cert(&self, x_vault_token: &str, mount: &str, name: &str, body: ::models::GenerateCertificateParameters) -> Box<Future<Item = ::models::GenerateCertificateResponse, Error = Error>>;
    fn read_cert(&self, mount: &str, serial: &str) -> Box<Future<Item = ::models::CertificateResponse, Error = Error>>;
}


impl<C: hyper::client::Connect>PkiBackendApi for PkiBackendApiClient<C> {
    fn generate_cert(&self, x_vault_token: &str, mount: &str, name: &str, body: ::models::GenerateCertificateParameters) -> Box<Future<Item = ::models::GenerateCertificateResponse, Error = Error>> {
        let configuration: &configuration::Configuration<C> = self.configuration.borrow();

        let method = hyper::Method::Post;

        let uri_str = format!("{}/{mount}/issue/{name}", configuration.base_path, mount=mount, name=name);

        let uri = uri_str.parse();
        // TODO(farcaller): handle error
        // if let Err(e) = uri {
        //     return Box::new(futures::future::err(e));
        // }
        let mut req = hyper::Request::new(method, uri.unwrap());

        {
            let mut headers = req.headers_mut();
            headers.set_raw("X-Vault-Token", x_vault_token);
        }

        let serialized = serde_json::to_string(&body).unwrap();
        req.headers_mut().set(hyper::header::ContentType::json());
        req.headers_mut().set(hyper::header::ContentLength(serialized.len() as u64));
        req.set_body(serialized);

        // send request
        Box::new(
            configuration.client.request(req).and_then(|res| { res.body().concat2() })
            .map_err(|e| Error::from(e))
            .and_then(|body| {
                let parsed: Result<::models::GenerateCertificateResponse, _> = serde_json::from_slice(&body);
                parsed.map_err(|e| Error::from(e))
            }).map_err(|e| Error::from(e))
        )
    }

    fn read_cert(&self, mount: &str, serial: &str) -> Box<Future<Item = ::models::CertificateResponse, Error = Error>> {
        let configuration: &configuration::Configuration<C> = self.configuration.borrow();

        let method = hyper::Method::Get;

        let uri_str = format!("{}/{mount}/cert/{serial}", configuration.base_path, mount=mount, serial=serial);

        let uri = uri_str.parse();
        // TODO(farcaller): handle error
        // if let Err(e) = uri {
        //     return Box::new(futures::future::err(e));
        // }
        let mut req = hyper::Request::new(method, uri.unwrap());



        // send request
        Box::new(
            configuration.client.request(req).and_then(|res| { res.body().concat2() })
            .map_err(|e| Error::from(e))
            .and_then(|body| {
                let parsed: Result<::models::CertificateResponse, _> = serde_json::from_slice(&body);
                parsed.map_err(|e| Error::from(e))
            }).map_err(|e| Error::from(e))
        )
    }

}
