From 0739d55f27141e42bb2f6a235c5df23fede293a0 Mon Sep 17 00:00:00 2001 From: Denis Mishankov Date: Tue, 19 Dec 2023 06:16:05 +0300 Subject: [PATCH] Implement raiseForStatus (#8) * add raiseForStatus * pretty * bump version --- src/yahttp.nim | 18 ++++++++++++++---- src/yahttp/exceptions.nim | 1 + tests/test_yahttp.nim | 7 +++++++ yahttp.nimble | 2 +- 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 src/yahttp/exceptions.nim diff --git a/src/yahttp.nim b/src/yahttp.nim index 1e6ebb4..a13ca43 100644 --- a/src/yahttp.nim +++ b/src/yahttp.nim @@ -1,6 +1,7 @@ import base64, httpclient, net, json, uri, strutils, tables import yahttp/internal/utils +import yahttp/exceptions type ## Types without methods @@ -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 = @@ -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: '&') @@ -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) @@ -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 diff --git a/src/yahttp/exceptions.nim b/src/yahttp/exceptions.nim new file mode 100644 index 0000000..437ad8d --- /dev/null +++ b/src/yahttp/exceptions.nim @@ -0,0 +1 @@ +type HttpError* = object of IOError diff --git a/tests/test_yahttp.nim b/tests/test_yahttp.nim index 3b4e65b..535fc9b 100644 --- a/tests/test_yahttp.nim +++ b/tests/test_yahttp.nim @@ -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() diff --git a/yahttp.nimble b/yahttp.nimble index ae0e2b7..f77d1b4 100644 --- a/yahttp.nimble +++ b/yahttp.nimble @@ -1,6 +1,6 @@ # Package -version = "0.6.0" +version = "0.7.0" author = "Denis Mishankov" description = "Awesome simple HTTP client" license = "MIT"