Skip to content

Commit

Permalink
Easy way to convert objects to JSON (#13)
Browse files Browse the repository at this point in the history
* toJson helper implementation

* deploy docs to GHA on tag creation

* add docstring for toJson

* toJson -> toJsonString

* update readme docs
  • Loading branch information
mishankov authored Mar 20, 2024
1 parent 4494eed commit b1bb92d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
39 changes: 39 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: CI
on:
push:
branches: [ main ]
tags: ["v*.*.*"]
pull_request:
branches: [ main ]

Expand Down Expand Up @@ -33,3 +34,41 @@ jobs:
# go-httpbin container does not run on windows runner for some reason. macos runner does not has docker by default
if: ${{ matrix.os == 'ubuntu-latest' }}
run: nimble inttests

deploy-docs:
needs:
- build

environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

if: github.event_name == 'release' && github.event.action == 'created'

runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

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

- name: Generate docs
run: nimble docs

- name: Setup Pages
uses: actions/configure-pages@v4

- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: './src/htmldocs'

- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ nimble install yahttp
> more examples [here](examples/examples.nim)
## Get HTTP status code

```nim
import yahttp
echo get("https://www.google.com/").status
```
## Send query params and parse response to JSON

```nim
import json
import yahttp
Expand Down Expand Up @@ -64,6 +66,7 @@ Has the same arguments as method procedures and one additional:
- `httpMethod` - HTTP method. `Method.GET` by default. Example: `Method.POST`

## Response object

All procedures above return `Response` object with fields:
- `status` - HTTP status code
- `body` - response body as a string
Expand All @@ -79,3 +82,8 @@ All procedures above return `Response` object with fields:
- `Response.html()` - returns response body as HTML
- `Response.to(t)` - converts response body to JSON and unmarshals it to type `t`
- `Response.ok()` - returns `true` if `status` is greater than 0 and less than 400
- `Response.raiseForStatus()` - throws `HttpError` exceptions if status is 400 or above

## Other helper functions

`object.toJsonString()` - converts object of any type to json string. Helpful to use for `body` argument
5 changes: 5 additions & 0 deletions src/yahttp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ proc raiseForStatus*(response: Response) {.raises: [HttpError].} =
$response.status)


proc toJsonString*(obj: object): string =
## Converts object of any type to json. Helpful to use for `body` argument
return $ %*obj


const defaultEncodeQueryParams = EncodeQueryParams(usePlus: false, omitEq: true, sep: '&')


Expand Down
10 changes: 10 additions & 0 deletions tests/int/test_yahttp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ test "Test JSON body":

check jsonResp["json"]["key"].getStr() == "value"

test "Test body with toJson helper":
type TestReq = object
field1: string
field2: int

let jsonResp = put(BASE_URL & "/put", headers = {"Content-Type": "application/json"}, body = TestReq(field1: "value1", field2: 123).toJsonString()).json()

check jsonResp["json"]["field1"].getStr() == "value1"
check jsonResp["json"]["field2"].getInt() == 123

test "Test timeout":
expect TimeoutError:
discard get(BASE_URL & "/delay/5", timeout = 100)
Expand Down
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.10.0"
version = "0.11.0"
author = "Denis Mishankov"
description = "Awesome simple HTTP client"
license = "MIT"
Expand Down

0 comments on commit b1bb92d

Please sign in to comment.