Skip to content

Commit 1957eee

Browse files
committed
feat(graphql): Add class method to create Gitlab connection from configuration files
This commit introduces a new class method from_config to the GraphQL class. This method allows users to create a Gitlab connection object by reading configuration files. Users can specify the ID and paths of the configuration files, and the method will parse the connection information (e.g., URL, tokens, SSL verification, etc.) from the files to create a pre-configured Gitlab object. This feature provides a more convenient and configurable way to manage GitLab connections, especially useful for scenarios requiring connections to multiple GitLab instances or aiming to separate connection configurations from the codebase. Example usage (assuming the configuration file is gitlab.ini): Python gl = GraphQL.from_config(config_files=['gitlab.ini']) Internally, this method utilizes gitlab.config.GitlabConfigParser to handle the parsing of the configuration files.
1 parent d139d60 commit 1957eee

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

gitlab/client.py

+80
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,86 @@ def execute(self, request: str | graphql.Source, *args: Any, **kwargs: Any) -> A
14321432

14331433
return result
14341434

1435+
@classmethod
1436+
def from_config(
1437+
cls,
1438+
gitlab_id: str | None = None,
1439+
config_files: list[str] | None = None,
1440+
**kwargs: Any,
1441+
) -> Gitlab:
1442+
"""Create a Gitlab connection from configuration files.
1443+
1444+
Args:
1445+
gitlab_id: ID of the configuration section.
1446+
config_files list[str]: List of paths to configuration files.
1447+
1448+
kwargs:
1449+
session requests.Session: Custom requests Session
1450+
1451+
Returns:
1452+
A Gitlab connection.
1453+
1454+
Raises:
1455+
gitlab.config.GitlabDataError: If the configuration is not correct.
1456+
"""
1457+
config = gitlab.config.GitlabConfigParser(
1458+
gitlab_id=gitlab_id, config_files=config_files
1459+
)
1460+
return cls(
1461+
config.url,
1462+
private_token=config.private_token,
1463+
oauth_token=config.oauth_token,
1464+
job_token=config.job_token,
1465+
ssl_verify=config.ssl_verify,
1466+
timeout=config.timeout,
1467+
http_username=config.http_username,
1468+
http_password=config.http_password,
1469+
api_version=config.api_version,
1470+
per_page=config.per_page,
1471+
pagination=config.pagination,
1472+
order_by=config.order_by,
1473+
user_agent=config.user_agent,
1474+
retry_transient_errors=config.retry_transient_errors,
1475+
keep_base_url=config.keep_base_url,
1476+
**kwargs,
1477+
)
1478+
1479+
def _set_auth_info(self) -> None:
1480+
tokens = [
1481+
token
1482+
for token in [self.private_token, self.oauth_token, self.job_token]
1483+
if token
1484+
]
1485+
if len(tokens) > 1:
1486+
raise ValueError(
1487+
"Only one of private_token, oauth_token or job_token should "
1488+
"be defined"
1489+
)
1490+
if (self.http_username and not self.http_password) or (
1491+
not self.http_username and self.http_password
1492+
):
1493+
raise ValueError("Both http_username and http_password should be defined")
1494+
if tokens and self.http_username:
1495+
raise ValueError(
1496+
"Only one of token authentications or http "
1497+
"authentication should be defined"
1498+
)
1499+
1500+
self._auth: requests.auth.AuthBase | None = None
1501+
if self.private_token:
1502+
self._auth = _backends.PrivateTokenAuth(self.private_token)
1503+
1504+
if self.oauth_token:
1505+
self._auth = _backends.OAuthTokenAuth(self.oauth_token)
1506+
1507+
if self.job_token:
1508+
self._auth = _backends.JobTokenAuth(self.job_token)
1509+
1510+
if self.http_username and self.http_password:
1511+
self._auth = requests.auth.HTTPBasicAuth(
1512+
self.http_username, self.http_password
1513+
)
1514+
14351515

14361516
class AsyncGraphQL(_BaseGraphQL):
14371517
def __init__(

0 commit comments

Comments
 (0)