From 3256cc25d44e50e3289fba1b371e6621c3130cde Mon Sep 17 00:00:00 2001 From: Denis Mishankov Date: Sun, 3 Dec 2023 13:15:13 +0300 Subject: [PATCH 1/2] timeout param --- README.md | 1 + src/yahttp.nim | 6 +++--- src/yahttp/internal/utils.nim | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 989282c..fe48e0a 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ Arguments: - `encodeQueryParams` - parameters for `encodeQuery` function that encodes query params. [More](https://nim-lang.org/docs/uri.html#encodeQuery%2CopenArray%5B%5D%2Cchar) - `body` - request body as a string. Example: `"{\"key\": \"value\"}\"`. Is not available for `get`, `head` and `options` procedures - `auth` - login and password for basic authorization. Example: `("login", "password")` +- `timeout` - stop waiting for a response after a given number of milliseconds - `ignoreSsl` - no certificate verification if `true` ## General procedure diff --git a/src/yahttp.nim b/src/yahttp.nim index 555b7e4..1e6ebb4 100644 --- a/src/yahttp.nim +++ b/src/yahttp.nim @@ -68,15 +68,15 @@ proc request*(url: string, httpMethod: Method = Method.GET, headers: openArray[ RequestHeader] = [], query: openArray[QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, body: string = "", - auth: BasicAuth = ("", ""), ignoreSsl = false): Response = + auth: BasicAuth = ("", ""), timeout = -1, ignoreSsl = false): Response = ## Genreal proc to make HTTP request with every HTTP method # Prepare client let client: HttpClient = if ignoreSsl: - newHttpClient(sslContext = newContext(verifyMode = CVerifyNone)) + newHttpClient(timeout = timeout, sslContext = newContext(verifyMode = CVerifyNone)) else: - newHttpClient() + newHttpClient(timeout = timeout) # Prepare headers diff --git a/src/yahttp/internal/utils.nim b/src/yahttp/internal/utils.nim index 7443400..a21ab40 100644 --- a/src/yahttp/internal/utils.nim +++ b/src/yahttp/internal/utils.nim @@ -6,7 +6,7 @@ macro http_method_gen*(name: untyped): untyped = let comment = newCommentStmtNode(fmt"Proc for {methodUpper} HTTP method") quote do: proc `name`*(url: string, headers: openArray[RequestHeader] = [], query: openArray[ - QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, body: string = "", auth: BasicAuth = ("", ""), + QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, body: string = "", auth: BasicAuth = ("", ""), timeout = -1, ignoreSsl = false): Response = `comment` return request( @@ -26,7 +26,7 @@ macro http_method_no_body_gen*(name: untyped): untyped = let comment = newCommentStmtNode(fmt"Proc for {methodUpper} HTTP method") quote do: proc `name`*(url: string, headers: openArray[RequestHeader] = [], query: openArray[ - QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, auth: BasicAuth = ("", ""), ignoreSsl = false): Response = + QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, auth: BasicAuth = ("", ""), timeout = -1, ignoreSsl = false): Response = `comment` return request( url = url, From 88ec5e8824e93b9dae3c57d0184489a671cd7b6d Mon Sep 17 00:00:00 2001 From: Denis Mishankov Date: Sun, 3 Dec 2023 13:16:34 +0300 Subject: [PATCH 2/2] docs and version --- README.md | 2 +- yahttp.nimble | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fe48e0a..1e9487c 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Arguments: - `encodeQueryParams` - parameters for `encodeQuery` function that encodes query params. [More](https://nim-lang.org/docs/uri.html#encodeQuery%2CopenArray%5B%5D%2Cchar) - `body` - request body as a string. Example: `"{\"key\": \"value\"}\"`. Is not available for `get`, `head` and `options` procedures - `auth` - login and password for basic authorization. Example: `("login", "password")` -- `timeout` - stop waiting for a response after a given number of milliseconds +- `timeout` - stop waiting for a response after a given number of milliseconds. `-1` for no timeout, which is default value - `ignoreSsl` - no certificate verification if `true` ## General procedure diff --git a/yahttp.nimble b/yahttp.nimble index 5bdbe89..ae0e2b7 100644 --- a/yahttp.nimble +++ b/yahttp.nimble @@ -1,6 +1,6 @@ # Package -version = "0.5.1" +version = "0.6.0" author = "Denis Mishankov" description = "Awesome simple HTTP client" license = "MIT"