Exemplo n.º 1
0
    def _send(self, action: str, args: dict, **options) -> dict:
        args["Action"] = action
        for handler in self.middleware.request_handlers:
            args = handler(args)
        req = self._build_http_request(args)

        max_retries = options.get("max_retries") or self.config.max_retries
        timeout = options.get("timeout") or self.config.timeout

        resp = self.transport.send(
            req,
            ssl_option=SSLOption(
                self.config.ssl_verify,
                self.config.ssl_cacert,
                self.config.ssl_cert,
                self.config.ssl_key,
            ),
            timeout=timeout,
            max_retries=max_retries,
        ).json()

        for handler in self.middleware.response_handlers:
            resp = handler(resp)

        if int(resp.get("RetCode", -1)) != 0:
            raise exc.RetCodeException(
                action=req.data.get("Action"),
                code=int(resp.get("RetCode")),
                message=resp.get("Message"),
            )

        return resp
Exemplo n.º 2
0
def test_ret_code_error():
    assert not exc.UCloudException().retryable
    code_error = exc.RetCodeException("Foo", 1, "")
    assert str(code_error)
    assert not code_error.retryable
    assert code_error.json() == {"Action": "Foo", "Message": "", "RetCode": 1}
    validate_error = exc.ValidationException(ValueError("invalid type"))
    assert not validate_error.retryable
    assert str(validate_error)
Exemplo n.º 3
0
    def _send(self, action: str, args: dict, max_retries, timeout) -> dict:
        args["Action"] = action

        # inject request middleware
        for handler in self.middleware.request_handlers:
            args = handler(args)

        # send http request
        try:
            req = self._build_http_request(args)

            resp = self.transport.send(
                req,
                ssl_option=SSLOption(
                    self.config.ssl_verify,
                    self.config.ssl_cacert,
                    self.config.ssl_cert,
                    self.config.ssl_key,
                ),
                timeout=timeout,
                max_retries=max_retries,
            )
            data = resp.json()
        except Exception as e:
            for handler in self.middleware.exception_handlers:
                handler(e)
            raise e

        # inject response middleware
        for handler in self.middleware.response_handlers:
            data = handler(data, resp)

        # return when successful
        if int(data.get("RetCode", -1)) == 0:
            return data

        # inject exception middleware
        ret_code_exc = exc.RetCodeException(
            action=req.data.get("Action", ""),
            code=int(data.get("RetCode", 0)),
            message=data.get("Message", ""),
            request_uuid=resp.request_uuid,
        )
        for handler in self.middleware.exception_handlers:
            handler(ret_code_exc)
        raise ret_code_exc
Exemplo n.º 4
0
 def _send(self, action, args, **options):
     args["Action"] = action
     for handler in self.middleware.request_handlers:
         args = handler(args)
     req = self._build_http_request(args)
     max_retries = options.get("max_retries") or self.config.max_retries
     timeout = options.get("timeout") or self.config.timeout
     resp = self.transport.send(req,
                                timeout=timeout,
                                max_retries=max_retries).json()
     for handler in self.middleware.response_handlers:
         resp = handler(resp)
     if int(resp.get("RetCode", -1)) != 0:
         raise exc.RetCodeException(
             action=req.json.get("Action"),
             code=int(resp.get("RetCode")),
             message=resp.get("Message"),
         )
     return resp
Exemplo n.º 5
0
    def _send(self, req: Request, **options: dict) -> dict:
        for handler in self.middleware.request_handlers:
            req = handler(req)

        max_retries = options.get("max_retries") or self.config.max_retries
        timeout = options.get("timeout") or self.config.timeout
        resp = self.transport.send(req,
                                   timeout=timeout,
                                   max_retries=max_retries).json()

        for handler in self.middleware.response_handlers:
            resp = handler(resp)

        if int(resp.get("RetCode", -1)) != 0:
            raise exc.RetCodeException(
                action=req.json.get("Action"),
                code=int(resp.get("RetCode")),
                message=resp.get("Message"),
            )

        return resp