Skip to content

Commit f73cd23

Browse files
author
João Guerreiro
committed
fix(config): refactor API constants into config file
1 parent cc7b151 commit f73cd23

File tree

8 files changed

+26
-28
lines changed

8 files changed

+26
-28
lines changed

Pipfile

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ verify_ssl = true
44
name = "pypi"
55

66
[packages]
7-
marshmallow = "~=3.5"
87
pygitguardian = {editable = true,path = "."}
9-
requests = ">=2.21.0"
108

119
[dev-packages]
1210
black = "==19.10b0"
@@ -19,6 +17,3 @@ nose = "*"
1917
pre-commit = "*"
2018
vcrpy = "*"
2119
vcrpy-unittest = "*"
22-
23-
[pipenv]
24-
allow_prereleases = true

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ pipenv install pygitguardian
3333
poetry add pygitguardian
3434
```
3535

36-
Check [examples/content_scan.py](examples/content_scan.py) for an example usage of the API.
36+
## Examples
37+
38+
Check [examples/](examples/) for an example usages of the API.
3739

3840
### Dependencies
3941

40-
Py-gitguardian depends on these excelent libraries:
42+
Py-gitguardian depends on these excellent libraries:
4143

4244
- `requests` - HTTP client
4345
- `marshmallow` - Request (de)serialization and input validation

examples/content_scan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pygitguardian import GGClient
88

99

10-
API_KEY = os.getenv("GG_APIKEY")
10+
API_KEY = os.getenv("GG_API_KEY")
1111
FILENAME = ".env"
1212
DOCUMENT = """
1313
import urllib.request

pygitguardian/client.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66
from marshmallow import Schema
77
from requests import Response, Session, codes
88

9+
from .config import DEFAULT_API_VERSION, DEFAULT_BASE_URI, DEFAULT_TIMEOUT
910
from .models import Detail, ScanResult
1011
from .schemas import DetailSchema, DocumentSchema, ScanResultSchema
1112

1213

13-
_BASE_URI = "https://api.gitguardian.com"
14-
_API_VERSION = "v1"
15-
_DEFAULT_TIMEOUT = 20.0 # 20s default timeout
16-
17-
1814
class GGClient:
1915
DETAIL_SCHEMA = DetailSchema()
2016
DOCUMENT_SCHEMA = DocumentSchema()
@@ -27,7 +23,7 @@ def __init__(
2723
base_uri: Optional[str] = None,
2824
session: Optional[requests.Session] = None,
2925
user_agent: Optional[str] = None,
30-
timeout: Optional[float] = _DEFAULT_TIMEOUT,
26+
timeout: Optional[float] = DEFAULT_TIMEOUT,
3127
) -> "GGClient":
3228
"""
3329
:param token: APIKey to be added to requests
@@ -43,7 +39,7 @@ def __init__(
4339
if not base_uri.startswith(("http://", "https://")):
4440
raise ValueError("Invalid protocol, prepend with http:// or https://")
4541
else:
46-
base_uri = _BASE_URI
42+
base_uri = DEFAULT_BASE_URI
4743

4844
if not isinstance(token, str):
4945
raise TypeError("Missing token string")
@@ -70,7 +66,7 @@ def request(
7066
method: str,
7167
endpoint: str,
7268
schema: Schema = None,
73-
version: str = _API_VERSION,
69+
version: str = DEFAULT_API_VERSION,
7470
many: bool = False,
7571
**kwargs
7672
) -> Tuple[Any, Response]:
@@ -104,7 +100,7 @@ def post(
104100
endpoint: str,
105101
data: str = None,
106102
schema: Schema = None,
107-
version: str = _API_VERSION,
103+
version: str = DEFAULT_API_VERSION,
108104
many: bool = False,
109105
**kwargs
110106
) -> Tuple[Any, Response]:
@@ -122,7 +118,7 @@ def get(
122118
self,
123119
endpoint: str,
124120
schema: Schema = None,
125-
version: str = _API_VERSION,
121+
version: str = DEFAULT_API_VERSION,
126122
many: bool = False,
127123
**kwargs
128124
) -> Tuple[Any, Response]:
@@ -166,7 +162,7 @@ def multi_content_scan(
166162
if all(isinstance(doc, dict) for doc in documents):
167163
request_obj = self.DOCUMENT_SCHEMA.load(documents, many=True)
168164
else:
169-
raise TypeError("documents must be a dict")
165+
raise TypeError("each document must be a dict")
170166

171167
obj, resp = self.post(
172168
endpoint="multiscan",

pygitguardian/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DEFAULT_BASE_URI = "https://api.gitguardian.com"
2+
DEFAULT_API_VERSION = "v1"
3+
DEFAULT_TIMEOUT = 20.0 # 20s default timeout
4+
5+
DOCUMENT_SIZE_THRESHOLD_BYTES = 1048576 # 1MB

pygitguardian/schemas.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
validates,
1414
)
1515

16+
from .config import DOCUMENT_SIZE_THRESHOLD_BYTES
1617
from .models import Detail, Match, PolicyBreak, ScanResult
1718

1819

19-
MB = 1048576
20-
21-
2220
class DocumentSchema(Schema):
2321
class Meta:
2422
unknown = EXCLUDE
@@ -32,13 +30,15 @@ def validate_document(self, document: str) -> str:
3230
validate that document is smaller than scan limit
3331
"""
3432
encoded = document.encode("utf-8")
35-
if len(encoded) > MB:
33+
if len(encoded) > DOCUMENT_SIZE_THRESHOLD_BYTES:
3634
raise ValidationError(
37-
"This file exceeds the maximum allowed size of {}B".format(MB)
35+
"file exceeds the maximum allowed size of {}B".format(
36+
DOCUMENT_SIZE_THRESHOLD_BYTES
37+
)
3838
)
3939

4040
if "\x00" in document:
41-
raise ValidationError("Document has null characters")
41+
raise ValidationError("document has null characters")
4242

4343
return document
4444

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def readme():
2626
version=__version__,
2727
packages=find_packages(exclude=["tests"]),
2828
description=readme(),
29-
install_requires=["marshmallow", "requests"],
29+
install_requires=["marshmallow>=3.5", "requests>=2"],
3030
include_package_data=True,
3131
author="GitGuardian",
3232
author_email="[email protected]",

tests/test_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from vcr_unittest import VCRTestCase
66

77
from pygitguardian import GGClient, ScanResultSchema
8-
from pygitguardian.client import _BASE_URI
8+
from pygitguardian.config import DEFAULT_BASE_URI
99

1010

1111
FILENAME = ".env"
@@ -225,7 +225,7 @@ def test_client_creation(self):
225225
if entry.uri:
226226
self.assertEqual(client.base_uri, entry.uri)
227227
else:
228-
self.assertEqual(client.base_uri, _BASE_URI)
228+
self.assertEqual(client.base_uri, DEFAULT_BASE_URI)
229229
self.assertEqual(client.token, entry.token)
230230
self.assertTrue(
231231
entry.user_agent in client.session.headers["User-Agent"],

0 commit comments

Comments
 (0)