diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2018-04-23 22:37:41 +0300 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2018-04-23 22:37:41 +0300 |
| commit | 3d786878ac839e558bde0a096f74638f38adec86 (patch) | |
| tree | 879060bd9d5bea4374ff562c02a7031dc5d058e7 | |
| parent | 70fc125042676e86dc33f5365b44fbbbcb9fe6df (diff) | |
| download | nimtwirp-3d786878ac839e558bde0a096f74638f38adec86.tar.gz nimtwirp-3d786878ac839e558bde0a096f74638f38adec86.zip | |
Refactor + fix content-length issue
This way need to generate a bit less code.
Also include the Content-Length header always in POST requests made by the
client. This fixes an issue when we are serializing a message with no fields and
we have empty body. HttpClient does not include the Content-Length header for
us, so we'll just include it every time to make our requests work in all cases.
| -rw-r--r-- | nimtwirp/generator.nim | 15 | ||||
| -rw-r--r-- | nimtwirp/nimtwirp.nim | 17 |
2 files changed, 20 insertions, 12 deletions
diff --git a/nimtwirp/generator.nim b/nimtwirp/generator.nim index 5ea9137..167ad17 100644 --- a/nimtwirp/generator.nim +++ b/nimtwirp/generator.nim @@ -88,9 +88,7 @@ proc genClient(service: Service, prefix: string): string = type - {service.name}Client* = ref object - client*: HttpClient - address*: string + {service.name}Client* = ref object of nimtwirp.Client proc new{service.name}Client*(address: string): {service.name}Client = new(result) @@ -104,15 +102,8 @@ proc new{service.name}Client*(address: string): {service.name}Client = result &= &""" proc {meth.name}*(client: {service.name}Client, req: {meth.inputType}): {meth.outputType} = let body = serialize(req) - let resp = client.client.request(client.address & {service.name}Prefix & "{meth.name}", httpMethod=HttpPost, body=body) - let httpStatus = code(resp) - if httpStatus != Http200: - if contentType(resp) != "application/json": - raise newTwirpError(TwirpInternal, "Invalid Content-Type in response") - let errorInfo = parseJson(resp.body) - raise twirpErrorFromJson(errorInfo) - else: - result = new{meth.outputType}(resp.body) + let resp = request(client, {service.name}Prefix, "{meth.name}", body) + result = new{meth.outputType}(resp.body) """ diff --git a/nimtwirp/nimtwirp.nim b/nimtwirp/nimtwirp.nim index 32921bc..7c30980 100644 --- a/nimtwirp/nimtwirp.nim +++ b/nimtwirp/nimtwirp.nim @@ -1,5 +1,6 @@ import asyncdispatch import asynchttpserver +import httpclient import json import macros import strformat @@ -18,6 +19,10 @@ type ServeHandlerProc = proc (request: asynchttpserver.Request): Future[nimtwirp.Response] {.gcsafe, closure.} + Client* = ref object of RootObj + client*: HttpClient + address*: string + proc respond*(req: asynchttpserver.Request, resp: nimtwirp.Response): Future[void] = req.respond(resp.code, resp.body, resp.headers) @@ -99,3 +104,15 @@ proc handler(request: asynchttpserver.Request): Future[nimtwirp.Response] {{.asy result = newStmtList() add(result, handlerProc) add(result, parseStmt(&"nimtwirp.serve(handler, {settings.symbol})")) + +proc request*(client: Client, prefix: string, meth: string, body: string): httpclient.Response = + let address = client.address & prefix & meth + let headers = newHttpHeaders({"Content-Length": $len(body)}) + result = request(client.client, address, httpMethod=HttpPost, body=body, + headers=headers) + let httpStatus = code(result) + if httpStatus != Http200: + if contentType(result) != "application/json": + raise newTwirpError(TwirpInternal, "Invalid Content-Type in response") + let errorInfo = parseJson(result.body) + raise twirpErrorFromJson(errorInfo) |
