parent
7f67c593c1
commit
4ae520711d
3 changed files with 50 additions and 4 deletions
|
|
@ -841,8 +841,11 @@ proc parseResponse(client: HttpClient | AsyncHttpClient,
|
|||
client.bodyStream = newFutureStream[string]("parseResponse")
|
||||
result.bodyStream = client.bodyStream
|
||||
assert(client.parseBodyFut.isNil or client.parseBodyFut.finished)
|
||||
# do not wait here for the body request to complete
|
||||
client.parseBodyFut = parseBody(client, result.headers, result.version)
|
||||
# do not wait here for the body request to complete
|
||||
client.parseBodyFut.addCallback do():
|
||||
if client.parseBodyFut.failed:
|
||||
client.bodyStream.fail(client.parseBodyFut.error)
|
||||
|
||||
proc newConnection(client: HttpClient | AsyncHttpClient,
|
||||
url: Uri) {.multisync.} =
|
||||
|
|
@ -1228,13 +1231,16 @@ proc downloadFile*(client: AsyncHttpClient, url: Uri | string,
|
|||
|
||||
client.bodyStream = newFutureStream[string]("downloadFile")
|
||||
var file = openAsync(filename, fmWrite)
|
||||
defer: file.close()
|
||||
# Let `parseBody` write response data into client.bodyStream in the
|
||||
# background.
|
||||
asyncCheck parseBody(client, resp.headers, resp.version)
|
||||
let parseBodyFut = parseBody(client, resp.headers, resp.version)
|
||||
parseBodyFut.addCallback do():
|
||||
if parseBodyFut.failed:
|
||||
client.bodyStream.fail(parseBodyFut.error)
|
||||
# The `writeFromStream` proc will complete once all the data in the
|
||||
# `bodyStream` has been written to the file.
|
||||
await file.writeFromStream(client.bodyStream)
|
||||
file.close()
|
||||
|
||||
if resp.code.is4xx or resp.code.is5xx:
|
||||
raise newException(HttpRequestError, resp.status)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue