Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

WIP: migrate to asyncio and httpx#998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
L0ric0 wants to merge9 commits intopython-gitlab:mainfromL0ric0:master
Closed
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 70 additions & 41 deletionsgitlab/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,11 +22,22 @@
import time
import warnings

import requests
import httpx
import asyncio

import gitlab.config
from gitlab.const import * # noqa
from gitlab.exceptions import * # noqa
from gitlab.exceptions import (
on_http_error,
GitlabVerifyError,
GitlabMarkdownError,
GitlabLicenseError,
RedirectError,
GitlabHttpError,
GitlabAuthenticationError,
GitlabParsingError,
GitlabSearchError,
)
from gitlab import utils # noqa

__title__ = "python-gitlab"
Expand DownExpand Up@@ -82,7 +93,7 @@ def __init__(
http_password=None,
timeout=None,
api_version="4",
session=None,
client=None,
per_page=None,
):

Expand All@@ -105,8 +116,8 @@ def __init__(
self.job_token = job_token
self._set_auth_info()

#: Create asession object for requests
self.session =session orrequests.Session()
#: Create aclient object for requests
self.client =client orhttpx.AsyncClient()

self.per_page = per_page

Expand DownExpand Up@@ -144,7 +155,8 @@ def __enter__(self):
return self

def __exit__(self, *args):
self.session.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(self.client.aclose())

def __getstate__(self):
state = self.__dict__.copy()
Expand DownExpand Up@@ -211,7 +223,7 @@ def auth(self):
"""
self.user = self._objects.CurrentUserManager(self).get()

def version(self):
asyncdef version(self):
"""Returns the version and revision of the gitlab server.

Note that self.version and self.revision will be set on the gitlab
Expand All@@ -224,7 +236,7 @@ def version(self):
"""
if self._server_version is None:
try:
data = self.http_get("/version")
data =awaitself.http_get("/version")
self._server_version = data["version"]
self._server_revision = data["revision"]
except Exception:
Expand All@@ -233,7 +245,7 @@ def version(self):
return self._server_version, self._server_revision

@on_http_error(GitlabVerifyError)
def lint(self, content, **kwargs):
asyncdef lint(self, content, **kwargs):
"""Validate a gitlab CI configuration.

Args:
Expand All@@ -249,11 +261,11 @@ def lint(self, content, **kwargs):
otherwise
"""
post_data = {"content": content}
data = self.http_post("/ci/lint", post_data=post_data, **kwargs)
data =awaitself.http_post("/ci/lint", post_data=post_data, **kwargs)
return (data["status"] == "valid", data["errors"])

@on_http_error(GitlabMarkdownError)
def markdown(self, text, gfm=False, project=None, **kwargs):
asyncdef markdown(self, text, gfm=False, project=None, **kwargs):
"""Render an arbitrary Markdown document.

Args:
Expand All@@ -274,11 +286,11 @@ def markdown(self, text, gfm=False, project=None, **kwargs):
post_data = {"text": text, "gfm": gfm}
if project is not None:
post_data["project"] = project
data = self.http_post("/markdown", post_data=post_data, **kwargs)
data =awaitself.http_post("/markdown", post_data=post_data, **kwargs)
return data["html"]

@on_http_error(GitlabLicenseError)
def get_license(self, **kwargs):
asyncdef get_license(self, **kwargs):
"""Retrieve information about the current license.

Args:
Expand All@@ -291,10 +303,10 @@ def get_license(self, **kwargs):
Returns:
dict: The current license information
"""
return self.http_get("/license", **kwargs)
returnawaitself.http_get("/license", **kwargs)

@on_http_error(GitlabLicenseError)
def set_license(self, license, **kwargs):
asyncdef set_license(self, license, **kwargs):
"""Add a new license.

Args:
Expand All@@ -309,7 +321,7 @@ def set_license(self, license, **kwargs):
dict: The new license information
"""
data = {"license": license}
return self.http_post("/license", post_data=data, **kwargs)
returnawaitself.http_post("/license", post_data=data, **kwargs)

def _construct_url(self, id_, obj, parameters, action=None):
if "next_url" in parameters:
Expand DownExpand Up@@ -369,7 +381,7 @@ def _set_auth_info(self):
self.headers["JOB-TOKEN"] = self.job_token

if self.http_username:
self._http_auth =requests.auth.HTTPBasicAuth(
self._http_auth =httpx.auth.HTTPBasicAuth(
self.http_username, self.http_password
)

Expand DownExpand Up@@ -436,7 +448,7 @@ def _check_redirects(self, result):
if location and location.startswith("https://"):
raise RedirectError(REDIRECT_MSG)

def http_request(
asyncdef http_request(
self,
verb,
path,
Expand DownExpand Up@@ -508,12 +520,12 @@ def http_request(
# The Requests behavior is right but it seems that web servers don't
# always agree with this decision (this is the case with a default
# gitlab installation)
req =requests.Request(
req =httpx.Request(
verb, url, json=json, data=data, params=params, files=files, **opts
)
prepped = self.session.prepare_request(req)
prepped = self.client.prepare_request(req)
prepped.url = utils.sanitized_url(prepped.url)
settings = self.session.merge_environment_settings(
settings = self.client.merge_environment_settings(
prepped.url, {}, streamed, verify, None
)

Expand All@@ -527,7 +539,7 @@ def http_request(
cur_retries = 0

while True:
result = self.session.send(prepped, timeout=timeout, **settings)
result =awaitself.client.send(prepped, timeout=timeout, **settings)

self._check_redirects(result)

Expand DownExpand Up@@ -567,7 +579,9 @@ def http_request(
response_body=result.content,
)

def http_get(self, path, query_data=None, streamed=False, raw=False, **kwargs):
async def http_get(
self, path, query_data=None, streamed=False, raw=False, **kwargs
):
"""Make a GET request to the Gitlab server.

Args:
Expand All@@ -588,7 +602,7 @@ def http_get(self, path, query_data=None, streamed=False, raw=False, **kwargs):
GitlabParsingError: If the json data could not be parsed
"""
query_data = query_data or {}
result = self.http_request(
result =awaitself.http_request(
"get", path, query_data=query_data, streamed=streamed, **kwargs
)

Expand All@@ -606,7 +620,7 @@ def http_get(self, path, query_data=None, streamed=False, raw=False, **kwargs):
else:
return result

def http_list(self, path, query_data=None, as_list=None, **kwargs):
asyncdef http_list(self, path, query_data=None, as_list=None, **kwargs):
"""Make a GET request to the Gitlab server for list-oriented queries.

Args:
Expand DownExpand Up@@ -636,16 +650,22 @@ def http_list(self, path, query_data=None, as_list=None, **kwargs):
url = self._build_url(path)

if get_all is True and as_list is True:
return list(GitlabList(self, url, query_data, **kwargs))
return list(awaitGitlabList(self, url, query_data, **kwargs).__setup__())

if "page" in kwargs or as_list is True:
# pagination requested, we return a list
return list(GitlabList(self, url, query_data, get_next=False, **kwargs))
return list(
await GitlabList(
self, url, query_data, get_next=False, **kwargs
).__setup__()
)

# No pagination, generator requested
return GitlabList(self, url, query_data, **kwargs)
returnawaitGitlabList(self, url, query_data, **kwargs).__setup__()

def http_post(self, path, query_data=None, post_data=None, files=None, **kwargs):
async def http_post(
self, path, query_data=None, post_data=None, files=None, **kwargs
):
"""Make a POST request to the Gitlab server.

Args:
Expand All@@ -668,7 +688,7 @@ def http_post(self, path, query_data=None, post_data=None, files=None, **kwargs)
query_data = query_data or {}
post_data = post_data or {}

result = self.http_request(
result =awaitself.http_request(
"post",
path,
query_data=query_data,
Expand All@@ -683,7 +703,9 @@ def http_post(self, path, query_data=None, post_data=None, files=None, **kwargs)
raise GitlabParsingError(error_message="Failed to parse the server message")
return result

def http_put(self, path, query_data=None, post_data=None, files=None, **kwargs):
async def http_put(
self, path, query_data=None, post_data=None, files=None, **kwargs
):
"""Make a PUT request to the Gitlab server.

Args:
Expand All@@ -705,7 +727,7 @@ def http_put(self, path, query_data=None, post_data=None, files=None, **kwargs):
query_data = query_data or {}
post_data = post_data or {}

result = self.http_request(
result =awaitself.http_request(
"put",
path,
query_data=query_data,
Expand All@@ -718,7 +740,7 @@ def http_put(self, path, query_data=None, post_data=None, files=None, **kwargs):
except Exception:
raise GitlabParsingError(error_message="Failed to parse the server message")

def http_delete(self, path, **kwargs):
asyncdef http_delete(self, path, **kwargs):
"""Make a PUT request to the Gitlab server.

Args:
Expand All@@ -732,10 +754,10 @@ def http_delete(self, path, **kwargs):
Raises:
GitlabHttpError: When the return code is not 2xx
"""
return self.http_request("delete", path, **kwargs)
returnawaitself.http_request("delete", path, **kwargs)

@on_http_error(GitlabSearchError)
def search(self, scope, search, **kwargs):
asyncdef search(self, scope, search, **kwargs):
"""Search GitLab resources matching the provided string.'

Args:
Expand DownExpand Up@@ -763,12 +785,19 @@ class GitlabList(object):

def __init__(self, gl, url, query_data, get_next=True, **kwargs):
self._gl = gl
self._query(url, query_data, **kwargs)
self.url = url
self.query_data = query_data
self.kwargs = kwargs
self._get_next = get_next

def _query(self, url, query_data=None, **kwargs):
async def __setup__(self):
await self._query(self.url, self.query_data, **self.kwargs)

async def _query(self, url, query_data=None, **kwargs):
query_data = query_data or {}
result = self._gl.http_request("get", url, query_data=query_data, **kwargs)
result = await self._gl.http_request(
"get", url, query_data=query_data, **kwargs
)
try:
self._next_url = result.links["next"]["url"]
except KeyError:
Expand DownExpand Up@@ -829,10 +858,10 @@ def __iter__(self):
def __len__(self):
return int(self._total)

def __next__(self):
asyncdef __next__(self):
return self.next()

def next(self):
asyncdef next(self):
try:
item = self._data[self._current]
self._current += 1
Expand All@@ -841,7 +870,7 @@ def next(self):
pass

if self._next_url and self._get_next is True:
self._query(self._next_url)
awaitself._query(self._next_url)
return self.next()

raise StopIteration
Loading

[8]ページ先頭

©2009-2025 Movatter.jp