-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlichessNetUtils.nim
134 lines (108 loc) · 4.41 KB
/
lichessNetUtils.nim
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
import std/[
net,
json,
strutils,
strformat,
tables,
os,
options,
random,
httpclient,
streams
]
import log
export HttpClient, HttpMethod
const
maxNumRetries = 5
maxNumEmptyReplies = 100
proc getRequestsSession*(): HttpClient =
newHttpClient()
iterator streamEvents*(host: string, path: string, token: string): Option[JsonNode] =
for i in 1..maxNumRetries:
var numEmptyReplies = 0
try:
let s = newSocket()
wrapSocket(newContext(), s)
s.connect(host, Port(443))
let req = &"GET {path} HTTP/1.1\r\nHost: {host}\r\nAuthorization: Bearer {token}\r\nAccept: x-ndjson\r\n\r\n"
logDebug "Sending stream request: ", req
s.send(req)
while true:
let line = s.recvLine(timeout = 15_000)
if line.strip.len == 0:
numEmptyReplies += 1
logDebug "Received empty line"
if numEmptyReplies > maxNumEmptyReplies:
logInfo "Retrying after getting too many empty replies: ", numEmptyReplies
break
else:
numEmptyReplies = 0
logDebug "Received line: ", line
if line.strip.len > 0 and line.strip[0] == '{':
let json = line.parseJson
yield some json
elif line.startsWith("HTTP/1.1 "):
let
words = line.splitWhitespace
status = if words.len >= 2: words[1].parseInt else: 418
if status == 200:
continue
elif status == 429:
logWarn "Rate limited."
sleep 60_000 + rand(0..10_000)
break
else:
raise newException(IOError, &"Unexpected response.\nRequest: {req}\nStatus: {status}\nError: {line}")
else:
# This is just a hack so that the loop gets a regular response, so that I don't need to do something multithreaded
yield none JsonNode
except CatchableError:
if i == maxNumRetries:
raise
logInfo "Retrying after getting an exception: ", getCurrentExceptionMsg()
sleep 500
proc jsonResponse*(client: var HttpClient, httpMethod: HttpMethod, url: string, token: string, payload = initTable[string, string]()): JsonNode =
client.headers = newHttpHeaders({
"Authorization": "Bearer " & token,
"Accept": "application/json",
"Content-Type": "application/json"
})
let (body, status) = block:
var
body: string
status: int
for i in 1..maxNumRetries:
try:
let
response = client.request(url, httpMethod = httpMethod, body = $(%payload))
statusNumberStrings = response.status.splitWhitespace
if statusNumberStrings.len == 0:
raise newException(IOError, "Unknown status code: " & response.status)
status = statusNumberStrings[0].parseInt
body = response.bodyStream.readAll
if status == 429: # rate limited, should wait at least a minute
logWarn "Rate limited."
sleep 60_000 + rand(0..10_000)
else:
break
except Exception:
if i == maxNumRetries:
raise
logInfo "Retrying after getting an exception: ", getCurrentExceptionMsg()
sleep 500
(body, status)
if status != 200:
var errorMsg: string = ""
try:
errorMsg = body.parseJson{"error"}.getStr
except JsonParsingError:
errorMsg = body
errorMsg = &"Unexpected response.\nURL: {url}\nStatus: {status}\nError: {errorMsg}"
raise newException(IOError, errorMsg)
result = body.parseJson
proc jsonResponse*(httpMethod: HttpMethod, query: string, token: string, payload = initTable[string, string]()): JsonNode =
var client = getRequestsSession()
try:
client.jsonResponse(httpMethod, query, token, payload)
finally:
client.close()