Skip to content

Commit

Permalink
Custom SSL context (#26)
Browse files Browse the repository at this point in the history
* Add sslContext function arguments

* Update nim version im CI

* Update package version

* Update README.md
  • Loading branch information
mishankov authored Aug 17, 2024
1 parent bac2882 commit 433fe7d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest, macOS-latest, windows-latest ]
nim-version: [ "2.0.0", "2.0.2", "2.0.4", "2.0.6" ]
nim-version: [ "2.0.0", "2.0.2", "2.0.4", "2.0.6", "2.0.8" ]

steps:
- name: Checkout repository
Expand Down Expand Up @@ -61,7 +61,7 @@ jobs:

- uses: jiro4989/setup-nim-action@v2
with:
nim-version: "2.0.6"
nim-version: "2.0.8"
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Generate docs
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Arguments:
- `auth` - login and password for basic authorization. Example: `("login", "password")`
- `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`
- `sslContext` - SSL context for TLS/SSL connections. See [newContext](https://nim-lang.org/docs/net.html#newContext%2Cstring%2Cstring%2Cstring%2Cstring)

## General procedure

Expand Down
6 changes: 4 additions & 2 deletions src/yahttp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ proc request*(url: string, httpMethod: Method = Method.GET, headers: openArray[
RequestHeader] = [], query: openArray[QueryParam] = [],
encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams,
body: string = "",
auth: BasicAuth = ("", ""), timeout = -1, ignoreSsl = false): Response =
auth: BasicAuth = ("", ""), timeout = -1, ignoreSsl = false, sslContext: SslContext = nil): Response =
## Genreal proc to make HTTP request with every HTTP method

# Prepare client

let client: HttpClient = if ignoreSsl:
let client: HttpClient = if sslContext != nil:
newHttpClient(timeout = timeout, sslContext = sslContext)
elif ignoreSsl:
newHttpClient(timeout = timeout, sslContext = newContext(
verifyMode = CVerifyNone))
else:
Expand Down
10 changes: 6 additions & 4 deletions src/yahttp/internal/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ macro http_method_gen*(name: untyped): untyped =
quote do:
proc `name`*(url: string, headers: openArray[RequestHeader] = [], query: openArray[
QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, body: string = "", auth: BasicAuth = ("", ""), timeout = -1,
ignoreSsl = false): Response =
ignoreSsl = false, sslContext: SslContext = nil): Response =
`comment`
return request(
url = url,
Expand All @@ -17,7 +17,8 @@ macro http_method_gen*(name: untyped): untyped =
body = body,
auth = auth,
timeout = timeout,
ignoreSsl = ignoreSsl
ignoreSsl = ignoreSsl,
sslContext = sslContext
)


Expand All @@ -27,7 +28,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 = ("", ""), timeout = -1, ignoreSsl = false): Response =
QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, auth: BasicAuth = ("", ""), timeout = -1, ignoreSsl = false, sslContext: SslContext = nil): Response =
`comment`
return request(
url = url,
Expand All @@ -36,5 +37,6 @@ macro http_method_no_body_gen*(name: untyped): untyped =
query = query,
auth = auth,
timeout = timeout,
ignoreSsl = ignoreSsl
ignoreSsl = ignoreSsl,
sslContext = sslContext
)
2 changes: 1 addition & 1 deletion yahttp.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.11.0"
version = "0.12.0"
author = "Denis Mishankov"
description = "Awesome simple HTTP client"
license = "MIT"
Expand Down

0 comments on commit 433fe7d

Please sign in to comment.