diff --git a/riocli/v2client/client.py b/riocli/v2client/client.py index 392ac9f7..67644766 100644 --- a/riocli/v2client/client.py +++ b/riocli/v2client/client.py @@ -11,10 +11,11 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +import http import json import typing +import requests from munch import munchify, Munch from rapyuta_io.utils.rest_client import HttpMethod, RestClient @@ -29,6 +30,28 @@ def __call__(cls, *args, **kwargs): return cls._instances[cls] +def handle_server_errors(response: requests.Response): + status_code = response.status_code + # 500 Internal Server Error + if status_code == http.HTTPStatus.INTERNAL_SERVER_ERROR: + raise Exception('internal server error') + # 501 Not Implemented + if status_code == http.HTTPStatus.NOT_IMPLEMENTED: + raise Exception('not implemented') + # 502 Bad Gateway + if status_code == http.HTTPStatus.BAD_GATEWAY: + raise Exception('bad gateway') + # 503 Service Unavailable + if status_code == http.HTTPStatus.SERVICE_UNAVAILABLE: + raise Exception('service unavailable') + # 504 Gateway Timeout + if status_code == http.HTTPStatus.GATEWAY_TIMEOUT: + raise Exception('gateway timeout') + # Anything else that is not known + if status_code > 504: + raise Exception('unknown server error') + + class Client(metaclass=_Singleton): """ v2 API Client @@ -98,6 +121,8 @@ def get_project(self, project_guid: str) -> Munch: response = RestClient(url).method( HttpMethod.GET).headers(headers).execute() + handle_server_errors(response) + data = json.loads(response.text) if not response.ok: err_msg = data.get('error') @@ -114,6 +139,8 @@ def create_project(self, spec: dict) -> Munch: response = RestClient(url).method(HttpMethod.POST).headers( headers).execute(payload=spec) + handle_server_errors(response) + data = json.loads(response.text) if not response.ok: err_msg = data.get('error') @@ -130,6 +157,8 @@ def update_project(self, project_guid: str, spec: dict) -> Munch: response = RestClient(url).method(HttpMethod.PUT).headers( headers).execute(payload=spec) + handle_server_errors(response) + data = json.loads(response.text) if not response.ok: err_msg = data.get('error') @@ -146,6 +175,8 @@ def delete_project(self, project_guid: str) -> Munch: response = RestClient(url).method( HttpMethod.DELETE).headers(headers).execute() + handle_server_errors(response) + data = json.loads(response.text) if not response.ok: err_msg = data.get('error') @@ -162,6 +193,9 @@ def list_providers(self) -> typing.List: headers = self._config.get_auth_header() response = RestClient(url).method( HttpMethod.GET).headers(headers).execute() + + handle_server_errors(response) + data = json.loads(response.text) if not response.ok: err_msg = data.get('error') @@ -201,6 +235,9 @@ def get_instance(self, instance_name: str) -> Munch: headers = self._config.get_auth_header() response = RestClient(url).method( HttpMethod.GET).headers(headers).execute() + + handle_server_errors(response) + data = json.loads(response.text) if not response.ok: err_msg = data.get('error') @@ -214,6 +251,9 @@ def create_instance(self, instance: typing.Dict) -> Munch: response = RestClient(url).method(HttpMethod.POST).headers( headers).execute(payload=instance) + + handle_server_errors(response) + data = json.loads(response.text) if not response.ok: err_msg = data.get('error') @@ -226,6 +266,9 @@ def delete_instance(self, instance_name) -> Munch: headers = self._config.get_auth_header() response = RestClient(url).method( HttpMethod.DELETE).headers(headers).execute() + + handle_server_errors(response) + data = json.loads(response.text) if not response.ok: err_msg = data.get('error') @@ -242,6 +285,9 @@ def create_instance_binding(self, instance_name, binding: dict) -> Munch: headers = self._config.get_auth_header() response = RestClient(url).method( HttpMethod.POST).headers(headers).execute(payload=binding) + + handle_server_errors(response) + data = json.loads(response.text) if not response.ok: err_msg = data.get('error') @@ -258,6 +304,9 @@ def get_instance_binding(self, instance_name: str, binding_name: str) -> Munch: headers = self._config.get_auth_header() response = RestClient(url).method( HttpMethod.GET).headers(headers).execute() + + handle_server_errors(response) + data = json.loads(response.text) if not response.ok: err_msg = data.get('error') @@ -274,6 +323,9 @@ def delete_instance_binding(self, instance_name: str, binding_name: str) -> Munc headers = self._config.get_auth_header() response = RestClient(url).method( HttpMethod.DELETE).headers(headers).execute() + + handle_server_errors(response) + data = json.loads(response.text) if not response.ok: err_msg = data.get('error')