The Downloader object holds all the downloads data, initDownloader takes the root directory for all the downloads (or "" if none) and the poll timeout (by default 1 ms). After initializing it, downloading something it's just as easy as calling download, procedure that takes the url, path and an optional name, if not given the path will be the name. Setting a name is useful to identify a download and makes changing the path a lot easier. You can also make a GET request using the request procedure, passing the url and optionally a name. After making a download/request you can use the downloading, downloaded, finished and failed procedures to check wheter a download/request finished, is still in progress or failed.
# Documentation downloader var downloader = initDownloader("./docs") downloader.download("https://nim-lang.org/docs/os.html", "os.html", "os") downloader.request("https://nim-lang.org/docs/strformat.html", "strformat") while true: downloader.update() # Poll events and check if downloads are complete if downloader.succeed("os") and downloader.succeed("strformat"): echo readFile(downloader.getPath("os").get()) echo downloader.getBody("strformat").get() break elif downloader.getError("os").isSome: # You can also do downloader.getState("os").get() == DownloadError raise downloader.getError("os").get()
Types
Download = object url*, path*: string state*: DownloadState error*: ref Exception downFuture*: Future[void] requestFuture*: Future[AsyncResponse]
- Source Edit
Downloader = object dir*: string ## Root directory for all downloads timeout*: int ## Poll events timeout downTable*: Table[string, Download]
- Source Edit
DownloadState = enum Downloading, Downloaded, DownloadError
- Source Edit
Procs
proc download(self: var Downloader; url, path: string; name = ""; replace = false; onProgressChanged: ProgressChangedProc[Future[void]] = nil) {. ...raises: [KeyError, OSError, IOError, Exception], tags: [ReadDirEffect, WriteDirEffect, RootEffect, ReadEnvEffect, TimeEffect, ReadIOEffect], forbids: [].}
- Starts an asynchronous download of url to path. path will be used as the name if name is empty. If replace is set to true and the file is downloaded overwrite it, otherwise if the file is downloaded and replace is false do nothing. Source Edit
proc downloadAgain(self: var Downloader; name: string) {. ...raises: [KeyError, OSError, IOError, Exception], tags: [ReadDirEffect, WriteDirEffect, RootEffect, ReadEnvEffect, TimeEffect, ReadIOEffect], forbids: [].}
- Downloads name again if it exists, does nothing otherwise. Source Edit
proc downloadImpl(url, path: string; proxy: Proxy; onProgressChanged: ProgressChangedProc[Future[void]] = nil): Future[ void] {....stackTrace: false, raises: [Exception], tags: [RootEffect, ReadDirEffect, ReadEnvEffect, TimeEffect, ReadIOEffect], forbids: [].}
- Source Edit
proc downTable(self: Downloader): Table[string, Download] {....raises: [], tags: [], forbids: [].}
- Source Edit
proc exists(self: Downloader; name: string): bool {....raises: [], tags: [], forbids: [].}
- Returns true if a download with name exists. Source Edit
proc failed(self: Downloader; name: string): bool {....raises: [KeyError], tags: [], forbids: [].}
- Returns true if name had a DownloadError. Source Edit
proc finished(self: Downloader; name: string): bool {....raises: [KeyError], tags: [], forbids: [].}
- Returns true if name succeed or failed. Source Edit
proc getBody(self: Downloader; name: string): Option[string] {. ...raises: [KeyError, ValueError, Exception], tags: [RootEffect], forbids: [].}
- Returns the body of name if it finished. Returns none for downloads. Source Edit
proc getError(self: Downloader; name: string): Option[ref Exception] {. ...raises: [KeyError], tags: [], forbids: [].}
- Returns the exception of name if it had a DownloadError. Source Edit
proc getErrorMsg(self: Downloader; name: string): Option[string] {. ...raises: [KeyError], tags: [], forbids: [].}
- Returns the error message of name if it had a DownloadError. Source Edit
proc getPath(self: Downloader; name: string; joinDir = true): Option[string] {. ...raises: [KeyError], tags: [], forbids: [].}
- Returns the path of name if it exists, joined with the downloader's root dir if joinDir is true otherwise returns the raw path. Returns none for requests. Source Edit
proc getResponse(self: Downloader; name: string): Option[AsyncResponse] {. ...raises: [KeyError, ValueError, Exception], tags: [], forbids: [].}
- Returns the AsyncRespones of name if it finished. Returns none for downloads. Source Edit
proc getState(self: Downloader; name: string): Option[DownloadState] {. ...raises: [KeyError], tags: [], forbids: [].}
- Returns the state of name if it exists. Source Edit
proc getURL(self: Downloader; name: string): Option[string] {. ...raises: [KeyError], tags: [], forbids: [].}
- Returns the url of name if it exists. Source Edit
proc initDownloader(dir: string; timeout = 1; proxy, proxyUser, proxyPassword = ""): Downloader {. ...raises: [OSError, IOError], tags: [WriteDirEffect, ReadDirEffect], forbids: [].}
- Initializes a Downloader object and creates dir. Source Edit
proc isDownload(self: Downloader; name: string): bool {....raises: [KeyError], tags: [], forbids: [].}
- Source Edit
proc isRequest(self: Downloader; name: string): bool {....raises: [KeyError], tags: [], forbids: [].}
- Source Edit
proc remove(self: var Downloader; name: string) {....raises: [KeyError, OSError], tags: [ReadDirEffect, WriteDirEffect], forbids: [].}
- Removes name's file if it exists. Source Edit
proc removeProxy(self: var Downloader) {....raises: [], tags: [], forbids: [].}
- Source Edit
proc request(self: var Downloader; url: string; name = "") {. ...raises: [Exception, ValueError], tags: [RootEffect, ReadDirEffect, ReadEnvEffect, TimeEffect, ReadIOEffect], forbids: [].}
- Starts an asynchronous GET request of url. url will be used as the name if name is empty. Source Edit
proc requestAgain(self: var Downloader; name: string) {. ...raises: [KeyError, Exception, ValueError], tags: [RootEffect, ReadDirEffect, ReadEnvEffect, TimeEffect, ReadIOEffect], forbids: [].}
- Requests name again if it exists, does nothing otherwise. Source Edit
proc requestImpl(url: string; proxy: Proxy): Future[AsyncResponse] {. ...stackTrace: false, raises: [Exception, ValueError], tags: [RootEffect, ReadDirEffect, ReadEnvEffect, TimeEffect, ReadIOEffect], forbids: [].}
- Source Edit
proc running(self: Downloader; name: string): bool {....raises: [KeyError], tags: [], forbids: [].}
- Returns true if name is being downloaded/requested. Source Edit
proc setProxy(self: var Downloader; proxy: string; proxyUser, proxyPassword = "") {. ...raises: [], tags: [], forbids: [].}
- Source Edit
proc succeed(self: Downloader; name: string): bool {....raises: [KeyError], tags: [ReadDirEffect], forbids: [].}
- Returns true if name was downloaded/requested successfully, the path must exist if it is a download. Source Edit
proc update(self: var Downloader) {....raises: [ValueError, Exception, OSError, KeyError], tags: [TimeEffect, RootEffect, ReadDirEffect, WriteDirEffect], forbids: [].}
- Poll for any outstanding events and check if any download/request is complete. Source Edit