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
use bytes::{BufMut, Bytes};
use serde_derive::{Deserialize, Serialize};
use serde_json::Value;

use crate::parse::{generate_request_headers, split_bytes};
use crate::response::Error;
use crate::types::Params;

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct Request {
    jsonrpc: String,
    method: String,
    id: String,
    params: Params,
    path: String,
    host: String,
}

impl Request {
    pub fn new(method: String, id: String, params: Params, path: String, host: String) -> Self {
        let jsonrpc = "2.0".into();

        Request {
            jsonrpc,
            method,
            id,
            params,
            path,
            host,
        }
    }

    pub fn parse_from_json(value: Value) -> Result<Self, Error> {
        let id = if let Some(id) = value.get("id") {
            if id.is_number() {
                id.as_u64().unwrap_or(0).to_string()
            } else if id.is_string() {
                id.as_str().unwrap().into()
            } else {
                " ".into()
            }
        } else {
            " ".into()
        };

        // check if json is response
        if value.get("result").is_some() || value.get("error").is_some() {
            return Err(Error::InvalidRequest("".into(), id));
        }

        if value.get("method").is_none() {
            return Err(Error::MethodNotFound("".into(), id));
        }

        let method = value.get("method").unwrap().as_str().unwrap().into();

        let params = if let Some(params) = value.get("params") {
            params.clone()
        } else {
            Value::Null
        };

        let jsonrpc = "2.0".into();
        let path = "/".into();
        let host = "DEFAULT".into();

        Ok(Request {
            jsonrpc,
            method,
            id,
            params,
            path,
            host,
        })
    }

    pub fn parse(bytes: Bytes) -> Result<Self, Error> {
        split_bytes(bytes).and_then(|value| Request::parse_from_json(value))
    }

    pub fn parse_from_json_bytes(bytes: Bytes) -> Result<Self, Error> {
        serde_json::from_slice(&bytes[..])
            .or(Err(Error::ParseError(None)))
            .and_then(|value| Request::parse_from_json(value))
    }

    pub fn deparse(&self) -> Bytes {
        let body = serde_json::to_string(&self).unwrap();

        let body_bytes = body.as_bytes();

        let mut headers =
            generate_request_headers(self.path.clone(), self.host.clone(), body_bytes.len());
        headers.put(body_bytes);
        headers.freeze()
    }

    pub fn method(&self) -> &String {
        &self.method
    }

    pub fn params(&self) -> &Params {
        &self.params
    }

    pub fn id(&self) -> &String {
        &self.id
    }
}