Skip to content

Commit

Permalink
Headers update (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mishankov authored Nov 25, 2023
1 parent be4b492 commit 5644250
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Has the same arguments as method procedures and one additional:
All procedures above return `Response` object with fields:
- `status` - HTTP status code
- `body` - response body as a string
- `headers` - sequence of response headers
- `headers` - table, where keys are header keys and values are sequences of header values for a key

`Response` object has some helper procedures:
- `Response.json()` - returns response body as JSON
Expand Down
6 changes: 2 additions & 4 deletions examples/examples.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ for catTag in catTags[0..4]:
let tag = catTag.getStr()

if tag.len() > 0:
echo "===="
echo "Working with tag " & tag
let catData = get("https://cataas.com/api/cats", query = {"tags": tag, "limit": "10"})

echo "===="
echo "Response status: " & $catData.status
echo "Response headers: "
for header in catData.headers:
echo header
echo "Response headers: ", catData.headers
echo "Response body: " & catData.body
72 changes: 34 additions & 38 deletions src/yahttp.nim
Original file line number Diff line number Diff line change
@@ -1,64 +1,60 @@
import base64, httpclient, net, json, uri, strutils
import base64, httpclient, net, json, uri, strutils, tables

type
BasicAuth* = tuple[login: string, password: string] ## Basic auth type
Header* = tuple[key: string, value: string] ## Type for HTTP header
## Types without methods
QueryParam* = tuple[key: string, value: string] ## Type for URL query params
RequestHeader* = tuple[key: string, value: string] ## Type for HTTP header

EncodeQueryParams* = object
## Parameters for encodeQuery procedure
usePlus*: bool
omitEq*: bool
sep*: char

Method* = enum
## Supported HTTP methods
GET, PUT, POST, PATCH, DELETE, HEAD, OPTIONS


type
BasicAuth* = tuple[login: string, password: string] ## Basic auth type

proc basicAuthHeader(auth: BasicAuth): string =
return "Basic " & encode(auth.login & ":" & auth.password)

type
Response* = object
## Type for HTTP response
status*: int
body*: string
headers*: seq[Header]

EncodeQueryParams* = object
## Paramters for encodeQuery procedure
usePlus*: bool
omitEq*: bool
sep*: char


const defaultEncodeQueryParams = EncodeQueryParams(usePlus: false, omitEq: true, sep: '&')
headers*: TableRef[string, seq[string]]

proc toResp(response: httpclient.Response): Response =
## Convert httpclient.Response to yahttp.Response
return Response(
status: parseInt(response.status.strip()[0..2]),
headers: response.headers.table,
body: response.body
)

proc json*(response: Response): JsonNode =
## Parses response body to json
return parseJson(response.body)


proc to*[T](response: Response, t: typedesc[T]): T =
## Parses response body to json and then casts it to passed type
return to(response.json(), t)


proc ok*(response: Response): bool =
## Is HTTP status in OK range (> 0 and < 400)?
return response.status > 0 and response.status < 400


proc toResp(response: httpclient.Response): Response =
## Convert httpclient.Response to yahttp.Response
var headers: seq[Header] = @[]
for headerKey, headerVal in response.headers:
headers.add((headerKey, headerVal))

return Response(
status: parseInt(response.status.strip()[0..2]),
headers: headers,
body: response.body
)


proc basicAuthHeader(auth: BasicAuth): string =
return "Basic " & encode(auth.login & ":" & auth.password)
const defaultEncodeQueryParams = EncodeQueryParams(usePlus: false, omitEq: true, sep: '&')


proc request*(url: string, httpMethod: Method = Method.GET, headers: openArray[
Header] = [], query: openArray[QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, body: string = "",
RequestHeader] = [], query: openArray[QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, body: string = "",
auth: BasicAuth = ("", ""), ignoreSsl = false): Response =
## Genreal proc to make HTTP request with every HTTP method

Expand Down Expand Up @@ -107,7 +103,7 @@ proc request*(url: string, httpMethod: Method = Method.GET, headers: openArray[

# Deidcated procs for individual methods

proc get*(url: string, headers: openArray[Header] = [], query: openArray[
proc get*(url: string, headers: openArray[RequestHeader] = [], query: openArray[
QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, auth: BasicAuth = ("", ""), ignoreSsl = false): Response =
return request(
url = url,
Expand All @@ -118,7 +114,7 @@ proc get*(url: string, headers: openArray[Header] = [], query: openArray[
ignoreSsl = ignoreSsl
)

proc put*(url: string, headers: openArray[Header] = [], query: openArray[
proc put*(url: string, headers: openArray[RequestHeader] = [], query: openArray[
QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, body: string = "", auth: BasicAuth = ("", ""),
ignoreSsl = false): Response =
return request(
Expand All @@ -131,7 +127,7 @@ proc put*(url: string, headers: openArray[Header] = [], query: openArray[
ignoreSsl = ignoreSsl
)

proc post*(url: string, headers: openArray[Header] = [], query: openArray[
proc post*(url: string, headers: openArray[RequestHeader] = [], query: openArray[
QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, body: string = "", auth: BasicAuth = ("", ""),
ignoreSsl = false): Response =
return request(
Expand All @@ -144,7 +140,7 @@ proc post*(url: string, headers: openArray[Header] = [], query: openArray[
ignoreSsl = ignoreSsl
)

proc patch*(url: string, headers: openArray[Header] = [],
proc patch*(url: string, headers: openArray[RequestHeader] = [],
query: openArray[QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, body: string = "",
auth: BasicAuth = ("", ""), ignoreSsl = false): Response =
return request(
Expand All @@ -158,7 +154,7 @@ proc patch*(url: string, headers: openArray[Header] = [],
)


proc delete*(url: string, headers: openArray[Header] = [],
proc delete*(url: string, headers: openArray[RequestHeader] = [],
query: openArray[QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, body: string = "",
auth: BasicAuth = ("", ""), ignoreSsl = false): Response =
return request(
Expand All @@ -171,7 +167,7 @@ proc delete*(url: string, headers: openArray[Header] = [],
ignoreSsl = ignoreSsl
)

proc head*(url: string, headers: openArray[Header] = [], query: openArray[
proc head*(url: string, headers: openArray[RequestHeader] = [], query: openArray[
QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, auth: BasicAuth = ("", ""), ignoreSsl = false): Response =
return request(
url = url,
Expand All @@ -182,7 +178,7 @@ proc head*(url: string, headers: openArray[Header] = [], query: openArray[
ignoreSsl = ignoreSsl
)

proc options*(url: string, headers: openArray[Header] = [],
proc options*(url: string, headers: openArray[RequestHeader] = [],
query: openArray[QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, auth: BasicAuth = ("", ""),
ignoreSsl = false): Response =
return request(
Expand Down
1 change: 1 addition & 0 deletions tests/test_yahttp.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import tables

include yahttp

Expand Down

0 comments on commit 5644250

Please sign in to comment.