From 784cd59b52af95958b2f49cee5079ab8fa93a55f Mon Sep 17 00:00:00 2001 From: mohsin-plivo Date: Wed, 19 Feb 2025 11:51:33 +0530 Subject: [PATCH] Add GeoPermissions exception on synchronous geo permissions error from server --- CHANGELOG.md | 2 ++ plivo/exceptions.py | 3 +++ plivo/rest/base_client.py | 14 +++++++++++++- plivo/rest/client.py | 11 ++++++++--- plivo/version.py | 2 +- setup.py | 2 +- 6 files changed, 28 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa4a593d..a64ef3e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,6 @@ # Change Log +## [4.58.5](https://github.com/plivo/plivo-python/tree/v4.58.5) (2025-02-18) +**Feature - Throw GeoPermissionError on synchronous geopermissions error** ## [4.58.4](https://github.com/plivo/plivo-python/tree/v4.58.4) (2025-01-27) - Role param validation enhancement in Multiparty Add Participant API and XML creation diff --git a/plivo/exceptions.py b/plivo/exceptions.py index b964aed1..2a728537 100644 --- a/plivo/exceptions.py +++ b/plivo/exceptions.py @@ -29,3 +29,6 @@ class ValidationError(PlivoRestError): class ForbiddenError(PlivoRestError): pass + +class GeoPermissionError(PlivoRestError): + pass diff --git a/plivo/rest/base_client.py b/plivo/rest/base_client.py index 0d5818a5..76e939e1 100644 --- a/plivo/rest/base_client.py +++ b/plivo/rest/base_client.py @@ -9,7 +9,7 @@ from plivo.base import ResponseObject from plivo.exceptions import (AuthenticationError, InvalidRequestError, - PlivoRestError, PlivoServerError, + PlivoRestError, PlivoServerError, GeoPermissionError, ResourceNotFoundError, ValidationError) from plivo.utils import is_valid_mainaccount, is_valid_subaccount from plivo.version import __version__ @@ -21,6 +21,8 @@ PLIVO_API = 'https://api.plivo.com' PLIVO_API_BASE_URI = '/'.join([PLIVO_API, 'v1/Account']) +GEO_PERMISSION_ENDPOINTS = ['/Message/', '/Call/', '/Session/'] + def get_user_agent(): return 'plivo-python/%s (Python: %s)' % (__version__, @@ -114,6 +116,16 @@ def process_response(self, raise AuthenticationError( 'Failed to authenticate while accessing resource at: ' '{url}'.format(url=response.url)) + + if \ + response.status_code == 403 and \ + method == "POST" and \ + any(response.url.endswith(endpoint) for endpoint in GEO_PERMISSION_ENDPOINTS): + if response_json and 'error' in response_json: + raise GeoPermissionError(response_json.error) + raise GeoPermissionError( + 'Request Failed : ' + '{url}'.format(url=response.url)) if response.status_code == 404: if response_json and 'error' in response_json: diff --git a/plivo/rest/client.py b/plivo/rest/client.py index 378056b9..7b79b344 100644 --- a/plivo/rest/client.py +++ b/plivo/rest/client.py @@ -9,7 +9,7 @@ from plivo.base import ResponseObject from plivo.exceptions import (AuthenticationError, InvalidRequestError, - PlivoRestError, PlivoServerError, + PlivoRestError, PlivoServerError, GeoPermissionError, ResourceNotFoundError, ValidationError, ForbiddenError) from plivo.resources import (Accounts, Addresses, Applications, Calls, Token, Conferences, Endpoints, Identities, @@ -44,6 +44,8 @@ CALLINSIGHTS_BASE_URL = 'https://stats.plivo.com' +GEO_PERMISSION_ENDPOINTS = ['/Message/', '/Call/', '/Session/'] + def get_user_agent(): return 'plivo-python/%s (Python: %s)' % (__version__, @@ -172,9 +174,12 @@ def process_response(self, '{url}'.format(url=response.url)) if response.status_code == 403: + error = ForbiddenError + if method == "POST" and any(response.url.endswith(endpoint) for endpoint in GEO_PERMISSION_ENDPOINTS): + error = GeoPermissionError if response_json and 'error' in response_json: - raise ForbiddenError(response_json.error) - raise ForbiddenError( + raise error(response_json.error) + raise error( 'Request Failed : ' '{url}'.format(url=response.url)) diff --git a/plivo/version.py b/plivo/version.py index 6cf34af2..386a7835 100644 --- a/plivo/version.py +++ b/plivo/version.py @@ -1,2 +1,2 @@ # -*- coding: utf-8 -*- -__version__ = '4.58.4' +__version__ = '4.58.5' diff --git a/setup.py b/setup.py index 4242bcc5..655f6de9 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name='plivo', - version='4.58.4', + version='4.58.5', description='A Python SDK to make voice calls & send SMS using Plivo and to generate Plivo XML', long_description=long_description, url='https://github.com/plivo/plivo-python',