Skip to content

Commit

Permalink
fix(v2client): handles 5xx server errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain authored and ankitrgadiya committed May 16, 2023
1 parent a80f2e6 commit b7cf0cf
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion riocli/v2client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand Down

0 comments on commit b7cf0cf

Please sign in to comment.