-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbring-cache-up-to-date.py
50 lines (44 loc) · 1.63 KB
/
bring-cache-up-to-date.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
import time
import requests
local = False
reset_from_zero = False # False to continue from where it left off
fly_app_name = "devhub-cache-api-rs"
# ~120 calls for devhub
# ~20 calls for infra
# ~40 calls for events
max_calls = 120 # This is for devhub to catch up to the latest block
base_url = f"http://localhost:8080/" if local else f"https://{fly_app_name}.fly.dev/"
def call_api(count):
url = f"{base_url}proposals" # Replace with your API URL
try:
response = requests.get(url)
if response.status_code == 200:
print(f"{count} API call successful: - response length {response.json().get('total_records')}")
else:
print("API call failed with status code:", response.status_code)
except requests.exceptions.RequestException as e:
print("An error occurred:", e)
except Exception as e:
print("An error2 occurred:", e)
print(response.json())
def reset_cache():
url = f"{base_url}proposals/info/reset" # Replace with your API URL
try:
response = requests.get(url)
if response.status_code == 200:
print("Cache reset successful")
else:
print("Cache reset failed with status code:", response.status_code)
except requests.exceptions.RequestException as e:
print("An error occurred:", e)
def main():
if reset_from_zero:
reset_cache()
count = 0
while count < max_calls:
call_api(count)
count += 1
# 6 calls/minute limit of near blocks https://nearblocks.io/apis#
time.sleep(0.5) # Wait for 11 seconds before the next call
if __name__ == "__main__":
main()