diff --git a/src/apiutils.nim b/src/apiutils.nim index a411e4e..db8015f 100644 --- a/src/apiutils.nim +++ b/src/apiutils.nim @@ -48,7 +48,7 @@ proc fetch*(url: Uri; retried=false; oldApi=false): Future[JsonNode] {.async.} = result = parseJson(body) - if result{"errors"} != nil and result{"errors"}[0]{"code"}.getInt == 200: + if result{"errors"} != nil and result.getError == forbidden: keepToken = false echo "bad token" except: diff --git a/src/parser.nim b/src/parser.nim index 58abc55..f6726a2 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -1,10 +1,6 @@ import json, strutils, options, tables, times, math 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 = if js == nil: return result = Profile( @@ -31,7 +27,7 @@ proc parseUserShow*(js: JsonNode; username: string): Profile = if js == nil: return with error, js{"errors"}: result = Profile(username: username) - if parseError(error) == suspended: + if error.getError == suspended: result.suspended = true return @@ -41,7 +37,7 @@ proc parseGraphProfile*(js: JsonNode; username: string): Profile = if js == nil: return with error, js{"errors"}: result = Profile(username: username) - if parseError(error) == suspended: + if error.getError == suspended: result.suspended = true return diff --git a/src/parserutils.nim b/src/parserutils.nim index 24b1c6e..0ee78fa 100644 --- a/src/parserutils.nim +++ b/src/parserutils.nim @@ -30,6 +30,10 @@ template `with`*(ident; value: JsonNode; body): untyped = template getCursor*(js: JsonNode): string = 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 = if time.len != flen: return parseTime(time, f, localTimezone).utc.toTime diff --git a/src/utils.nim b/src/utils.nim index ff7d901..b2bdb97 100644 --- a/src/utils.nim +++ b/src/utils.nim @@ -22,6 +22,7 @@ proc getHmac*(data: string): string = ($hmac(sha256, hmacKey, data))[0 .. 12] proc getVidUrl*(link: string): string = + if link.len == 0: return let sig = getHmac(link) url = encodeUrl(link)