Skip to content

Commit 784cd59

Browse files
committed
Add GeoPermissions exception on synchronous geo permissions error from server
1 parent b43aa04 commit 784cd59

File tree

6 files changed

+28
-6
lines changed

6 files changed

+28
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Change Log
2+
## [4.58.5](https://github.com/plivo/plivo-python/tree/v4.58.5) (2025-02-18)
3+
**Feature - Throw GeoPermissionError on synchronous geopermissions error**
24

35
## [4.58.4](https://github.com/plivo/plivo-python/tree/v4.58.4) (2025-01-27)
46
- Role param validation enhancement in Multiparty Add Participant API and XML creation

plivo/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ class ValidationError(PlivoRestError):
2929

3030
class ForbiddenError(PlivoRestError):
3131
pass
32+
33+
class GeoPermissionError(PlivoRestError):
34+
pass

plivo/rest/base_client.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from plivo.base import ResponseObject
1111
from plivo.exceptions import (AuthenticationError, InvalidRequestError,
12-
PlivoRestError, PlivoServerError,
12+
PlivoRestError, PlivoServerError, GeoPermissionError,
1313
ResourceNotFoundError, ValidationError)
1414
from plivo.utils import is_valid_mainaccount, is_valid_subaccount
1515
from plivo.version import __version__
@@ -21,6 +21,8 @@
2121
PLIVO_API = 'https://api.plivo.com'
2222
PLIVO_API_BASE_URI = '/'.join([PLIVO_API, 'v1/Account'])
2323

24+
GEO_PERMISSION_ENDPOINTS = ['/Message/', '/Call/', '/Session/']
25+
2426

2527
def get_user_agent():
2628
return 'plivo-python/%s (Python: %s)' % (__version__,
@@ -114,6 +116,16 @@ def process_response(self,
114116
raise AuthenticationError(
115117
'Failed to authenticate while accessing resource at: '
116118
'{url}'.format(url=response.url))
119+
120+
if \
121+
response.status_code == 403 and \
122+
method == "POST" and \
123+
any(response.url.endswith(endpoint) for endpoint in GEO_PERMISSION_ENDPOINTS):
124+
if response_json and 'error' in response_json:
125+
raise GeoPermissionError(response_json.error)
126+
raise GeoPermissionError(
127+
'Request Failed : '
128+
'{url}'.format(url=response.url))
117129

118130
if response.status_code == 404:
119131
if response_json and 'error' in response_json:

plivo/rest/client.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from plivo.base import ResponseObject
1111
from plivo.exceptions import (AuthenticationError, InvalidRequestError,
12-
PlivoRestError, PlivoServerError,
12+
PlivoRestError, PlivoServerError, GeoPermissionError,
1313
ResourceNotFoundError, ValidationError, ForbiddenError)
1414
from plivo.resources import (Accounts, Addresses, Applications, Calls, Token,
1515
Conferences, Endpoints, Identities,
@@ -44,6 +44,8 @@
4444

4545
CALLINSIGHTS_BASE_URL = 'https://stats.plivo.com'
4646

47+
GEO_PERMISSION_ENDPOINTS = ['/Message/', '/Call/', '/Session/']
48+
4749

4850
def get_user_agent():
4951
return 'plivo-python/%s (Python: %s)' % (__version__,
@@ -172,9 +174,12 @@ def process_response(self,
172174
'{url}'.format(url=response.url))
173175

174176
if response.status_code == 403:
177+
error = ForbiddenError
178+
if method == "POST" and any(response.url.endswith(endpoint) for endpoint in GEO_PERMISSION_ENDPOINTS):
179+
error = GeoPermissionError
175180
if response_json and 'error' in response_json:
176-
raise ForbiddenError(response_json.error)
177-
raise ForbiddenError(
181+
raise error(response_json.error)
182+
raise error(
178183
'Request Failed : '
179184
'{url}'.format(url=response.url))
180185

plivo/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# -*- coding: utf-8 -*-
2-
__version__ = '4.58.4'
2+
__version__ = '4.58.5'

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setup(
1212
name='plivo',
13-
version='4.58.4',
13+
version='4.58.5',
1414
description='A Python SDK to make voice calls & send SMS using Plivo and to generate Plivo XML',
1515
long_description=long_description,
1616
url='https://github.com/plivo/plivo-python',

0 commit comments

Comments
 (0)