mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-31 10:22:15 +00:00
This commit is contained in:
committed by
Andreas Rumpf
parent
090d22c715
commit
ef19634065
@@ -1263,21 +1263,30 @@ proc postContent*(client: HttpClient | AsyncHttpClient, url: string,
|
||||
else:
|
||||
return await resp.bodyStream.readAll()
|
||||
|
||||
proc downloadFile*(client: HttpClient | AsyncHttpClient,
|
||||
url: string, filename: string): Future[void] {.multisync.} =
|
||||
proc downloadFile*(client: HttpClient, url: string, filename: string) =
|
||||
## Downloads ``url`` and saves it to ``filename``.
|
||||
client.getBody = false
|
||||
defer:
|
||||
client.getBody = true
|
||||
let resp = await client.get(url)
|
||||
let resp = client.get(url)
|
||||
|
||||
client.bodyStream = newFileStream(filename, fmWrite)
|
||||
if client.bodyStream.isNil:
|
||||
fileError("Unable to open file")
|
||||
parseBody(client, resp.headers, resp.version)
|
||||
client.bodyStream.close()
|
||||
|
||||
if resp.code.is4xx or resp.code.is5xx:
|
||||
raise newException(HttpRequestError, resp.status)
|
||||
|
||||
proc downloadFile*(client: AsyncHttpClient, url: string,
|
||||
filename: string): Future[void] =
|
||||
proc downloadFileEx(client: AsyncHttpClient,
|
||||
url, filename: string): Future[void] {.async.} =
|
||||
## Downloads ``url`` and saves it to ``filename``.
|
||||
client.getBody = false
|
||||
let resp = await client.get(url)
|
||||
|
||||
when client is HttpClient:
|
||||
client.bodyStream = newFileStream(filename, fmWrite)
|
||||
if client.bodyStream.isNil:
|
||||
fileError("Unable to open file")
|
||||
parseBody(client, resp.headers, resp.version)
|
||||
client.bodyStream.close()
|
||||
else:
|
||||
client.bodyStream = newFutureStream[string]("downloadFile")
|
||||
var file = openAsync(filename, fmWrite)
|
||||
# Let `parseBody` write response data into client.bodyStream in the
|
||||
@@ -1288,5 +1297,15 @@ proc downloadFile*(client: HttpClient | AsyncHttpClient,
|
||||
await file.writeFromStream(client.bodyStream)
|
||||
file.close()
|
||||
|
||||
if resp.code.is4xx or resp.code.is5xx:
|
||||
raise newException(HttpRequestError, resp.status)
|
||||
if resp.code.is4xx or resp.code.is5xx:
|
||||
raise newException(HttpRequestError, resp.status)
|
||||
|
||||
result = newFuture[void]("downloadFile")
|
||||
try:
|
||||
result = downloadFileEx(client, url, filename)
|
||||
except Exception as exc:
|
||||
result.fail(exc)
|
||||
finally:
|
||||
result.addCallback(
|
||||
proc () = client.getBody = true
|
||||
)
|
||||
Reference in New Issue
Block a user