diff options
author | Dominik Picheta <dominikpicheta@googlemail.com> | 2014-04-06 19:27:16 +0100 |
---|---|---|
committer | Dominik Picheta <dominikpicheta@googlemail.com> | 2014-04-06 19:27:16 +0100 |
commit | 439569cfa3bce06c5a9ab77c88572a546e807fdd (patch) | |
tree | 2b9a4949c627fbb32b0a717b085abcb61ef18b49 /lib/pure/httpclient.nim | |
parent | b23dce03a13a5933319283750ad229bb88ada869 (diff) | |
download | Nim-439569cfa3bce06c5a9ab77c88572a546e807fdd.tar.gz |
Documentation for async httpclient. Notes for url modules.
Diffstat (limited to 'lib/pure/httpclient.nim')
-rw-r--r-- | lib/pure/httpclient.nim | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index 415775583..1e3a1032c 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -435,15 +435,25 @@ type connected: bool currentURL: TURL ## Where we are currently connected. headers: PStringTable + maxRedirects: int userAgent: string -proc newAsyncHttpClient*(): PAsyncHttpClient = +proc newAsyncHttpClient*(userAgent = defUserAgent, + maxRedirects = 5): PAsyncHttpClient = + ## Creates a new PAsyncHttpClient instance. + ## + ## ``userAgent`` specifies the user agent that will be used when making + ## requests. + ## + ## ``maxRedirects`` specifies the maximum amount of redirects to follow, + ## default is 5. new result result.headers = newStringTable(modeCaseInsensitive) result.userAgent = defUserAgent + result.maxRedirects = maxRedirects proc close*(client: PAsyncHttpClient) = - ## Closes any connections held by the HttpClient. + ## Closes any connections held by the HTTP client. if client.connected: client.socket.close() client.connected = false @@ -588,6 +598,14 @@ proc newConnection(client: PAsyncHttpClient, url: TURL) {.async.} = proc request*(client: PAsyncHttpClient, url: string, httpMethod = httpGET, body = ""): PFuture[TResponse] {.async.} = + ## Connects to the hostname specified by the URL and performs a request + ## using the method specified. + ## + ## Connection will kept alive. Further requests on the same ``client`` to + ## the same hostname will not require a new connection to be made. The + ## connection can be closed by using the ``close`` procedure. + ## + ## The returned future will complete once the request is completed. let r = parseUrl(url) await newConnection(client, r) @@ -602,6 +620,19 @@ proc request*(client: PAsyncHttpClient, url: string, httpMethod = httpGET, result = await parseResponse(client, httpMethod != httpHEAD) +proc get*(client: PAsyncHttpClient, url: string): PFuture[TResponse] {.async.} = + ## Connects to the hostname specified by the URL and performs a GET request. + ## + ## This procedure will follow redirects up to a maximum number of redirects + ## specified in ``newAsyncHttpClient``. + result = await client.request(url, httpGET) + var lastURL = url + for i in 1..client.maxRedirects: + if result.status.redirection(): + let redirectTo = getNewLocation(lastURL, result.headers) + result = await client.request(redirectTo, httpGET) + lastUrl = redirectTo + when isMainModule: when true: # Async |