Refactor api code

This commit is contained in:
Zed 2019-09-06 03:37:12 +02:00
commit 43bf6735d4
13 changed files with 414 additions and 397 deletions

32
src/api/utils.nim Normal file
View file

@ -0,0 +1,32 @@
import httpclient, asyncdispatch, htmlparser
import strutils, json, xmltree, uri
template newClient*() {.dirty.} =
var client = newAsyncHttpClient()
defer: client.close()
client.headers = headers
proc fetchHtml*(url: Uri; headers: HttpHeaders; jsonKey = ""): Future[XmlNode] {.async.} =
newClient()
var resp = ""
try:
resp = await client.getContent($url)
except:
return nil
if jsonKey.len > 0:
let json = parseJson(resp)[jsonKey].str
return parseHtml(json)
else:
return parseHtml(resp)
proc fetchJson*(url: Uri; headers: HttpHeaders): Future[JsonNode] {.async.} =
newClient()
var resp = ""
try:
resp = await client.getContent($url)
result = parseJson(resp)
except:
return nil