Move `downloadFileEx` out of `downloadFile` (solution, proposed by @Yardanico). Tested manually.
This commit is contained in:
parent
dfe6797023
commit
fdf4f74cd1
1 changed files with 22 additions and 22 deletions
|
|
@ -1221,30 +1221,30 @@ proc downloadFile*(client: HttpClient, url: Uri | string, filename: string) =
|
||||||
if resp.code.is4xx or resp.code.is5xx:
|
if resp.code.is4xx or resp.code.is5xx:
|
||||||
raise newException(HttpRequestError, resp.status)
|
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,
|
proc downloadFile*(client: AsyncHttpClient, url: Uri | string,
|
||||||
filename: string): Future[void] =
|
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")
|
result = newFuture[void]("downloadFile")
|
||||||
try:
|
try:
|
||||||
result = downloadFileEx(client, url, filename)
|
result = downloadFileEx(client, url, filename)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue