FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(api): Class _BaseGraphQL add parameter and class variable · python-gitlab/python-gitlab@68f0932 · GitHub

Repository navigation

Commit 68f0932

Browse files
committed
feat(api): Class _BaseGraphQL add parameter and class variable
Added parameters to be compatible with parameters required by from_config.
1 parent 4d8d7eb commit 68f0932

1 file changed

Lines changed: 136 additions & 2 deletions

File tree

‎gitlab/client.py‎

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,19 +1538,73 @@ def __init__(
15381538
self,
15391539
url: str | None = None,
15401540
*,
1541-
token: str | None = None,
1541+
private_token: str | None = None,
1542+
private_token: str | None = None,
1543+
oauth_token: str | None = None,
1544+
job_token: str | None = None,
15421545
ssl_verify: bool | str = True,
15431546
client: httpx.AsyncClient | None = None,
1547+
http_username: str | None = None,
1548+
http_password: str | None = None,
15441549
timeout: float | None = None,
1550+
api_version: str = "4",
1551+
per_page: int | None = None,
1552+
pagination: str | None = None,
1553+
order_by: str | None = None,
15451554
user_agent: str = gitlab.const.USER_AGENT,
15461555
fetch_schema_from_transport: bool = False,
15471556
max_retries: int = 100,
15481557
obey_rate_limit: bool = True,
15491558
retry_transient_errors: bool = False,
1559+
keep_base_url: bool = False,
1560+
**kwargs: Any,
15501561
) -> None:
1562+
self._api_version = str(api_version)
1563+
self._server_version: str | None = None
1564+
self._server_revision: str | None = None
1565+
self._base_url = utils.get_base_url(url)
1566+
self._url = f"{self._base_url}/api/v{api_version}"
1567+
#: Timeout to use for requests to gitlab server
1568+
self.timeout = timeout
1569+
self.retry_transient_errors = retry_transient_errors
1570+
self.keep_base_url = keep_base_url
1571+
#: Headers that will be used in request to GitLab
1572+
self.headers = {"User-Agent": user_agent}
1573+
1574+
#: Whether SSL certificates should be validated
1575+
self.ssl_verify = ssl_verify
1576+
1577+
self.private_token = private_token
1578+
self.http_username = http_username
1579+
self.http_password = http_password
1580+
self.oauth_token = oauth_token
1581+
self.job_token = job_token
1582+
self._set_auth_info()
1583+
1584+
#: Create a session object for requests
1585+
_backend: type[_backends.DefaultBackend] = kwargs.pop(
1586+
"backend", _backends.DefaultBackend
1587+
)
1588+
self._backend = _backend(**kwargs)
1589+
self.session = self._backend.client
1590+
1591+
self.per_page = per_page
1592+
self.pagination = pagination
1593+
self.order_by = order_by
1594+
1595+
# We only support v4 API at this time
1596+
if self._api_version not in ("4",):
1597+
raise ModuleNotFoundError(f"gitlab.v{self._api_version}.objects")
1598+
# NOTE: We must delay import of gitlab.v4.objects until now or
1599+
# otherwise it will cause circular import errors
1600+
from gitlab.v4 import objects
1601+
1602+
self._objects = objects
1603+
self.user: objects.CurrentUser | None = None
1604+
15511605
super().__init__(
15521606
url=url,
1553-
token=token,
1607+
private_token=private_token,
15541608
ssl_verify=ssl_verify,
15551609
timeout=timeout,
15561610
user_agent=user_agent,
@@ -1605,3 +1659,83 @@ async def execute(
16051659
)
16061660

16071661
return result
1662+
1663+
@classmethod
1664+
def from_config(
1665+
cls,
1666+
gitlab_id: str | None = None,
1667+
config_files: list[str] | None = None,
1668+
**kwargs: Any,
1669+
) -> Gitlab:
1670+
"""Create a Gitlab connection from configuration files.
1671+
1672+
Args:
1673+
gitlab_id: ID of the configuration section.
1674+
config_files list[str]: List of paths to configuration files.
1675+
1676+
kwargs:
1677+
session requests.Session: Custom requests Session
1678+
1679+
Returns:
1680+
A Gitlab connection.
1681+
1682+
Raises:
1683+
gitlab.config.GitlabDataError: If the configuration is not correct.
1684+
"""
1685+
config = gitlab.config.GitlabConfigParser(
1686+
gitlab_id=gitlab_id, config_files=config_files
1687+
)
1688+
return cls(
1689+
config.url,
1690+
private_token=config.private_token,
1691+
oauth_token=config.oauth_token,
1692+
job_token=config.job_token,
1693+
ssl_verify=config.ssl_verify,
1694+
timeout=config.timeout,
1695+
http_username=config.http_username,
1696+
http_password=config.http_password,
1697+
api_version=config.api_version,
1698+
per_page=config.per_page,
1699+
pagination=config.pagination,
1700+
order_by=config.order_by,
1701+
user_agent=config.user_agent,
1702+
retry_transient_errors=config.retry_transient_errors,
1703+
keep_base_url=config.keep_base_url,
1704+
**kwargs,
1705+
)
1706+
1707+
def _set_auth_info(self) -> None:
1708+
tokens = [
1709+
token
1710+
for token in [self.private_token, self.oauth_token, self.job_token]
1711+
if token
1712+
]
1713+
if len(tokens) > 1:
1714+
raise ValueError(
1715+
"Only one of private_token, oauth_token or job_token should "
1716+
"be defined"
1717+
)
1718+
if (self.http_username and not self.http_password) or (
1719+
not self.http_username and self.http_password
1720+
):
1721+
raise ValueError("Both http_username and http_password should be defined")
1722+
if tokens and self.http_username:
1723+
raise ValueError(
1724+
"Only one of token authentications or http "
1725+
"authentication should be defined"
1726+
)
1727+
1728+
self._auth: requests.auth.AuthBase | None = None
1729+
if self.private_token:
1730+
self._auth = _backends.PrivateTokenAuth(self.private_token)
1731+
1732+
if self.oauth_token:
1733+
self._auth = _backends.OAuthTokenAuth(self.oauth_token)
1734+
1735+
if self.job_token:
1736+
self._auth = _backends.JobTokenAuth(self.job_token)
1737+
1738+
if self.http_username and self.http_password:
1739+
self._auth = requests.auth.HTTPBasicAuth(
1740+
self.http_username, self.http_password
1741+
)

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL