Пример #1
0
    def _request_simple(self,
                        endpoint: EndpointTypes,
                        relative_uri: str,
                        params: Dict[str, str] = None,
                        headers: Dict[str, str] = None,
                        req_type: CurlRequestType = CurlRequestType.GET,
                        data: str or dict = None) -> CURLResponse or None:

        if endpoint == EndpointTypes.identity:
            _endpoint: str = f"{self.__login_api}"
        else:
            _endpoint: str = self.__endpoints.get_endpoint(endpoint)

        url = f"{_endpoint}{relative_uri}"

        _t_start = 0
        if self.__debug:
            _t_start = time.time_ns()

        r = None
        try:
            return curl(url,
                        req_type=req_type,
                        params=params,
                        headers=headers,
                        data=data)
        except TimeoutError:
            self.__last_errors.append("Timeout exception on API request")
            return None
        finally:
            if self.__debug:
                from openstack_cli.core.output import Console
                _t_delta = time.time_ns() - _t_start
                _t_sec = _t_delta / 1000000000
                _params = ",".join([f"{k}={v}" for k, v in params.items()
                                    ]) if params else "None"
                _f_caller = self.__get_origin_frame(
                    self._request_simple.__name__)

                _chunks = [
                    f"[{_t_sec:.2f}s]", f"[{req_type.value}]",
                    f"[{endpoint.value}]", f" {relative_uri}; ",
                    str(Colors.RESET),
                    f"{Colors.BRIGHT_BLACK}{os.path.basename(_f_caller[0].filename)}{Colors.RESET}: ",
                    f"{Colors.BRIGHT_BLACK}->{Colors.RESET}".join(
                        [f"{f.function}:{f.lineno}" for f in _f_caller])
                ]
                Console.print_debug("".join(_chunks))
Пример #2
0
def get_asset(ver: ConfProperties) -> Tuple[GHRelease, GHAsset]:
    r = curl(ver.update_src)
    if r.code not in [200, 201]:
        return None, None

    releases = sorted([GHRelease(serialized_obj=i) for i in r.from_json()],
                      key=lambda x: x.version,
                      reverse=True)
    release = releases[0] if releases else None

    if not release or release.version <= ver.version:
        return None, None

    for _asset in release.assets:
        if ".whl" in _asset.name and _asset.state == "uploaded":
            return release, _asset
Пример #3
0
    def _request(self,
                 endpoint: EndpointTypes,
                 relative_uri: str,
                 params: Dict[str, str] = None,
                 req_type: CurlRequestType = CurlRequestType.GET,
                 is_json: bool = False,
                 page_collection_name: str = None,
                 data: str or dict = None) -> str or dict or None:

        if not self.__is_auth and not self.login():
            raise RuntimeError("Not Authorised")

        _endpoint = self.__login_api if endpoint == EndpointTypes.identity else self.__endpoints.get_endpoint(
            endpoint)

        _t_start = 0
        if self.__debug:
            _t_start = time.time_ns()

        url = f"{_endpoint}{relative_uri}"
        headers = {"X-Auth-Token": self._conf.auth_token}

        r = None
        try:
            r = curl(url,
                     req_type=req_type,
                     params=params,
                     headers=headers,
                     data=data)
        except TimeoutError:
            self.__last_errors.append("Timeout exception on API request")
            return
        finally:
            if self.__debug:
                from openstack_cli.core.output import Console
                _t_delta = time.time_ns() - _t_start
                _t_sec = _t_delta / 1000000000
                _params = ",".join([f"{k}={v}" for k, v in params.items()
                                    ]) if params else "None"
                _f_caller = self.__get_origin_frame(self._request.__name__)

                _chunks = [
                    f"[{_t_sec:.2f}s]", f"[{req_type.value}]",
                    f"[{endpoint.value}]", f" {relative_uri}; ",
                    str(Colors.RESET),
                    f"{Colors.BRIGHT_BLACK}{os.path.basename(_f_caller[0].filename)}{Colors.RESET}: ",
                    f"{Colors.BRIGHT_BLACK}->{Colors.RESET}".join(
                        [f"{f.function}:{f.lineno}" for f in _f_caller])
                ]
                Console.print_debug("".join(_chunks))

        if r.code not in [200, 201, 202, 204]:
            # if not data:
            #   return None
            raise JSONValueError(r.content)

        if r.code in [204]:
            return ""

        content = r.from_json() if is_json else r.content

        if is_json and page_collection_name and isinstance(content, dict):
            if "next" in content and content["next"]:
                uri, _, args = content["next"].partition("?")
            elif "links" in content and "next" in content["links"] and content[
                    "links"]["next"]:
                uri, _, args = content["links"]["next"].partition("?")
            else:
                return content

            params = dict([i.split("=") for i in args.split("&")])
            next_page = self._request(
                endpoint,
                uri,
                params=params,
                req_type=req_type,
                is_json=is_json,
                page_collection_name=page_collection_name)
            content[page_collection_name].extend(
                next_page[page_collection_name])

        return content