| 1 | # Copyright (c) 2018-2022, The Monero Project |
| 2 | |
| 3 | # |
| 4 | # All rights reserved. |
| 5 | # |
| 6 | # Redistribution and use in source and binary forms, with or without modification, are |
| 7 | # permitted provided that the following conditions are met: |
| 8 | # |
| 9 | # 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | # conditions and the following disclaimer. |
| 11 | # |
| 12 | # 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 13 | # of conditions and the following disclaimer in the documentation and/or other |
| 14 | # materials provided with the distribution. |
| 15 | # |
| 16 | # 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 17 | # used to endorse or promote products derived from this software without specific |
| 18 | # prior written permission. |
| 19 | # |
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 21 | # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 | # THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 28 | # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | import requests |
| 31 | import json |
| 32 | |
| 33 | class Response(dict): |
| 34 | def __init__(self, d): |
| 35 | for k in d.keys(): |
| 36 | if type(d[k]) == dict: |
| 37 | self[k] = Response(d[k]) |
| 38 | elif type(d[k]) == list: |
| 39 | self[k] = [] |
| 40 | for i in range(len(d[k])): |
| 41 | if type(d[k][i]) == dict: |
| 42 | self[k].append(Response(d[k][i])) |
| 43 | else: |
| 44 | self[k].append(d[k][i]) |
| 45 | else: |
| 46 | self[k] = d[k] |
| 47 | |
| 48 | def __getattr__(self, key): |
| 49 | return self[key] |
| 50 | def __setattr__(self, key, value): |
| 51 | self[key] = value |
| 52 | def __eq__(self, other): |
| 53 | if type(other) == dict: |
| 54 | return self == Response(other) |
| 55 | if self.keys() != other.keys(): |
| 56 | return False |
| 57 | for k in self.keys(): |
| 58 | if self[k] != other[k]: |
| 59 | return False |
| 60 | return True |
| 61 | |
| 62 | class JSONRPC(object): |
| 63 | def __init__(self, url): |
| 64 | self.url = url |
| 65 | |
| 66 | def send_request(self, path, inputs, result_field = None): |
| 67 | res = requests.post( |
| 68 | self.url + path, |
| 69 | data=json.dumps(inputs), |
| 70 | headers={'content-type': 'application/json'}) |
| 71 | res = res.json() |
| 72 | |
| 73 | assert 'error' not in res, res |
| 74 | |
| 75 | if result_field: |
| 76 | res = res[result_field] |
| 77 | |
| 78 | return Response(res) |
| 79 | |
| 80 | def send_json_rpc_request(self, inputs): |
| 81 | return self.send_request("/json_rpc", inputs, 'result') |
| 82 | |
| 83 | |
| 84 |