Skip to content

Commit 162e297

Browse files
committed
Move request handling helper functions into utils module.
1 parent fad886c commit 162e297

File tree

3 files changed

+52
-41
lines changed

3 files changed

+52
-41
lines changed

pygitguardian/client.py

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
from typing import Any, Dict, List, Optional, Union, cast
1010

1111
import requests
12-
from requests import Response, Session, codes
12+
from requests import Response, Session
1313

1414
from .config import DEFAULT_API_VERSION, DEFAULT_BASE_URI, DEFAULT_TIMEOUT
15+
from .utils.response import (
16+
is_ok,
17+
is_create_ok,
18+
load_detail,
19+
)
1520
from .iac_models import (
1621
IaCDiffScanResult,
1722
IaCScanParameters,
@@ -63,46 +68,6 @@ class Versions:
6368
VERSIONS = Versions()
6469

6570

66-
def load_detail(resp: Response) -> Detail:
67-
"""
68-
load_detail loads a Detail from a response
69-
be it JSON or html.
70-
71-
:param resp: API response
72-
:type resp: Response
73-
:return: detail object of response
74-
:rtype: Detail
75-
"""
76-
if resp.headers["content-type"] == "application/json":
77-
data = resp.json()
78-
else:
79-
data = {"detail": resp.text}
80-
81-
return Detail.from_dict(data)
82-
83-
84-
def is_ok(resp: Response) -> bool:
85-
"""
86-
is_ok returns True is the API responded with 200
87-
and the content type is JSON.
88-
"""
89-
return (
90-
resp.headers["content-type"] == "application/json"
91-
and resp.status_code == codes.ok
92-
)
93-
94-
95-
def is_create_ok(resp: Response) -> bool:
96-
"""
97-
is_create_ok returns True if the API returns code 201
98-
and the content type is JSON.
99-
"""
100-
return (
101-
resp.headers["content-type"] == "application/json"
102-
and resp.status_code == codes.created
103-
)
104-
105-
10671
def _create_tar(root_path: Path, filenames: List[str]) -> bytes:
10772
"""
10873
:param root_path: the root_path from which the tar is created

pygitguardian/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Various utility functions"""

pygitguardian/utils/response.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from requests import Response, codes
2+
3+
from ..models import Detail
4+
5+
6+
def load_detail(resp: Response) -> Detail:
7+
"""
8+
load_detail loads a Detail from a response
9+
be it JSON or html.
10+
11+
:param resp: API response
12+
:type resp: Response
13+
:return: detail object of response
14+
:rtype: Detail
15+
"""
16+
if resp.headers["content-type"] == "application/json":
17+
data = resp.json()
18+
else:
19+
data = {"detail": resp.text}
20+
21+
return Detail.from_dict(data)
22+
23+
24+
def is_ok(resp: Response) -> bool:
25+
"""
26+
is_ok returns True is the API responded with 200
27+
and the content type is JSON.
28+
"""
29+
return (
30+
resp.headers["content-type"] == "application/json"
31+
and resp.status_code == codes.ok
32+
)
33+
34+
35+
def is_create_ok(resp: Response) -> bool:
36+
"""
37+
is_create_ok returns True if the API returns code 201
38+
and the content type is JSON.
39+
"""
40+
return (
41+
resp.headers["content-type"] == "application/json"
42+
and resp.status_code == codes.created
43+
)
44+
45+

0 commit comments

Comments
 (0)