Firstly, I've a limited gerrit understanding.
I'm trying to access Gerrit Rest API using python, but not able to do so. I want to fetch all the information related to accounts (commits, reviews).
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'})
The error I'm getting is:
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',))
I'm able to fetch the information if I copy paste the query in the url, but not thru python. How to do it? Please comment / edit if question is not clear. Thanks
You are encountering a Connection Error from the requests library(pygerrit2 is dependent on requests) - it is occurring because your connection is timing out. To avoid this I recommend using a library like backoff. Backoff will catch this connection Error and retry establishing the connection. This is done easily with a decorator and an import.
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'})
The following function will retry making any request that encounters a ConnectionError for 10 seconds before failing and raising a ConnectionError.
I would recommend visiting the backoff git README docs as they contain tons of useful info about backoff.