Support new chronos.Content-Type header parser. (#37)
This commit is contained in:
parent
3984431dc0
commit
545849eebe
|
@ -28,12 +28,12 @@ type
|
|||
|
||||
RestPlainResponse* = object
|
||||
status*: int
|
||||
contentType*: string
|
||||
contentType*: Opt[ContentTypeData]
|
||||
data*: seq[byte]
|
||||
|
||||
RestResponse*[T] = object
|
||||
status*: int
|
||||
contentType*: string
|
||||
contentType*: Opt[ContentTypeData]
|
||||
data*: T
|
||||
|
||||
RestStatus* = distinct int
|
||||
|
@ -70,6 +70,18 @@ proc `<=`*(x, y: RestStatus): bool {.borrow.}
|
|||
proc `<`*(x, y: RestStatus): bool {.borrow.}
|
||||
proc `$`*(x: RestStatus): string {.borrow.}
|
||||
|
||||
proc `$`*(x: Opt[ContentTypeData]): string =
|
||||
if x.isSome():
|
||||
$x.get()
|
||||
else:
|
||||
"<missing>"
|
||||
|
||||
proc `==`*(a: Opt[ContentTypeData], b: MediaType): bool =
|
||||
if a.isNone():
|
||||
false
|
||||
else:
|
||||
a.get() == b
|
||||
|
||||
proc new*(t: typedesc[RestClientRef],
|
||||
url: string,
|
||||
flags: RestClientFlags = {},
|
||||
|
@ -249,7 +261,7 @@ proc raiseRestResponseError*(resp: RestPlainResponse) {.
|
|||
msg.add("]")
|
||||
var error = newException(RestResponseError, msg)
|
||||
error.status = resp.status
|
||||
error.contentType = resp.contentType
|
||||
error.contentType = $resp.contentType
|
||||
error.message = bytesToString(resp.data)
|
||||
raise error
|
||||
|
||||
|
@ -450,7 +462,7 @@ proc requestWithoutBody*(req: HttpClientRequestRef,
|
|||
let res =
|
||||
block:
|
||||
let status = response.status
|
||||
let contentType = response.headers.getString("content-type")
|
||||
let contentType = response.contentType
|
||||
let data =
|
||||
block:
|
||||
var default: seq[byte]
|
||||
|
@ -462,7 +474,7 @@ proc requestWithoutBody*(req: HttpClientRequestRef,
|
|||
debug "Received REST response body from remote server",
|
||||
status = response.status, http_method = $request.meth,
|
||||
address, connection = request.connection,
|
||||
contentType = contentType, size = len(data)
|
||||
contentType = $contentType, size = len(data)
|
||||
await request.closeWait()
|
||||
request = nil
|
||||
await response.closeWait()
|
||||
|
@ -555,7 +567,7 @@ proc requestWithBody*(req: HttpClientRequestRef, pbytes: pointer,
|
|||
let res =
|
||||
block:
|
||||
let status = response.status
|
||||
let contentType = response.headers.getString("content-type")
|
||||
let contentType = response.contentType
|
||||
let data =
|
||||
block:
|
||||
var default: seq[byte]
|
||||
|
@ -565,7 +577,7 @@ proc requestWithBody*(req: HttpClientRequestRef, pbytes: pointer,
|
|||
else:
|
||||
await response.getBodyBytes()
|
||||
debug "Received REST response body from remote server",
|
||||
contentType = contentType, size = len(data),
|
||||
contentType = $contentType, size = len(data),
|
||||
address, connection = request.connection
|
||||
await request.closeWait()
|
||||
request = nil
|
||||
|
|
|
@ -73,11 +73,11 @@ proc decodeString*(t: typedesc[CustomType1],
|
|||
err("Unable to decode value")
|
||||
|
||||
proc decodeBytes*(t: typedesc[CustomType1], value: openArray[byte],
|
||||
contentType: string): RestResult[CustomType1] =
|
||||
contentType: Opt[ContentTypeData]): RestResult[CustomType1] =
|
||||
discard
|
||||
|
||||
proc decodeBytes*(t: typedesc[string], value: openArray[byte],
|
||||
contentType: string): RestResult[string] =
|
||||
contentType: Opt[ContentTypeData]): RestResult[string] =
|
||||
var res: string
|
||||
if len(value) > 0:
|
||||
res = newString(len(value))
|
||||
|
@ -85,7 +85,7 @@ proc decodeBytes*(t: typedesc[string], value: openArray[byte],
|
|||
ok(res)
|
||||
|
||||
proc decodeBytes*(t: typedesc[int], value: openArray[byte],
|
||||
contentType: string): RestResult[int] =
|
||||
contentType: Opt[ContentTypeData]): RestResult[int] =
|
||||
if len(value) == 0:
|
||||
err("Could not find any integer")
|
||||
else:
|
||||
|
|
|
@ -1038,16 +1038,16 @@ suite "REST API client test suite":
|
|||
|
||||
check:
|
||||
res6.status == 410
|
||||
res6.contentType == "text/html"
|
||||
res6.contentType == MediaType.init("text/html")
|
||||
bytesToString(res6.data) == "ERROR-410"
|
||||
res7.status == 411
|
||||
res7.contentType == "text/html"
|
||||
res7.contentType == MediaType.init("text/html")
|
||||
bytesToString(res7.data) == "ERROR-411"
|
||||
res8.status == 200
|
||||
res8.contentType == "text/plain"
|
||||
res8.contentType == MediaType.init("text/plain")
|
||||
bytesToString(res8.data) == "SUCCESS-200"
|
||||
res9.status == 204
|
||||
res9.contentType == "text/integer"
|
||||
res9.contentType == MediaType.init("text/integer")
|
||||
bytesToString(res9.data) == "204"
|
||||
res10.status == 404
|
||||
|
||||
|
@ -1059,16 +1059,16 @@ suite "REST API client test suite":
|
|||
|
||||
check:
|
||||
res11.status == 410
|
||||
res11.contentType == "text/html"
|
||||
res11.contentType == MediaType.init("text/html")
|
||||
res11.data == "ERROR-410"
|
||||
res12.status == 411
|
||||
res12.contentType == "text/html"
|
||||
res12.contentType == MediaType.init("text/html")
|
||||
res12.data == "ERROR-411"
|
||||
res13.status == 200
|
||||
res13.contentType == "text/plain"
|
||||
res13.contentType == MediaType.init("text/plain")
|
||||
res13.data == "SUCCESS-200"
|
||||
res14.status == 204
|
||||
res14.contentType == "text/integer"
|
||||
res14.contentType == MediaType.init("text/integer")
|
||||
res14.data == 204
|
||||
res15.status == 404
|
||||
|
||||
|
|
Loading…
Reference in New Issue