1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#[cfg(not(target_os = "windows"))]
use hyperlocal::{UnixConnector};
use tokio_core::reactor::Core;
use hyper::{Client, Uri, Request, Method};
use hyper::client::HttpConnector;
use hyper::header::ContentType;
use futures::Future;
use futures::Stream;
use serde_json;
use serde_json::Value;
use std::io::{self};
use utils;

///Socket
#[derive(Clone)]
pub struct Socket{
    /// Socket address
    pub address: String
}

impl Socket{
    /// Create new socket
    pub fn new(address: &str) -> Self{
        Socket{
            address: address.to_string()
        }
    }

    /// Returns the Socket address
    pub fn address(&self) -> String{
        self.address.clone()
    }

    /// Returns if the Socket is a Unix one
    pub fn is_unix(&self) -> bool{
        match utils::is_http_scheme(&self.address()){
            Some(scheme) =>{
                if scheme == "http"{
                    false
                }
                else {
                    true
                }
            },
            None => {
                true
            }
        }
    }

    /// Execute the request on the client
    #[cfg(target_os = "windows")]
    pub fn request(&mut self, uri: Uri, method: Method, body: Option<String>) -> Option<Value>{
        let mut core = Core::new().unwrap();
        let handle = core.handle();
        let client = Client::configure().connector(HttpConnector::new(4, &handle)).build(&core.handle());

        let mut request = Request::new(method, uri);
        request.headers_mut().set(ContentType::json());
        if let Some(b) = body{
            request.set_body(b);
        }

        let work = client.request(request).and_then(|res| {
            res.body().concat2().and_then(move |body| {
                let v: Value = serde_json::from_slice(&body).map_err(|e| {
                    io::Error::new(
                        io::ErrorKind::Other,
                        e
                    )
                })?;

                Ok(v)
            })
        });

        match core.run(work){
            Ok(item) =>{
                Some(item)
            },
            Err(e)=>{
                error!("Error message: {}", e);
                None
            }
        }
    }

    /// Execute the request on the client
    #[cfg(not(target_os = "windows"))]
    pub fn request(&mut self, uri: Uri, method: Method, body: Option<String>) -> Option<Value>{
        let mut core = Core::new().unwrap();
        let handle = core.handle();

        if self.is_unix() {
            let client = Client::configure().connector(UnixConnector::new(handle)).build(&core.handle());
            let mut request = Request::new(method, uri);
            request.headers_mut().set(ContentType::json());
            if let Some(b) = body {
                request.set_body(b);
            }

            let work = client.request(request).and_then(|res| {
                res.body().concat2().and_then(move |body| {
                    let v: Value = serde_json::from_slice(&body).map_err(|e| {
                        io::Error::new(
                            io::ErrorKind::Other,
                            e
                        )
                    })?;

                    Ok(v)
                })
            });

            match core.run(work){
                Ok(item) =>{
                    Some(item)
                },
                Err(e)=>{
                    error!("Error message: {}", e);
                    None
                }
            }
        }
        else{
            let client = Client::configure().connector(HttpConnector::new(4, &handle)).build(&core.handle());
            let mut request = Request::new(method, uri);
            request.headers_mut().set(ContentType::json());
            if let Some(b) = body {
                request.set_body(b);
            }

            let work = client.request(request).and_then(|res| {
                res.body().concat2().and_then(move |body| {
                    let v: Value = serde_json::from_slice(&body).map_err(|e| {
                        io::Error::new(
                            io::ErrorKind::Other,
                            e
                        )
                    })?;

                    Ok(v)
                })
            });

            match core.run(work){
                Ok(item) =>{
                    Some(item)
                },
                Err(e)=>{
                    error!("Error message: {}", e);
                    None
                }
            }
        }
    }
}