|
1 | 1 | # custom-templates/my_client.mustache |
2 | 2 | """DefectDojo Client""" |
3 | 3 |
|
| 4 | +from typing import Optional, Tuple, overload |
| 5 | +from urllib.request import proxy_bypass, getproxies |
| 6 | +from urllib.parse import urlparse |
| 7 | + |
4 | 8 | from {{packageName}}.api_client import ApiClient as _ApiClient |
5 | 9 | from {{packageName}}.configuration import Configuration |
6 | 10 |
|
7 | | -from urllib.request import proxy_bypass, getproxies |
8 | | -from urllib.parse import urlparse |
9 | 11 |
|
10 | 12 | class DefectDojo: |
11 | 13 | """API client for DefectDojo. |
12 | 14 |
|
13 | | - :param base_url: base URL of the DefectDojo instance. |
| 15 | + :param base_url: Base URL of the DefectDojo instance. |
14 | 16 | :param token: API token to use with DefectDojo. |
15 | 17 | 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. |
17 | 19 | Use this OR token, not both. |
| 20 | + :param verify_ssl: Set this to false to skip verifying SSL server certificate. |
18 | 21 | :param config: Configuration object to use. If provided, all other parameters are ignored. |
19 | 22 | """ |
20 | 23 |
|
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 | + ): |
22 | 50 | if token is not None and auth is not None: |
23 | 51 | raise ValueError('Provide `token` OR `auth`, not both.') |
24 | 52 | if auth is not None and (not isinstance(auth, tuple) or len(auth) != 2): |
25 | 53 | raise ValueError('`auth` needs to be a tuple with 2 elements, username and password') |
26 | 54 |
|
27 | 55 | 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 |
30 | 59 |
|
31 | 60 | if token is not None: |
32 | 61 | kwargs.update({'api_key': {'tokenAuth': token}, 'api_key_prefix': {'tokenAuth': 'Token'}}) |
33 | 62 | elif auth is not None: |
34 | 63 | kwargs.update({'username': auth[0], 'password': auth[1]}) |
35 | 64 |
|
36 | 65 | self.config = Configuration(**kwargs) |
| 66 | + self.config.verify_ssl = verify_ssl |
37 | 67 | else: |
38 | 68 | self.config = config |
39 | 69 |
|
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) |
43 | 72 | if not proxy_bypass(host): |
44 | 73 | self.config.proxy = getproxies().get(scheme) |
45 | 74 |
|
|
0 commit comments