-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_client_system.py
More file actions
67 lines (57 loc) · 2.19 KB
/
Copy pathapi_client_system.py
File metadata and controls
67 lines (57 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python
from typing import Any
from agent_utilities.base_utilities import get_logger
from pydantic import ValidationError
logger = get_logger(__name__)
from agent_utilities.core.exceptions import (
ParameterError,
)
from gitlab_api.api.api_client_base import GitLabApiBase
from gitlab_api.gitlab_response_models import (
Response,
)
class GitLabApiSystem(GitLabApiBase):
def api_request(
self,
method: str,
endpoint: str,
data: dict[str, Any] | None = None,
json: dict[str, Any] | None = None,
) -> Response:
"""
Make a custom API request to the GitLab server.
Args:
method: The HTTP method to use (GET, POST, PUT, DELETE).
endpoint: The API endpoint to call.
data: The data to send in the request body (for form data).
json: The JSON data to send in the request body.
Returns:
Response: A wrapper containing the original response and the response data (if applicable).
Raises:
ValueError: If an unsupported HTTP method is provided.
ParameterError: If invalid parameters are provided.
HTTPError: If the API request fails.
"""
if method.upper() not in ["GET", "POST", "PUT", "DELETE"]:
raise ValueError(f"Unsupported HTTP method: {method.upper()}")
try:
request_func = getattr(self._session, method.lower())
response = request_func(
url=f"{self.url}/{endpoint.lstrip('/')}",
headers=self.headers,
data=data,
json=json,
)
response.raise_for_status()
parsed_data = (
response.json()
if response.content
and "application/json" in response.headers.get("Content-Type", "")
else None
)
return Response(response=response, data=parsed_data)
except ValidationError as e:
raise ParameterError(f"Invalid parameters: {e.errors()}") from e
except Exception as e:
logger.error("GitLab request failed: error_type=%s", type(e).__name__)
raise