def GetArtifactURL(self, account_id, build_id, target, artifact_name, branch, internal, method=GET): """Get the URL for an artifact on the PAB server, using buildsvc. Args: account_id: int, ID associated with the PAB account. build_id: string/int, id of the build. target: string, "latest" or a specific version. artifact_name: string, simple file name (no parent dir or path). branch: string, branch to pull resource from. internal: int, whether the request is for an internal build artifact method: 'GET' or 'POST', which endpoint to query Returns: string, The URL for the resource specified by the parameters """ if method == POST: params = { "1": str(build_id), "2": target, "3": artifact_name, "4": branch, "5": "", # release_candidate_name "6": internal } result = self.CallBuildsvc(method='downloadBuildArtifact', params=params, account_id=account_id) # in downloadBuildArtifact response, index '1' contains the url if self.DOWNLOAD_URL_KEY in result: return result[self.DOWNLOAD_URL_KEY] if len(result) == 0: raise ValueError("Resource not found -- %s" % params) elif method == GET: headers = {} self._credentials.apply(headers) action = 'get-internal' if internal else 'get' get_url = path_urljoin(self.BASE_URL, 'build', 'builds', action, branch, target, build_id, artifact_name) + '?a=' + str(account_id) response = requests.get(get_url, headers=headers) try: responseJSON = response.json() return responseJSON['url'] except ValueError: raise ValueError("Backend error -- check your account ID")
def urljoin(site, path): # FIXME: Hide unusual imports from urllib.parse import urljoin as abs_urljoin from posixpath import join as path_urljoin if isinstance(path, str): segments = [s for s in path.split("/") if s] else: segments = path # assume list or tuple return abs_urljoin(site, path_urljoin(urlparse(site).path, *segments))
def GetBuildList(self, account_id, branch, target, page_token="", max_results=10, internal=True, method=GET): """Get the list of builds for a given account, branch and target Args: account_id: int, ID associated with the PAB account. branch: string, branch to pull resource from. target: string, "latest" or a specific version. page_token: string, token used for pagination max_results: maximum build results the build list contains, e.g. 25 internal: bool, whether to query internal build method: 'GET' or 'POST', which endpoint to query Returns: list of dicts representing the builds, descending in time """ if method == POST: params = { "1": branch, "2": target, "3": page_token, "4": max_results, "7": int(internal) } result = self.CallBuildsvc("listBuild", params, account_id) # in listBuild response, index '1' contains builds if self.LISTBUILD_BUILD_KEY in result: return result[self.LISTBUILD_BUILD_KEY] raise ValueError("Build list not found -- %s" % params) elif method == GET: headers = {} self._credentials.apply(headers) action = 'list-internal' if internal else 'list' # PAB URL format expects something (anything) to be given as buildid # and resource, even for action list dummy = 'DUMMY' url = path_urljoin(self.BASE_URL, 'build', 'builds', action, branch, target, dummy, dummy) + '?a=' + str(account_id) response = requests.get(url, headers=headers) try: responseJSON = response.json() return responseJSON['build'] except ValueError as e: logging.exception(e) raise ValueError("Backend error -- check your account ID")
def __request(self, method, url, params=None, payload=None): if method in ['POST', 'PUT', 'PATCH']: self.headers.update({'Content-type': 'application/json'}) r = requests.request( method, path_urljoin(self.base_url, url), #os.path.join(self.base_url, url), #self.base_url+"/"+url, params=params, data=payload, headers=self.headers) if r.status_code == requests.codes.all_okay: return r.json(object_pairs_hook=OrderedDict) else: try: message = None r.raise_for_status() except requests.exceptions.HTTPError as e: message = e.args[0] #error descrition return {'error': dict(code=r.status_code, message=message)}
def get(self, table_name, record_id=None, limit=0, offset=None, filter_by_formula=None, view=None): params = {} if check_string(record_id): url = path_urljoin(table_name, record_id) #url = os.path.join(table_name, record_id) #url = table_name+"/"+record_id else: url = table_name if limit and check_integer(limit): params.update({'pageSize': limit}) if offset and check_string(offset): params.update({'offset': offset}) if filter_by_formula is not None: params.update({'filterByFormula': filter_by_formula}) if view is not None: params.update({'view': view}) return self.__request('GET', url, params)
def GetBuildList(self, account_id, branch, target, page_token="", max_results=10, internal=True, method=GET, verify_signed=False): """Get the list of builds for a given account, branch and target Args: account_id: int, ID associated with the PAB account. branch: string, branch to pull resource from. target: string, "latest" or a specific version. page_token: string, token used for pagination max_results: maximum build results the build list contains, e.g. 25 internal: bool, whether to query internal build method: 'GET' or 'POST', which endpoint to query verify_signed: bool, whether to verify signed build. Returns: list of dicts representing the builds, descending in time """ if method == POST: params = { "1": branch, "2": target, "3": page_token, "4": max_results, "7": int(internal) } result = self.CallBuildsvc("listBuild", params, account_id) # in listBuild response, index '1' contains builds if self.LISTBUILD_BUILD_KEY in result: return result[self.LISTBUILD_BUILD_KEY] if verify_signed: logging.error("verify_signed does not support POST method.") raise ValueError("Build list not found -- %s" % params) elif method == GET: headers = {} self._credentials.apply(headers) action = 'list-internal' if internal else 'list' # PAB URL format expects something (anything) to be given as buildid # and resource, even for action list dummy = 'DUMMY' url = path_urljoin(self.BASE_URL, 'build', 'builds', action, branch, target, dummy, dummy) + '?a=' + str(account_id) response = requests.get(url, headers=headers) try: responseJSON = response.json() builds = responseJSON['build'] except ValueError as e: logging.exception(e) raise ValueError("Backend error -- check your account ID") if verify_signed: for build in builds: artifact_name = "signed%2Fsigned-{}-img-{}.zip".format( target.split("-")[0], build["build_id"]) logging.debug("Checking whether the build is signed for " "build_target {} and build_id {}".format( target, build["build_id"])) signed_build_url = self.GetArtifactURL( account_id=account_id, build_id=build["build_id"], target=target, artifact_name=artifact_name, branch=branch, internal=False, method=method) try: self.GetResponseWithURL(signed_build_url) logging.debug("The build is signed.") build["signed"] = True except requests.HTTPError: logging.debug("The build is not signed.") build["signed"] = False return builds
def __init__(self, base_id, api_key): self.airtable_url = API_URL % API_VERSION self.base_url = path_urljoin(self.airtable_url, base_id) #self.base_url = os.path.join(self.airtable_url, base_id) self.headers = {'Authorization': 'Bearer %s' % api_key}
def delete(self, table_name, record_id): if check_string(table_name) and check_string(record_id): url = path_urljoin(table_name, record_id) #url = os.path.join(table_name, record_id) return self.__request('DELETE', url)
def update_all(self, table_name, record_id, data): if check_string(table_name) and check_string(record_id): url = path_urljoin(table_name, record_id) #url = os.path.join(table_name, record_id) payload = create_payload(data) return self.__request('PUT', url, payload=json.dumps(payload))