2022-11-10 09:29:34 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2023-04-04 13:34:53 +00:00
|
|
|
|
2022-11-10 09:29:34 +00:00
|
|
|
import
|
2022-11-16 15:38:31 +00:00
|
|
|
std/json,
|
|
|
|
stew/results,
|
|
|
|
stew/shims/net,
|
2022-11-10 09:29:34 +00:00
|
|
|
chronicles,
|
|
|
|
chronicles/topics_registry,
|
|
|
|
chronos,
|
2022-11-16 15:38:31 +00:00
|
|
|
presto/[client,common]
|
2022-11-10 09:29:34 +00:00
|
|
|
|
|
|
|
type
|
2022-11-16 15:38:31 +00:00
|
|
|
NodeLocation* = object
|
2022-11-10 09:29:34 +00:00
|
|
|
country*: string
|
|
|
|
city*: string
|
|
|
|
lat*: string
|
|
|
|
long*: string
|
|
|
|
isp*: string
|
|
|
|
|
|
|
|
proc flatten*[T](a: seq[seq[T]]): seq[T] =
|
|
|
|
var aFlat = newSeq[T](0)
|
|
|
|
for subseq in a:
|
|
|
|
aFlat &= subseq
|
|
|
|
return aFlat
|
|
|
|
|
2022-11-16 15:38:31 +00:00
|
|
|
proc decodeBytes*(t: typedesc[NodeLocation], value: openArray[byte],
|
|
|
|
contentType: Opt[ContentTypeData]): RestResult[NodeLocation] =
|
|
|
|
var res: string
|
|
|
|
if len(value) > 0:
|
|
|
|
res = newString(len(value))
|
|
|
|
copyMem(addr res[0], unsafeAddr value[0], len(value))
|
|
|
|
try:
|
|
|
|
let jsonContent = parseJson(res)
|
|
|
|
if $jsonContent["status"].getStr() != "success":
|
|
|
|
error "query failed", result=jsonContent
|
|
|
|
return err("query failed")
|
|
|
|
return ok(NodeLocation(
|
|
|
|
country: jsonContent["country"].getStr(),
|
|
|
|
city: jsonContent["city"].getStr(),
|
|
|
|
lat: $jsonContent["lat"].getFloat(),
|
|
|
|
long: $jsonContent["lon"].getFloat(),
|
|
|
|
isp: jsonContent["isp"].getStr()
|
|
|
|
))
|
2023-04-05 12:27:11 +00:00
|
|
|
except Exception:
|
2022-11-16 15:38:31 +00:00
|
|
|
return err("failed to get the location: " & getCurrentExceptionMsg())
|
2022-11-10 09:29:34 +00:00
|
|
|
|
2022-11-16 15:38:31 +00:00
|
|
|
proc encodeString*(value: string): RestResult[string] =
|
|
|
|
ok(value)
|
2022-11-10 09:29:34 +00:00
|
|
|
|
2022-11-16 15:38:31 +00:00
|
|
|
proc ipToLocation*(ip: string): RestResponse[NodeLocation] {.rest, endpoint: "json/{ip}", meth: MethodGet.}
|