Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement raiseForStatus #8

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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