Skip to content

Commit

Permalink
Implement raiseForStatus (#8)
Browse files Browse the repository at this point in the history
* add raiseForStatus

* pretty

* bump version
  • Loading branch information
mishankov authored Dec 19, 2023
1 parent 7039ec4 commit 0739d55
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/yahttp.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import base64, httpclient, net, json, uri, strutils, tables

import yahttp/internal/utils
import yahttp/exceptions

type
## Types without methods
Expand Down Expand Up @@ -39,13 +40,15 @@ type
request*: Request

proc toResp(response: httpclient.Response, requestUrl: string,
requestHeaders: seq[tuple[key: string, val: string]], requestHttpMethod: Method): Response =
requestHeaders: seq[tuple[key: string, val: string]],
requestHttpMethod: Method): Response =
## Converts httpclient.Response to yahttp.Response
return Response(
status: parseInt(response.status.strip()[0..2]),
headers: response.headers.table,
body: response.body,
request: Request(url: requestUrl, headers: requestHeaders, httpMethod: requestHttpMethod)
request: Request(url: requestUrl, headers: requestHeaders,
httpMethod: requestHttpMethod)
)

proc json*(response: Response): JsonNode =
Expand All @@ -60,6 +63,11 @@ proc ok*(response: Response): bool =
## Is HTTP status in OK range (> 0 and < 400)?
return response.status > 0 and response.status < 400

proc raiseForStatus*(response: Response) {.raises: [HttpError].} =
## Throws `HttpError` exceptions if status is 400 or above
if response.status >= 400: raise HttpError.newException("Status is: " &
$response.status)


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

Expand All @@ -74,7 +82,8 @@ proc request*(url: string, httpMethod: Method = Method.GET, headers: openArray[
# Prepare client

let client: HttpClient = if ignoreSsl:
newHttpClient(timeout = timeout, sslContext = newContext(verifyMode = CVerifyNone))
newHttpClient(timeout = timeout, sslContext = newContext(
verifyMode = CVerifyNone))
else:
newHttpClient(timeout = timeout)

Expand Down Expand Up @@ -113,7 +122,8 @@ proc request*(url: string, httpMethod: Method = Method.GET, headers: openArray[
let response = client.request(innerUrl, httpMethod = innerMethod, body = body)
client.close()

return response.toResp(requestUrl = innerUrl, requestHeaders = innerHeaders, requestHttpMethod = httpMethod)
return response.toResp(requestUrl = innerUrl, requestHeaders = innerHeaders,
requestHttpMethod = httpMethod)


# Gnerating procs for individual HTTP methods
Expand Down
1 change: 1 addition & 0 deletions src/yahttp/exceptions.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type HttpError* = object of IOError
7 changes: 7 additions & 0 deletions tests/test_yahttp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ test "OK response":

test "Not OK response":
check not Response(status: 404).ok()

test "Exception for 4xx and 5xx":
expect HttpError:
Response(status: 400).raiseForStatus()

expect HttpError:
Response(status: 599).raiseForStatus()
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.6.0"
version = "0.7.0"
author = "Denis Mishankov"
description = "Awesome simple HTTP client"
license = "MIT"
Expand Down

0 comments on commit 0739d55

Please sign in to comment.