Simplify error parser
This commit is contained in:
parent
2642e5efe4
commit
2fa76db099
|
@ -48,7 +48,7 @@ proc fetch*(url: Uri; retried=false; oldApi=false): Future[JsonNode] {.async.} =
|
||||||
|
|
||||||
result = parseJson(body)
|
result = parseJson(body)
|
||||||
|
|
||||||
if result{"errors"} != nil and result{"errors"}[0]{"code"}.getInt == 200:
|
if result{"errors"} != nil and result.getError == forbidden:
|
||||||
keepToken = false
|
keepToken = false
|
||||||
echo "bad token"
|
echo "bad token"
|
||||||
except:
|
except:
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
import json, strutils, options, tables, times, math
|
import json, strutils, options, tables, times, math
|
||||||
import types, parserutils
|
import types, parserutils
|
||||||
|
|
||||||
proc parseError(js: JsonNode): Error =
|
|
||||||
if js == nil or js.kind != JArray or js.len < 1: return null
|
|
||||||
return Error(js[0]{"code"}.getInt)
|
|
||||||
|
|
||||||
proc parseProfile(js: JsonNode; id=""): Profile =
|
proc parseProfile(js: JsonNode; id=""): Profile =
|
||||||
if js == nil: return
|
if js == nil: return
|
||||||
result = Profile(
|
result = Profile(
|
||||||
|
@ -31,7 +27,7 @@ proc parseUserShow*(js: JsonNode; username: string): Profile =
|
||||||
if js == nil: return
|
if js == nil: return
|
||||||
with error, js{"errors"}:
|
with error, js{"errors"}:
|
||||||
result = Profile(username: username)
|
result = Profile(username: username)
|
||||||
if parseError(error) == suspended:
|
if error.getError == suspended:
|
||||||
result.suspended = true
|
result.suspended = true
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -41,7 +37,7 @@ proc parseGraphProfile*(js: JsonNode; username: string): Profile =
|
||||||
if js == nil: return
|
if js == nil: return
|
||||||
with error, js{"errors"}:
|
with error, js{"errors"}:
|
||||||
result = Profile(username: username)
|
result = Profile(username: username)
|
||||||
if parseError(error) == suspended:
|
if error.getError == suspended:
|
||||||
result.suspended = true
|
result.suspended = true
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,10 @@ template `with`*(ident; value: JsonNode; body): untyped =
|
||||||
template getCursor*(js: JsonNode): string =
|
template getCursor*(js: JsonNode): string =
|
||||||
js{"content", "operation", "cursor", "value"}.getStr
|
js{"content", "operation", "cursor", "value"}.getStr
|
||||||
|
|
||||||
|
template getError*(js: JsonNode): Error =
|
||||||
|
if js.kind != JArray or js.len == 0: null
|
||||||
|
else: Error(js[0]{"code"}.getInt)
|
||||||
|
|
||||||
template parseTime(time: string; f: static string; flen: int): Time =
|
template parseTime(time: string; f: static string; flen: int): Time =
|
||||||
if time.len != flen: return
|
if time.len != flen: return
|
||||||
parseTime(time, f, localTimezone).utc.toTime
|
parseTime(time, f, localTimezone).utc.toTime
|
||||||
|
|
|
@ -22,6 +22,7 @@ proc getHmac*(data: string): string =
|
||||||
($hmac(sha256, hmacKey, data))[0 .. 12]
|
($hmac(sha256, hmacKey, data))[0 .. 12]
|
||||||
|
|
||||||
proc getVidUrl*(link: string): string =
|
proc getVidUrl*(link: string): string =
|
||||||
|
if link.len == 0: return
|
||||||
let
|
let
|
||||||
sig = getHmac(link)
|
sig = getHmac(link)
|
||||||
url = encodeUrl(link)
|
url = encodeUrl(link)
|
||||||
|
|
Loading…
Reference in New Issue