diff options
author | Roman Inflianskas <rominf@users.noreply.github.com> | 2021-01-21 23:29:24 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-22 00:29:24 +0100 |
commit | fdf4f74cd111e64fa8416b8dc6cffd73cb5ab44b (patch) | |
tree | 6802d5bbe320ec414e1b8da438649172f94c08e4 /lib | |
parent | dfe67970235d2c270a4fe91a7ba4451102d8eaac (diff) | |
download | Nim-fdf4f74cd111e64fa8416b8dc6cffd73cb5ab44b.tar.gz |
Fix #16741 (#16783)
Move `downloadFileEx` out of `downloadFile` (solution, proposed by @Yardanico). Tested manually.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/httpclient.nim | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index ba08b63f2..bcd10803e 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -1221,30 +1221,30 @@ proc downloadFile*(client: HttpClient, url: Uri | string, filename: string) = if resp.code.is4xx or resp.code.is5xx: raise newException(HttpRequestError, resp.status) +proc downloadFileEx(client: AsyncHttpClient, + url: Uri | string, filename: string): Future[void] {.async.} = + ## Downloads ``url`` and saves it to ``filename``. + client.getBody = false + let resp = await client.get(url) + + client.bodyStream = newFutureStream[string]("downloadFile") + var file = openAsync(filename, fmWrite) + defer: file.close() + # Let `parseBody` write response data into client.bodyStream in the + # background. + 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) + + if resp.code.is4xx or resp.code.is5xx: + raise newException(HttpRequestError, resp.status) + proc downloadFile*(client: AsyncHttpClient, url: Uri | string, filename: string): Future[void] = - proc downloadFileEx(client: AsyncHttpClient, - url: Uri | string, filename: string): Future[void] {.async.} = - ## Downloads ``url`` and saves it to ``filename``. - client.getBody = false - let resp = await client.get(url) - - client.bodyStream = newFutureStream[string]("downloadFile") - var file = openAsync(filename, fmWrite) - defer: file.close() - # Let `parseBody` write response data into client.bodyStream in the - # background. - 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) - - if resp.code.is4xx or resp.code.is5xx: - raise newException(HttpRequestError, resp.status) - result = newFuture[void]("downloadFile") try: result = downloadFileEx(client, url, filename) |