forked from Reecepbcups/cosmos-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestsHandler.py
153 lines (128 loc) · 4.65 KB
/
RequestsHandler.py
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
import json
import httpx
import CONFIG
from CONFIG import KV_STORE
from HELPERS import hide_rest_data, hide_rpc_data, increment_call_value
from HELPERS_TYPES import CallType, Mode
timeout = httpx.Timeout(30.0, connect=5.0, read=4.0)
def set_cache_for_time_if_valid(
status_code: int,
call_key: str,
cache_seconds: int,
redis_key: str,
res: dict,
use_hset: bool = False,
second_key: str = "", # the params / args
):
increment_call_value(call_key)
if cache_seconds == Mode.NO_CACHE.value:
# useful for broadcasted txs
return
if status_code == 200:
# -2 = clear when a new block is minted
if cache_seconds == Mode.FOR_BLOCK_TIME.value:
if CONFIG.DEFAULT_CACHE_SECONDS > 0:
cache_seconds = CONFIG.DEFAULT_CACHE_SECONDS
else:
cache_seconds = 6
if use_hset:
# Expire timeout is only changed on creation.
# Future: per sub key timeouts?
KV_STORE.hset(redis_key, second_key, json.dumps(res), cache_seconds)
# KV_STORE.delete(redis_key) # Why was this here?
else:
KV_STORE.set(redis_key, json.dumps(res), cache_seconds)
class RestApiHandler:
def handle_single_rest_get_requests(
self, path, key, cache_seconds: int, param_args, headers: dict
) -> dict:
try:
req = httpx.get(
f"{CONFIG.REST_URL}/{path}", params=param_args, headers=headers
)
except:
req = httpx.get(
f"{CONFIG.BACKUP_REST_URL}/{path}", params=param_args, headers=headers
)
res = hide_rest_data(req.json(), path)
set_cache_for_time_if_valid(
req.status_code,
CallType.REST_GET_OUTBOUND.value,
cache_seconds,
key,
res,
use_hset=True,
second_key=str(param_args),
)
return res
# This breaks right now, very few ever will do this. Needs to be done in the future though, but not a priority
# def handle_single_rest_post_requests(self, path, data: dict) -> dict:
# # simulate, txs
# try:
# req = httpx.post(
# f"{CONFIG.RPC_URL}/{path}", headers=CONFIG.HEADERS, data=data
# )
# except:
# req = httpx.post(
# f"{CONFIG.BACKUP_RPC_URL}/{path}",
# headers=CONFIG.HEADERS,
# data=data,
# )
# return req.json()
class RPCHandler:
def handle_batch_http_request(self, REQ_DATA: list) -> dict:
"""
This function handles batch http requests from TendermintClient34.create client
"""
# TODO: add cache here in the future possible? since each elem in the list has a method and params like below
# TODO: add hide_rpc_data here for each if they req the status method
try:
req = httpx.post(f"{CONFIG.RPC_URL}", json=REQ_DATA)
except:
req = httpx.post(
f"{CONFIG.BACKUP_RPC_URL}",
json=REQ_DATA,
)
return req.json()
def handle_single_rpc_post_request(
self, data, key, method, cache_seconds, use_hset: bool = False
) -> dict:
# TODO: add round robin query here for multiple RPC nodes. If a node errors, save to cache for X period to not use (unless its the only 1)
try:
req = httpx.post(f"{CONFIG.RPC_URL}", data=data, timeout=timeout)
except:
req = httpx.post(f"{CONFIG.BACKUP_RPC_URL}", data=data, timeout=timeout)
# only saves to cache if the request was successful
res = hide_rpc_data(req.json(), method)
set_cache_for_time_if_valid(
req.status_code,
CallType.RPC_POST_OUTBOUND.value,
cache_seconds,
key,
res,
use_hset,
second_key=str(data),
)
return res
def handle_single_rpc_get_requests(
self, path, key, param_args, cache_seconds: int, use_hset: bool = False
) -> dict:
try:
req = httpx.get(
f"{CONFIG.RPC_URL}/{path}", params=param_args, timeout=timeout
)
except Exception as e:
req = httpx.get(
f"{CONFIG.BACKUP_RPC_URL}/{path}", params=param_args, timeout=timeout
)
res = hide_rpc_data(req.json(), path)
set_cache_for_time_if_valid(
req.status_code,
CallType.RPC_GET_OUTBOUND.value,
cache_seconds,
key,
res,
use_hset,
second_key=str(param_args),
)
return res