oeffisearch/src/backend/hafas/util.nim
2020-02-07 14:09:42 +01:00

51 lines
1.2 KiB
Nim

import httpclient
import asyncdispatch
import md5
import json
import strutils
proc slug*(s: string): string =
for c in s:
if c.isAlphaNumeric():
result &= c.toLowerAscii()
else:
result &= '-'
proc request*(req: JsonNode): Future[JsonNode] {.async.} =
let client = newAsyncHttpClient()
let body = %*{
"lang": "de",
"svcReqL": [req]
}
# TODO: move to profile
body["svcReqL"][0]["cfg"]["rtMode"] = %* "HYBRID"
body["client"] = %* {
"id": "DB",
"v": "16040000",
"type": "IPH",
"name": "DB Navigator"
}
body["ext"] = %* "DB.R19.04.a"
body["ver"] = %* "1.16"
body["auth"] = %* {
"type": "AID",
"aid": "n91dB8Z77MLdoR0K"
}
let salt = "bdI8UVj40K5fvxwf"
let bodytext = $body
let checksum = $toMD5(bodytext & salt)
let url = "https://reiseauskunft.bahn.de/bin/mgate.exe?checksum=" & checksum
client.headers = newHttpHeaders({
"Content-Type": "application/json",
"Accept": "application/json",
"user-agent": "my-awesome-e5f276d8fe6cprogram",
})
#echo pretty body
let req = await client.request(url, httpMethod = HttpPost, body = $body)
let resp = await req.body
return parseJson(resp){"svcResL"}{0}