@@ -37,6 +37,50 @@ poetry add pygitguardian
37
37
38
38
Check [ examples/] ( examples/ ) for an example usages of the API.
39
39
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
+
40
84
### Dependencies
41
85
42
86
Py-gitguardian depends on these excellent libraries:
0 commit comments