首先,我对 gerrit 的理解有限。
我正在尝试使用 python 访问 Gerrit Rest API,但无法这样做。我想获取与帐户相关的所有信息(提交、评论)。
from requests.auth import HTTPDigestAuth
from pygerrit2 import GerritRestAPI, HTTPBasicAuth
auth = HTTPBasicAuth('user', 'password')
from pygerrit2 import GerritRestAPI
rest = GerritRestAPI(url='https://xxx.xx.xxx.xxx.com/', auth = auth)
changes = rest.get("changes/?q=is:open&q=is:close&q=all&o=DETAILED_ACCOUNTS&o=ALL_REVISIONS&o=ALL_COMMITS&o=ALL_FILES&o=MESSAGES", headers={'Content-Type': 'application/json'})
Run Code Online (Sandbox Code Playgroud)
我收到的错误是:
ConnectionError: HTTPSConnectionPool(host='xxx.xx.xxx.xxx.com', port=443): Max retries exceeded with url: /login/a/changes/?q=is:open&q=is:close&q=all&o=DETAILED_ACCOUNTS&o=ALL_REVISIONS&o=ALL_COMMITS&o=ALL_FILES&o=MESSAGES (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x825bad0>: Failed to establish a new connection: [Errno 110] Connection timed out',))
Run Code Online (Sandbox Code Playgroud)
如果我将查询复制粘贴到 url 中,我就可以获取信息,但不能通过 python 获取。怎么做?如果问题不清楚,请评论/编辑。谢谢
您遇到来自 requests 库的连接错误(pygerrit2依赖于 requests) - 发生这种情况是因为您的连接超时。为了避免这种情况,我建议使用像backoff这样的库。Backoff 将捕获此连接错误并重试建立连接。这可以通过装饰器和导入轻松完成。
from requests.auth import HTTPDigestAuth
from pygerrit2 import GerritRestAPI, HTTPBasicAuth
import backoff
import requests
@backoff.on_exception(backoff.expo,
requests.exceptions.ConnectionError,
max_time=10)
def makeGerritAPICall():
auth = HTTPBasicAuth('user', 'password')
rest = GerritRestAPI(url='https://xxx.xx.xxx.xxx.com/', auth = auth)
changes = rest.get("changes/?q=is:open&q=is:close&q=all&o=DETAILED_ACCOUNTS&o=ALL_REVISIONS&o=ALL_COMMITS&o=ALL_FILES&o=MESSAGES", headers={'Content-Type': 'application/json'})
Run Code Online (Sandbox Code Playgroud)
以下函数将在 10 秒内重试遇到 ConnectionError 的任何请求,然后失败并引发 ConnectionError。
我建议访问 backoff git README 文档,因为它们包含大量有关 backoff 的有用信息。
| 归档时间: |
|
| 查看次数: |
5863 次 |
| 最近记录: |