use serde_json::Value;
use serde_json::json;
use super::constants::JSONRPC_DEFAULT_ID;
use super::constants::JSONRPC_FIELD;
use super::constants::JSONRPC_FIELD_ID;
use super::constants::JSONRPC_FIELD_METHOD;
use super::constants::JSONRPC_FIELD_PARAMS;
use super::constants::JSONRPC_VERSION;
pub(super) struct BrpJsonRpcBuilder {
method: String,
params: Option<Value>,
id: u64,
}
impl BrpJsonRpcBuilder {
pub(super) fn new(method: impl Into<String>) -> Self {
Self {
method: method.into(),
params: None,
id: JSONRPC_DEFAULT_ID,
}
}
pub(super) fn params(mut self, params: Value) -> Self {
self.params = Some(params);
self
}
pub(super) fn build(self) -> Value {
let mut request = json!({
JSONRPC_FIELD: JSONRPC_VERSION,
JSONRPC_FIELD_METHOD: self.method,
JSONRPC_FIELD_ID: self.id
});
if let Some(params) = self.params {
request[JSONRPC_FIELD_PARAMS] = params;
} else {
request[JSONRPC_FIELD_PARAMS] = json!(null);
}
request
}
}