bitcoin_remote/
json_request.rs

1crate::ix!();
2
3pub enum JSONRPCRequestMode { 
4    EXECUTE, 
5    GET_HELP, 
6    GET_ARGS 
7}
8
9//-------------------------------------------[.cpp/bitcoin/src/rpc/request.h]
10
11pub struct JSONRPCRequest {
12    id:         UniValue,
13    str_method: String,
14    params:     UniValue,
15    mode:       JSONRPCRequestMode, // default = EXECUTE
16    uri:        String,
17    auth_user:  String,
18    peer_addr:  String,
19    context:    Box<dyn Any>,
20}
21
22impl JSONRPCRequest {
23    
24    pub fn parse(&mut self, val_request: &UniValue)  {
25        
26        todo!();
27        /*
28            // Parse request
29        if (!valRequest.isObject())
30            throw JSONRPCError(RPC_INVALID_REQUEST, "Invalid Request object");
31        const UniValue& request = valRequest.get_obj();
32
33        // Parse id now so errors from here on will have the id
34        id = find_value(request, "id");
35
36        // Parse method
37        UniValue valMethod = find_value(request, "method");
38        if (valMethod.isNull())
39            throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method");
40        if (!valMethod.isStr())
41            throw JSONRPCError(RPC_INVALID_REQUEST, "Method must be a string");
42        strMethod = valMethod.get_str();
43        if (fLogIPs)
44            LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s peeraddr=%s\n", SanitizeString(strMethod),
45                this->authUser, this->peerAddr);
46        else
47            LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s\n", SanitizeString(strMethod), this->authUser);
48
49        // Parse params
50        UniValue valParams = find_value(request, "params");
51        if (valParams.isArray() || valParams.isObject())
52            params = valParams;
53        else if (valParams.isNull())
54            params = UniValue(UniValue::VARR);
55        else
56            throw JSONRPCError(RPC_INVALID_REQUEST, "Params must be an array or object");
57        */
58    }
59}
60
61//-------------------------------------------[.cpp/bitcoin/src/rpc/request.cpp]
62
63/**
64  | JSON-RPC protocol.
65  | 
66  | Bitcoin speaks version 1.0 for maximum
67  | compatibility, but uses JSON-RPC 1.1/2.0
68  | standards for parts of the 1.0 standard
69  | that were unspecified (HTTP errors
70  | and contents of 'error').
71  | 
72  | 1.0 spec: http://json-rpc.org/wiki/specification
73  | 
74  | 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html
75  |
76  */
77pub fn jsonrpc_request_obj(
78        str_method: &str,
79        params:     &UniValue,
80        id:         &UniValue) -> UniValue {
81    
82    todo!();
83        /*
84            UniValue request(UniValue::VOBJ);
85        request.pushKV("method", strMethod);
86        request.pushKV("params", params);
87        request.pushKV("id", id);
88        return request;
89        */
90}
91
92pub fn jsonrpc_reply_obj(
93        result: &UniValue,
94        error:  &UniValue,
95        id:     &UniValue) -> UniValue {
96    
97    todo!();
98        /*
99            UniValue reply(UniValue::VOBJ);
100        if (!error.isNull())
101            reply.pushKV("result", NullUniValue);
102        else
103            reply.pushKV("result", result);
104        reply.pushKV("error", error);
105        reply.pushKV("id", id);
106        return reply;
107        */
108}
109
110pub fn jsonrpc_reply(
111        result: &UniValue,
112        error:  &UniValue,
113        id:     &UniValue) -> String {
114    
115    todo!();
116        /*
117            UniValue reply = JSONRPCReplyObj(result, error, id);
118        return reply.write() + "\n";
119        */
120}
121
122pub fn jsonrpc_error(
123        code:    i32,
124        message: &str) -> UniValue {
125    
126    todo!();
127        /*
128            UniValue error(UniValue::VOBJ);
129        error.pushKV("code", code);
130        error.pushKV("message", message);
131        return error;
132        */
133}
134
135/**
136  | Parse JSON-RPC batch reply into a vector
137  |
138  */
139pub fn jsonrpc_process_batch_reply(in_: &UniValue) -> Vec<UniValue> {
140    
141    todo!();
142        /*
143            if (!in.isArray()) {
144            throw std::runtime_error("Batch must be an array");
145        }
146        const size_t num {in.size()};
147        std::vector<UniValue> batch(num);
148        for (const UniValue& rec : in.getValues()) {
149            if (!rec.isObject()) {
150                throw std::runtime_error("Batch member must be an object");
151            }
152            size_t id = rec["id"].get_int();
153            if (id >= num) {
154                throw std::runtime_error("Batch member id is larger than batch size");
155            }
156            batch[id] = rec;
157        }
158        return batch;
159        */
160}