Skip to content

Commit 6ebf350

Browse files
authored
chore: add v4 examples (#157)
1 parent d7591b3 commit 6ebf350

6 files changed

Lines changed: 295 additions & 0 deletions

File tree

README.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Bring the VulnCheck API to your Python applications.
1818
- [PURL](#purl)
1919
- [CPE](#cpe)
2020
- [Backup](#backup)
21+
- [Advisory](#advisory)
22+
- [Backup v4](#backup-v4)
2123
- [Indices](#indices)
2224
- [Index](#index)
2325
- [Pagination](#pagination)
@@ -392,6 +394,160 @@ if __name__ == "__main__":
392394
```
393395

394396

397+
</details>
398+
399+
### Advisory
400+
401+
List all advisory feeds and query advisories filtered by feed
402+
403+
```python
404+
import vulncheck_sdk
405+
from vulncheck_sdk.models.search_v4_advisory_return_value import SearchV4AdvisoryReturnValue
406+
from vulncheck_sdk.models.search_v4_list_feed_return_value import SearchV4ListFeedReturnValue
407+
import os
408+
409+
TOKEN = os.environ["VULNCHECK_API_TOKEN"]
410+
411+
configuration = vulncheck_sdk.Configuration()
412+
configuration.api_key["Bearer"] = TOKEN
413+
414+
with vulncheck_sdk.ApiClient(configuration) as api_client:
415+
advisory_client = vulncheck_sdk.AdvisoryApi(api_client)
416+
417+
# List all available advisory feeds (/v4/advisory)
418+
feeds: SearchV4ListFeedReturnValue = advisory_client.v4_list_advisory_feeds()
419+
print(f"Available feeds: {feeds.data}")
420+
421+
# Query advisories filtered by feed=wolfi (/v4/advisory?feed=wolfi)
422+
advisories: SearchV4AdvisoryReturnValue = advisory_client.v4_query_advisories(name="wolfi")
423+
print(f"Wolfi advisories (page 1): {len(advisories.data)} results")
424+
for advisory in advisories.data:
425+
print(f" - {advisory.id}")
426+
```
427+
428+
429+
<details><summary><b>Click to View Async Implementation</b></summary>
430+
431+
```python
432+
import asyncio
433+
import os
434+
import vulncheck_sdk.aio as vcaio
435+
from vulncheck_sdk.aio.models.search_v4_advisory_return_value import SearchV4AdvisoryReturnValue
436+
from vulncheck_sdk.aio.models.search_v4_list_feed_return_value import SearchV4ListFeedReturnValue
437+
438+
TOKEN = os.environ.get("VULNCHECK_API_TOKEN")
439+
440+
configuration = vcaio.Configuration()
441+
configuration.api_key["Bearer"] = TOKEN
442+
443+
444+
async def main():
445+
async with vcaio.ApiClient(configuration) as api_client:
446+
advisory_client = vcaio.AdvisoryApi(api_client)
447+
448+
# List all available advisory feeds (/v4/advisory)
449+
feeds: SearchV4ListFeedReturnValue = await advisory_client.v4_list_advisory_feeds()
450+
print(f"Available feeds: {feeds.data}")
451+
452+
# Query advisories filtered by feed=wolfi (/v4/advisory?feed=wolfi)
453+
advisories: SearchV4AdvisoryReturnValue = await advisory_client.v4_query_advisories(name="wolfi")
454+
print(f"Wolfi advisories (page 1): {len(advisories.data)} results")
455+
for advisory in advisories.data:
456+
print(f" - {advisory.id}")
457+
458+
459+
if __name__ == "__main__":
460+
asyncio.run(main())
461+
```
462+
463+
464+
</details>
465+
466+
### Backup v4
467+
468+
List available v4 backups and download a backup by feed name
469+
470+
```python
471+
import urllib.request
472+
import vulncheck_sdk
473+
from vulncheck_sdk.models.backup_list_backups_response import BackupListBackupsResponse
474+
from vulncheck_sdk.models.backup_feed_item import BackupFeedItem
475+
import os
476+
477+
TOKEN = os.environ["VULNCHECK_API_TOKEN"]
478+
479+
configuration = vulncheck_sdk.Configuration()
480+
configuration.api_key["Bearer"] = TOKEN
481+
482+
with vulncheck_sdk.ApiClient(configuration) as api_client:
483+
backup_client = vulncheck_sdk.BackupApi(api_client)
484+
485+
# List available backups (/v4/backup)
486+
available: BackupListBackupsResponse = backup_client.v4_list_backups()
487+
for potential in available.data:
488+
print(f"found: {potential.name}")
489+
490+
# Get backup for the wolfi feed (/v4/backup/wolfi)
491+
feed = "wolfi"
492+
response = backup_client.v4_get_backup_by_name(feed)
493+
494+
file_path = f"{feed}.zip"
495+
with urllib.request.urlopen(response.url) as r:
496+
with open(file_path, "wb") as f:
497+
f.write(r.read())
498+
499+
print(f"Successfully saved to {file_path}")
500+
```
501+
502+
503+
<details><summary><b>Click to View Async Implementation</b></summary>
504+
505+
```python
506+
import asyncio
507+
import os
508+
import urllib.request
509+
import vulncheck_sdk.aio as vcaio
510+
from vulncheck_sdk.aio.models.backup_list_backups_response import BackupListBackupsResponse
511+
from vulncheck_sdk.aio.models.backup_backup_response import BackupBackupResponse
512+
513+
TOKEN = os.environ.get("VULNCHECK_API_TOKEN")
514+
515+
configuration = vcaio.Configuration()
516+
configuration.api_key["Bearer"] = TOKEN
517+
518+
519+
def download_sync(url, file_path):
520+
with urllib.request.urlopen(url) as response:
521+
with open(file_path, "wb") as file:
522+
file.write(response.read())
523+
524+
525+
async def main():
526+
async with vcaio.ApiClient(configuration) as api_client:
527+
backup_client = vcaio.BackupApi(api_client)
528+
529+
# List available backups (/v4/backup)
530+
available: BackupListBackupsResponse = await backup_client.v4_list_backups()
531+
for potential in available.data:
532+
print(f"Found backup: {potential.name}")
533+
534+
# Get backup for the wolfi feed (/v4/backup/wolfi)
535+
feed = "wolfi"
536+
response: BackupBackupResponse = await backup_client.v4_get_backup_by_name(feed)
537+
538+
file_path = f"{feed}.zip"
539+
print(f"Downloading {feed} backup via urllib (offloaded to thread)...")
540+
541+
await asyncio.to_thread(download_sync, response.url, file_path)
542+
543+
print(f"Successfully saved to {file_path}")
544+
545+
546+
if __name__ == "__main__":
547+
asyncio.run(main())
548+
```
549+
550+
395551
</details>
396552

397553
### Indices

tests/advisory.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import vulncheck_sdk
2+
from vulncheck_sdk.models.search_v4_advisory_return_value import SearchV4AdvisoryReturnValue
3+
from vulncheck_sdk.models.search_v4_list_feed_return_value import SearchV4ListFeedReturnValue
4+
import os
5+
6+
TOKEN = os.environ["VULNCHECK_API_TOKEN"]
7+
8+
configuration = vulncheck_sdk.Configuration()
9+
configuration.api_key["Bearer"] = TOKEN
10+
11+
with vulncheck_sdk.ApiClient(configuration) as api_client:
12+
advisory_client = vulncheck_sdk.AdvisoryApi(api_client)
13+
14+
# List all available advisory feeds (/v4/advisory)
15+
feeds: SearchV4ListFeedReturnValue = advisory_client.v4_list_advisory_feeds()
16+
print("Available feeds:")
17+
for feed in feeds.data:
18+
print(f"name: {feed.name}")
19+
20+
feed = "wolfi"
21+
# Query advisories filtered by feed=wolfi (/v4/advisory?feed=wolfi)
22+
advisories: SearchV4AdvisoryReturnValue = advisory_client.v4_query_advisories(name=feed)
23+
print(f"{feed.capitalize()} advisories (page 1): {len(advisories.data)} results")
24+
for advisory in advisories.data:
25+
print(f"cve: {advisory.cve_metadata.cve_id}")

tests/advisory_aio.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import asyncio
2+
import os
3+
import vulncheck_sdk.aio as vcaio
4+
from vulncheck_sdk.aio.models.search_v4_advisory_return_value import SearchV4AdvisoryReturnValue
5+
from vulncheck_sdk.aio.models.search_v4_list_feed_return_value import SearchV4ListFeedReturnValue
6+
7+
TOKEN = os.environ.get("VULNCHECK_API_TOKEN")
8+
9+
configuration = vcaio.Configuration()
10+
configuration.api_key["Bearer"] = TOKEN
11+
12+
13+
async def main():
14+
async with vcaio.ApiClient(configuration) as api_client:
15+
advisory_client = vcaio.AdvisoryApi(api_client)
16+
17+
# List all available advisory feeds (/v4/advisory)
18+
feeds: SearchV4ListFeedReturnValue = await advisory_client.v4_list_advisory_feeds()
19+
print("Available feeds:")
20+
for feed in feeds.data:
21+
print(f"name: {feed.name}")
22+
23+
feed = "wolfi"
24+
# Query advisories filtered by feed=wolfi (/v4/advisory?feed=wolfi)
25+
advisories: SearchV4AdvisoryReturnValue = await advisory_client.v4_query_advisories(name=feed)
26+
print(f"{feed.capitalize()} advisories (page 1): {len(advisories.data)} results")
27+
for advisory in advisories.data:
28+
print(f"cve: {advisory.cve_metadata.cve_id}")
29+
30+
31+
if __name__ == "__main__":
32+
asyncio.run(main())

tests/backupv4.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import urllib.request
2+
import vulncheck_sdk
3+
from vulncheck_sdk.models.backup_list_backups_response import BackupListBackupsResponse
4+
from vulncheck_sdk.models.backup_feed_item import BackupFeedItem
5+
import os
6+
7+
TOKEN = os.environ["VULNCHECK_API_TOKEN"]
8+
9+
configuration = vulncheck_sdk.Configuration()
10+
configuration.api_key["Bearer"] = TOKEN
11+
12+
with vulncheck_sdk.ApiClient(configuration) as api_client:
13+
backup_client = vulncheck_sdk.BackupApi(api_client)
14+
15+
# List available backups (/v4/backup)
16+
available: BackupListBackupsResponse = backup_client.v4_list_backups()
17+
18+
for potential_backup in available.data:
19+
print(f"Found backup: {potential_backup.name}")
20+
21+
# Get backup for the wolfi feed (/v4/backup/wolfi)
22+
feed = "wolfi"
23+
response: BackupListBackupsResponse = backup_client.v4_get_backup_by_name(feed)
24+
25+
print(f"Downloading {feed} backup")
26+
file_path = f"{feed}.zip"
27+
with urllib.request.urlopen(response.url) as r:
28+
with open(file_path, "wb") as f:
29+
f.write(r.read())
30+
31+
print(f"Successfully saved to {file_path}")

tests/backupv4_aio.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import asyncio
2+
import os
3+
import urllib.request
4+
import vulncheck_sdk.aio as vcaio
5+
from vulncheck_sdk.aio.models.backup_list_backups_response import BackupListBackupsResponse
6+
from vulncheck_sdk.aio.models.backup_backup_response import BackupBackupResponse
7+
8+
TOKEN = os.environ.get("VULNCHECK_API_TOKEN")
9+
10+
configuration = vcaio.Configuration()
11+
configuration.api_key["Bearer"] = TOKEN
12+
13+
14+
def download_sync(url, file_path):
15+
"""
16+
Standard synchronous download using urllib.request.
17+
This runs in a separate thread to avoid blocking the event loop.
18+
"""
19+
with urllib.request.urlopen(url) as response:
20+
with open(file_path, "wb") as file:
21+
file.write(response.read())
22+
23+
24+
async def main():
25+
async with vcaio.ApiClient(configuration) as api_client:
26+
backup_client = vcaio.BackupApi(api_client)
27+
28+
# List available backups (/v4/backup)
29+
available: BackupListBackupsResponse = await backup_client.v4_list_backups()
30+
for potential_backup in available.data:
31+
print(f"Found backup: {potential_backup.name}")
32+
33+
# Get backup for the wolfi feed (/v4/backup/wolfi)
34+
feed = "wolfi"
35+
response: BackupBackupResponse = await backup_client.v4_get_backup_by_name(feed)
36+
37+
38+
file_path = f"{feed}.zip"
39+
print(f"Downloading {feed} backup via urllib (offloaded to thread)...")
40+
41+
await asyncio.to_thread(download_sync, response.url, file_path)
42+
43+
print(f"Successfully saved to {file_path}")
44+
45+
46+
if __name__ == "__main__":
47+
asyncio.run(main())

tests/readme_examples_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313

1414
# List of python programs to test
1515
PROGRAMS = [
16+
"./tests/advisory.py",
17+
"./tests/advisory_aio.py",
1618
"./tests/backup.py",
19+
"./tests/backupv4.py",
1720
"./tests/backup_aio.py",
21+
"./tests/backupv4_aio.py",
1822
"./tests/cpe.py",
1923
"./tests/cpe_aio.py",
2024
"./tests/index.py",

0 commit comments

Comments
 (0)