Skip to content

Commit 59d9b23

Browse files
committed
Add documentation for HTTP APIs, small refactoring
Sphinx documentation pages and some additional docstrings have been added. The "api" module in gvm.http.core is renamed to "_api" and url_join is now a class method of HttpApiConnector.
1 parent 22cc490 commit 59d9b23

File tree

14 files changed

+121
-27
lines changed

14 files changed

+121
-27
lines changed

docs/api/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ utilities and xml helpers.
1616
connections
1717
transforms
1818
protocols
19+
http
1920
errors
2021
other

docs/api/http.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.. _http:
2+
3+
HTTP APIs
4+
---------
5+
6+
.. automodule:: gvm.http
7+
8+
.. toctree::
9+
:maxdepth: 1
10+
11+
httpcore
12+
openvasdv1

docs/api/httpcore.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.. _httpcore:
2+
3+
HTTP core classes
4+
^^^^^^^^^^^^^^^^^
5+
6+
Connector
7+
#########
8+
9+
.. automodule:: gvm.http.core.connector
10+
11+
.. autoclass:: HttpApiConnector
12+
:members:
13+
14+
Headers
15+
#######
16+
17+
.. automodule:: gvm.http.core.headers
18+
19+
.. autoclass:: ContentType
20+
:members:
21+
22+
Response
23+
########
24+
25+
.. automodule:: gvm.http.core.response
26+
27+
.. autoclass:: HttpResponse
28+
:members:

docs/api/openvasdv1.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.. _openvasdv1:
2+
3+
openvasd v1
4+
^^^^^^^^^^^
5+
6+
.. automodule:: gvm.http.openvasd.openvasd1
7+
8+
.. autoclass:: OpenvasdHttpApiV1
9+
:members:

gvm/http/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
# SPDX-FileCopyrightText: 2025 Greenbone AG
22
#
33
# SPDX-License-Identifier: GPL-3.0-or-later
4+
"""
5+
Package for supported Greenbone HTTP APIs.
6+
7+
Currently only `openvasd version 1`_ is supported.
8+
9+
.. _openvasd version 1:
10+
https://greenbone.github.io/scanner-api/#/
11+
"""

gvm/http/core/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
# SPDX-FileCopyrightText: 2025 Greenbone AG
22
#
33
# SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
"""
6+
HTTP core classes
7+
"""

gvm/http/core/api.py renamed to gvm/http/core/_api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#
33
# SPDX-License-Identifier: GPL-3.0-or-later
44

5+
"""
6+
Base class module for GVM HTTP APIs
7+
"""
8+
59
from typing import Optional
610

711
from gvm.http.core.connector import HttpApiConnector

gvm/http/core/connector.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,17 @@
22
#
33
# SPDX-License-Identifier: GPL-3.0-or-later
44

5+
"""
6+
Module for handling GVM HTTP API connections
7+
"""
8+
59
import urllib.parse
610
from typing import Optional, Tuple, Dict, Any
711

812
from requests import Session
913

1014
from gvm.http.core.response import HttpResponse
1115

12-
13-
def url_join(base: str, rel_path: str) -> str:
14-
"""
15-
Combines a base URL and a relative path into one URL.
16-
17-
Unlike `urrlib.parse.urljoin` the base path will always be the parent of the relative path as if it
18-
ends with "/".
19-
"""
20-
if base.endswith("/"):
21-
return urllib.parse.urljoin(base, rel_path)
22-
else:
23-
return urllib.parse.urljoin(base + "/", rel_path)
24-
25-
2616
class HttpApiConnector:
2717
"""
2818
Class for connecting to HTTP based API servers, sending requests and receiving the responses.
@@ -35,6 +25,19 @@ def _new_session(cls):
3525
"""
3626
return Session()
3727

28+
@classmethod
29+
def url_join(cls, base: str, rel_path: str) -> str:
30+
"""
31+
Combines a base URL and a relative path into one URL.
32+
33+
Unlike `urrlib.parse.urljoin` the base path will always be the parent of the relative path as if it
34+
ends with "/".
35+
"""
36+
if base.endswith("/"):
37+
return urllib.parse.urljoin(base, rel_path)
38+
else:
39+
return urllib.parse.urljoin(base + "/", rel_path)
40+
3841
def __init__(
3942
self,
4043
base_url: str,
@@ -93,7 +96,7 @@ def delete(
9396
Return:
9497
The HTTP response.
9598
"""
96-
url = url_join(self.base_url, rel_path)
99+
url = self.url_join(self.base_url, rel_path)
97100
r = self._session.delete(url, params=params, headers=headers)
98101
if raise_for_status:
99102
r.raise_for_status()
@@ -119,7 +122,7 @@ def get(
119122
Return:
120123
The HTTP response.
121124
"""
122-
url = url_join(self.base_url, rel_path)
125+
url = self.url_join(self.base_url, rel_path)
123126
r = self._session.get(url, params=params, headers=headers)
124127
if raise_for_status:
125128
r.raise_for_status()
@@ -147,7 +150,7 @@ def post_json(
147150
Return:
148151
The HTTP response.
149152
"""
150-
url = url_join(self.base_url, rel_path)
153+
url = self.url_join(self.base_url, rel_path)
151154
r = self._session.post(url, json=json, params=params, headers=headers)
152155
if raise_for_status:
153156
r.raise_for_status()

gvm/http/core/headers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# SPDX-FileCopyrightText: 2025 Greenbone AG
22
#
33
# SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
"""
6+
Module for handling special HTTP headers
7+
"""
8+
49
from dataclasses import dataclass
510
from typing import Self, Dict, Optional
611

gvm/http/core/response.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# SPDX-FileCopyrightText: 2025 Greenbone AG
22
#
33
# SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
"""
6+
Module for abstracting HTTP responses
7+
"""
8+
49
from dataclasses import dataclass
510
from typing import Any, Dict, Self, Optional
611
from requests import Request, Response
@@ -14,9 +19,16 @@ class HttpResponse:
1419
Class representing an HTTP response.
1520
"""
1621
body: Any
22+
"The body of the response"
23+
1724
status: int
25+
"HTTP status code of the response"
26+
1827
headers: Dict[str, str]
28+
"Dict containing the headers of the response"
29+
1930
content_type: Optional[ContentType]
31+
"The content type of the response if it was included in the headers"
2032

2133
@classmethod
2234
def from_requests_lib(cls, r: Response) -> Self:

0 commit comments

Comments
 (0)