mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-21 23:05:27 +00:00
Documentation for async httpclient. Notes for url modules.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
#
|
||||
#
|
||||
# Nimrod's Runtime Library
|
||||
# (c) Copyright 2010 Dominik Picheta
|
||||
# (c) Copyright 2014 Dominik Picheta
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## Parses & constructs URLs.
|
||||
##
|
||||
## **Note**: This module will be deprecated in the future and merged into a
|
||||
## new ``url`` module.
|
||||
|
||||
import strutils
|
||||
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
#
|
||||
#
|
||||
# Nimrod's Runtime Library
|
||||
# (c) Copyright 2012 Dominik Picheta
|
||||
# (c) Copyright 2014 Dominik Picheta
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## **Note**: This module will be deprecated in the future and merged into a
|
||||
## new ``url`` module.
|
||||
|
||||
import strutils
|
||||
type
|
||||
TUrl* = distinct string
|
||||
@@ -30,4 +34,4 @@ proc add*(url: var TUrl, a: TUrl) =
|
||||
url = url / a
|
||||
|
||||
when isMainModule:
|
||||
assert($("http://".TUrl / "localhost:5000".TUrl) == "http://localhost:5000")
|
||||
assert($("http://".TUrl / "localhost:5000".TUrl) == "http://localhost:5000")
|
||||
|
||||
Reference in New Issue
Block a user