Skip to content

Commit f28a13b

Browse files
author
João Guerreiro
committed
feat(doc): add new examples
1 parent f73cd23 commit f28a13b

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,50 @@ poetry add pygitguardian
3737

3838
Check [examples/](examples/) for an example usages of the API.
3939

40+
### Scanning a piece of text
41+
42+
```py
43+
API_KEY = os.getenv("GG_API_KEY")
44+
FILENAME = ".env"
45+
DOCUMENT = """
46+
import urllib.request
47+
url = 'http://jen_barber:[email protected]/isreal.json'
48+
response = urllib.request.urlopen(url)
49+
consume(response.read())"
50+
"""
51+
52+
client = GGClient(token=API_KEY)
53+
54+
# Check the health of the API and the token used.
55+
health_obj, status = client.health_check()
56+
57+
if status != codes[r"\o/"]: # this is 200 but cooler
58+
print("Invalid API Key")
59+
60+
try:
61+
scan_result = client.content_scan(filename=FILENAME, document=DOCUMENT)
62+
except Exception as exc:
63+
# Handle exceptions such as schema validation
64+
traceback.print_exc(2, file=sys.stderr)
65+
print(str(exc))
66+
67+
print("Scan results:", scan_result.has_secrets, "-", scan_result.policy_break_count)
68+
```
69+
70+
### Scanning multiple files
71+
72+
```py
73+
API_KEY = os.getenv("GG_API_KEY")
74+
client = GGClient(token=API_KEY)
75+
# Create a list of dictionaries for scanning
76+
to_scan = []
77+
for name in glob.glob("**/*"):
78+
with open(name) as fn:
79+
to_scan.append({"document": fn.read(), "filename": os.path.basename(name)})
80+
81+
scan, status_code = client.multi_content_scan(to_scan)
82+
```
83+
4084
### Dependencies
4185

4286
Py-gitguardian depends on these excellent libraries:

examples/directory_scan.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import glob
2+
import os
3+
import sys
4+
import traceback
5+
6+
from pygitguardian import GGClient
7+
from pygitguardian.config import MULTI_DOCUMENT_LIMIT
8+
9+
10+
API_KEY = os.getenv("GG_API_KEY")
11+
12+
client = GGClient(token=API_KEY)
13+
14+
# Create a list of dictionaries for scanning
15+
to_scan = []
16+
for name in glob.glob("**/*"):
17+
with open(name) as fn:
18+
to_scan.append({"document": fn.read(), "filename": os.path.basename(name)})
19+
20+
# Process in a chunked way to avoid passing the multi document limit
21+
to_process = []
22+
for i in range(0, len(to_scan), MULTI_DOCUMENT_LIMIT):
23+
chunk = to_scan[i : i + MULTI_DOCUMENT_LIMIT]
24+
try:
25+
scan, status_code = client.multi_content_scan(chunk)
26+
except Exception as exc:
27+
# Handle exceptions such as schema validation
28+
traceback.print_exc(2, file=sys.stderr)
29+
print(str(exc))
30+
if status_code != 200:
31+
print("Error scanning some files. Results may be incomplete.")
32+
to_process.extend(scan)
33+
34+
for scan_result in to_process:
35+
print("Scan results:", scan_result.has_secrets, "-", scan_result.policy_break_count)

0 commit comments

Comments
 (0)