Documentation for async httpclient. Notes for url modules.
This commit is contained in:
parent
b23dce03a1
commit
439569cfa3
3 changed files with 43 additions and 5 deletions
|
|
@ -435,15 +435,25 @@ type
|
||||||
connected: bool
|
connected: bool
|
||||||
currentURL: TURL ## Where we are currently connected.
|
currentURL: TURL ## Where we are currently connected.
|
||||||
headers: PStringTable
|
headers: PStringTable
|
||||||
|
maxRedirects: int
|
||||||
userAgent: string
|
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
|
new result
|
||||||
result.headers = newStringTable(modeCaseInsensitive)
|
result.headers = newStringTable(modeCaseInsensitive)
|
||||||
result.userAgent = defUserAgent
|
result.userAgent = defUserAgent
|
||||||
|
result.maxRedirects = maxRedirects
|
||||||
|
|
||||||
proc close*(client: PAsyncHttpClient) =
|
proc close*(client: PAsyncHttpClient) =
|
||||||
## Closes any connections held by the HttpClient.
|
## Closes any connections held by the HTTP client.
|
||||||
if client.connected:
|
if client.connected:
|
||||||
client.socket.close()
|
client.socket.close()
|
||||||
client.connected = false
|
client.connected = false
|
||||||
|
|
@ -588,6 +598,14 @@ proc newConnection(client: PAsyncHttpClient, url: TURL) {.async.} =
|
||||||
|
|
||||||
proc request*(client: PAsyncHttpClient, url: string, httpMethod = httpGET,
|
proc request*(client: PAsyncHttpClient, url: string, httpMethod = httpGET,
|
||||||
body = ""): PFuture[TResponse] {.async.} =
|
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)
|
let r = parseUrl(url)
|
||||||
await newConnection(client, r)
|
await newConnection(client, r)
|
||||||
|
|
||||||
|
|
@ -602,6 +620,19 @@ proc request*(client: PAsyncHttpClient, url: string, httpMethod = httpGET,
|
||||||
|
|
||||||
result = await parseResponse(client, httpMethod != httpHEAD)
|
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 isMainModule:
|
||||||
when true:
|
when true:
|
||||||
# Async
|
# Async
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,16 @@
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
# Nimrod's Runtime Library
|
# Nimrod's Runtime Library
|
||||||
# (c) Copyright 2010 Dominik Picheta
|
# (c) Copyright 2014 Dominik Picheta
|
||||||
#
|
#
|
||||||
# See the file "copying.txt", included in this
|
# See the file "copying.txt", included in this
|
||||||
# distribution, for details about the copyright.
|
# distribution, for details about the copyright.
|
||||||
#
|
#
|
||||||
|
|
||||||
## Parses & constructs URLs.
|
## Parses & constructs URLs.
|
||||||
|
##
|
||||||
|
## **Note**: This module will be deprecated in the future and merged into a
|
||||||
|
## new ``url`` module.
|
||||||
|
|
||||||
import strutils
|
import strutils
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,15 @@
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
# Nimrod's Runtime Library
|
# Nimrod's Runtime Library
|
||||||
# (c) Copyright 2012 Dominik Picheta
|
# (c) Copyright 2014 Dominik Picheta
|
||||||
#
|
#
|
||||||
# See the file "copying.txt", included in this
|
# See the file "copying.txt", included in this
|
||||||
# distribution, for details about the copyright.
|
# distribution, for details about the copyright.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
## **Note**: This module will be deprecated in the future and merged into a
|
||||||
|
## new ``url`` module.
|
||||||
|
|
||||||
import strutils
|
import strutils
|
||||||
type
|
type
|
||||||
TUrl* = distinct string
|
TUrl* = distinct string
|
||||||
|
|
@ -30,4 +34,4 @@ proc add*(url: var TUrl, a: TUrl) =
|
||||||
url = url / a
|
url = url / a
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
assert($("http://".TUrl / "localhost:5000".TUrl) == "http://localhost:5000")
|
assert($("http://".TUrl / "localhost:5000".TUrl) == "http://localhost:5000")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue