Skip to content
This repository was archived by the owner on Jun 2, 2026. It is now read-only.

Commit abd3108

Browse files
authored
fix client constructor (#28)
1 parent 254e5ad commit abd3108

3 files changed

Lines changed: 87 additions & 23 deletions

File tree

defectdojo_api_generated/client.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# custom-templates/my_client.mustache
22
"""DefectDojo Client"""
33

4+
from typing import Optional, Tuple, overload
45
from urllib.parse import urlparse
56
from urllib.request import getproxies, proxy_bypass
67

@@ -11,36 +12,63 @@
1112
class DefectDojo:
1213
"""API client for DefectDojo.
1314
14-
:param base_url: base URL of the DefectDojo instance.
15+
:param base_url: Base URL of the DefectDojo instance.
1516
:param token: API token to use with DefectDojo.
1617
Use this OR auth, not both.
17-
:param auth: tuple with username and password for basic authentication with DefectDojo.
18+
:param auth: Tuple with username and password for basic authentication with DefectDojo.
1819
Use this OR token, not both.
20+
:param verify_ssl: Set this to false to skip verifying SSL server certificate.
1921
:param config: Configuration object to use. If provided, all other parameters are ignored.
2022
"""
2123

22-
def __init__(self, base_url: str, token: str = None, auth: tuple[str] = None, config: Configuration = None):
24+
@overload
25+
def __init__(
26+
self,
27+
*,
28+
base_url: Optional[str] = None,
29+
token: Optional[str] = None,
30+
auth: Optional[Tuple[str, str]] = None,
31+
verify_ssl: bool = True,
32+
): ...
33+
34+
@overload
35+
def __init__(
36+
self,
37+
*,
38+
config: Configuration,
39+
): ...
40+
41+
def __init__(
42+
self,
43+
*,
44+
base_url: Optional[str] = None,
45+
token: Optional[str] = None,
46+
auth: Optional[Tuple[str, str]] = None,
47+
verify_ssl: bool = True,
48+
config: Optional[Configuration] = None,
49+
):
2350
if token is not None and auth is not None:
2451
raise ValueError('Provide `token` OR `auth`, not both.')
2552
if auth is not None and (not isinstance(auth, tuple) or len(auth) != 2):
2653
raise ValueError('`auth` needs to be a tuple with 2 elements, username and password')
2754

2855
if config is None:
29-
# drop last / (if any) as it is already added as part of endpoints
30-
kwargs = {'host': base_url.rstrip('/')}
56+
kwargs = {}
57+
if base_url is not None:
58+
kwargs['host'] = base_url
3159

3260
if token is not None:
3361
kwargs.update({'api_key': {'tokenAuth': token}, 'api_key_prefix': {'tokenAuth': 'Token'}})
3462
elif auth is not None:
3563
kwargs.update({'username': auth[0], 'password': auth[1]})
3664

3765
self.config = Configuration(**kwargs)
66+
self.config.verify_ssl = verify_ssl
3867
else:
3968
self.config = config
4069

41-
# TODO: just python-requests as generator library...
42-
if self.config.proxy is None:
43-
scheme, host, *_ = urlparse(base_url)
70+
if self.config.proxy is None and self.config.host:
71+
scheme, host, *_ = urlparse(self.config.host)
4472
if not proxy_bypass(host):
4573
self.config.proxy = getproxies().get(scheme)
4674

example.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
#!/usr/bin/env python3
22

3-
from defectdojo_api_generated import DefectDojo
3+
from defectdojo_api_generated import Configuration, DefectDojo
44

55
if __name__ == '__main__':
66
# password publicly available in https://github.com/DefectDojo/django-DefectDojo/?tab=readme-ov-file#demo
7-
dojo = DefectDojo('https://demo.defectdojo.org', auth=('admin', '1Defectdojo@demo#appsec'))
8-
r = dojo.findings_api.findings_list()
9-
print(f'{r.count} findings, example:\n')
10-
print(r.results[0])
7+
dojo = DefectDojo(base_url='https://demo.defectdojo.org/', auth=('admin', '1Defectdojo@demo#appsec'))
8+
r = dojo.findings_api.findings_list(limit=1)
9+
print(f'{r.count} findings, example:')
10+
print(f'- [{r.results[0].severity}] {r.results[0].title} - {r.results[0].description}')
11+
12+
dojo = DefectDojo(
13+
config=Configuration(host='https://demo.defectdojo.org', username='admin', password='1Defectdojo@demo#appsec')
14+
)
15+
r = dojo.system_settings_api.system_settings_list(limit=1)
16+
print(f'{r.count} settings')
17+
print(f'- {r.results[0]}')

support/api_generation/custom_templates/client.mustache

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,74 @@
11
# custom-templates/my_client.mustache
22
"""DefectDojo Client"""
33

4+
from typing import Optional, Tuple, overload
5+
from urllib.request import proxy_bypass, getproxies
6+
from urllib.parse import urlparse
7+
48
from {{packageName}}.api_client import ApiClient as _ApiClient
59
from {{packageName}}.configuration import Configuration
610

7-
from urllib.request import proxy_bypass, getproxies
8-
from urllib.parse import urlparse
911

1012
class DefectDojo:
1113
"""API client for DefectDojo.
1214

13-
:param base_url: base URL of the DefectDojo instance.
15+
:param base_url: Base URL of the DefectDojo instance.
1416
:param token: API token to use with DefectDojo.
1517
Use this OR auth, not both.
16-
:param auth: tuple with username and password for basic authentication with DefectDojo.
18+
:param auth: Tuple with username and password for basic authentication with DefectDojo.
1719
Use this OR token, not both.
20+
:param verify_ssl: Set this to false to skip verifying SSL server certificate.
1821
:param config: Configuration object to use. If provided, all other parameters are ignored.
1922
"""
2023

21-
def __init__(self, base_url: str, token: str = None, auth: tuple[str] = None, config: Configuration = None):
24+
@overload
25+
def __init__(
26+
self,
27+
*,
28+
base_url: Optional[str] = None,
29+
token: Optional[str] = None,
30+
auth: Optional[Tuple[str, str]] = None,
31+
verify_ssl: bool = True,
32+
): ...
33+
34+
@overload
35+
def __init__(
36+
self,
37+
*,
38+
config: Configuration,
39+
): ...
40+
41+
def __init__(
42+
self,
43+
*,
44+
base_url: Optional[str] = None,
45+
token: Optional[str] = None,
46+
auth: Optional[Tuple[str, str]] = None,
47+
verify_ssl: bool = True,
48+
config: Optional[Configuration] = None,
49+
):
2250
if token is not None and auth is not None:
2351
raise ValueError('Provide `token` OR `auth`, not both.')
2452
if auth is not None and (not isinstance(auth, tuple) or len(auth) != 2):
2553
raise ValueError('`auth` needs to be a tuple with 2 elements, username and password')
2654

2755
if config is None:
28-
# drop last / (if any) as it is already added as part of endpoints
29-
kwargs = {'host': base_url.rstrip('/')}
56+
kwargs = {}
57+
if base_url is not None:
58+
kwargs['host'] = base_url
3059

3160
if token is not None:
3261
kwargs.update({'api_key': {'tokenAuth': token}, 'api_key_prefix': {'tokenAuth': 'Token'}})
3362
elif auth is not None:
3463
kwargs.update({'username': auth[0], 'password': auth[1]})
3564

3665
self.config = Configuration(**kwargs)
66+
self.config.verify_ssl = verify_ssl
3767
else:
3868
self.config = config
3969

40-
# TODO: just python-requests as generator library...
41-
if self.config.proxy is None:
42-
scheme, host, *_ = urlparse(base_url)
70+
if self.config.proxy is None and self.config.host:
71+
scheme, host, *_ = urlparse(self.config.host)
4372
if not proxy_bypass(host):
4473
self.config.proxy = getproxies().get(scheme)
4574

0 commit comments

Comments
 (0)