summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@gmail.com>2016-09-24 20:33:00 +0200
committerDominik Picheta <dominikpicheta@gmail.com>2016-09-24 20:33:00 +0200
commit0baef8e6ea9995f3bc75a1f28417e95f9dfe2d15 (patch)
tree2dc9d8e67ef4b001f598379d3c8bdffb7812af25 /lib
parentfa9ec7a6b54ae1fc59674f530a0ee4a7532ca5e7 (diff)
downloadNim-0baef8e6ea9995f3bc75a1f28417e95f9dfe2d15.tar.gz
Implements getContent and postContent for (Async)HttpClient.
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/httpclient.nim37
1 files changed, 36 insertions, 1 deletions
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim
index 5eeb54f32..043bfc423 100644
--- a/lib/pure/httpclient.nim
+++ b/lib/pure/httpclient.nim
@@ -950,10 +950,13 @@ proc request*(client: HttpClient | AsyncHttpClient, url: string,
   ## 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
+  ## Connection will be 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.
   ##
+  ## When a request is made to a different hostname, the current connection will
+  ## be closed.
+  ##
   ## The returned future will complete once the request is completed.
   result = await request(client, url, $httpMethod, body)
 
@@ -973,6 +976,21 @@ proc get*(client: HttpClient | AsyncHttpClient,
       result = await client.request(redirectTo, HttpGET)
       lastURL = redirectTo
 
+proc getContent*(client: HttpClient | AsyncHttpClient,
+                 url: string): Future[string] {.multisync.} =
+  ## 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 ``client.maxRedirects``.
+  ##
+  ## A ``HttpRequestError`` will be raised if the server responds with a
+  ## client error (status code 4xx) or a server error (status code 5xx).
+  let resp = await get(client, url)
+  if resp.code.is4xx or resp.code.is5xx:
+    raise newException(HttpRequestError, resp.status)
+  else:
+    return resp.body
+
 proc post*(client: HttpClient | AsyncHttpClient, url: string, body = "",
            multipart: MultipartData = nil): Future[Response] {.multisync.} =
   ## Connects to the hostname specified by the URL and performs a POST request.
@@ -1000,3 +1018,20 @@ proc post*(client: HttpClient | AsyncHttpClient, url: string, body = "",
       var meth = if result.status != "307": HttpGet else: HttpPost
       result = await client.request(redirectTo, meth, xb)
       lastURL = redirectTo
+
+proc postContent*(client: HttpClient | AsyncHttpClient, url: string,
+                  body = "",
+                  multipart: MultipartData = nil): Future[string]
+                  {.multisync.} =
+  ## Connects to the hostname specified by the URL and performs a POST request.
+  ##
+  ## This procedure will follow redirects up to a maximum number of redirects
+  ## specified in ``client.maxRedirects``.
+  ##
+  ## A ``HttpRequestError`` will be raised if the server responds with a
+  ## client error (status code 4xx) or a server error (status code 5xx).
+  let resp = await post(client, url, body, multipart)
+  if resp.code.is4xx or resp.code.is5xx:
+    raise newException(HttpRequestError, resp.status)
+  else:
+    return resp.body