Skip to content

Commit 57853a2

Browse files
authored
Merge pull request #143 from plivo/ppai-lookup-api
Add lookup API support
2 parents d8aa94b + a65280e commit 57853a2

File tree

9 files changed

+89
-5
lines changed

9 files changed

+89
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Change Log
22

3+
## [4.12.0](https://github.com/plivo/plivo-python/tree/v4.12.0) (2020-10-06)
4+
- Add lookup API support.
5+
36
## [4.11.0](https://github.com/plivo/plivo-python/tree/v4.11.0) (2020-09-24)
47
- Add "public_uri" optional param support for Application API.
58

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ call_made = client.calls.create(
116116

117117
```
118118

119+
### Lookup a number
120+
121+
```python
122+
import plivo
123+
124+
client = plivo.RestClient(auth_id='', auth_token='')
125+
resp = client.lookup.get("<insert-number-here>", info_type='carrier')
126+
print(resp)
127+
```
128+
119129
### Generate Plivo XML
120130

121131
```python

plivo/resources/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
from .media import Media
1414
from .identities import Identities
1515
from .call_feedback import CallFeedback
16-
from .powerpacks import Powerpacks
16+
from .powerpacks import Powerpacks
17+
from .lookup import Lookup

plivo/resources/lookup.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
from plivo.base import PlivoResourceInterface, ResponseObject
3+
4+
5+
class Number(ResponseObject):
6+
def __init__(self, client, data):
7+
super(Number, self).__init__(data)
8+
9+
10+
class Lookup(PlivoResourceInterface):
11+
_resource_type = Number
12+
13+
def get(self, number, info_type='carrier'):
14+
params = {
15+
'type': info_type,
16+
}
17+
return self.client.request(
18+
'GET',
19+
('Lookup/Number', number),
20+
data=params,
21+
response_type=Number,
22+
plivo_api_v1_base_url=True)

plivo/rest/client.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
PlivoRestError, PlivoServerError,
1313
ResourceNotFoundError, ValidationError)
1414
from plivo.resources import (Accounts, Addresses, Applications, Calls,
15-
Conferences, Endpoints, Identities, Messages, Powerpacks, Media,
15+
Conferences, Endpoints, Identities,
16+
Messages, Powerpacks, Media, Lookup,
1617
Numbers, Pricings, Recordings, Subaccounts, CallFeedback)
1718
from plivo.resources.live_calls import LiveCalls
1819
from plivo.resources.queued_calls import QueuedCalls
@@ -24,6 +25,7 @@
2425
'auth_id auth_token')
2526

2627
PLIVO_API = 'https://api.plivo.com'
28+
PLIVO_API_V1 = '/'.join([PLIVO_API, 'v1'])
2729
PLIVO_API_BASE_URI = '/'.join([PLIVO_API, 'v1/Account'])
2830

2931
# Will change these urls before putting this change in production
@@ -92,6 +94,7 @@ def __init__(self, auth_id=None, auth_token=None, proxies=None, timeout=5):
9294
self.conferences = Conferences(self)
9395
self.endpoints = Endpoints(self)
9496
self.messages = Messages(self)
97+
self.lookup = Lookup(self)
9598
self.numbers = Numbers(self)
9699
self.powerpacks = Powerpacks(self)
97100
self.media = Media(self)
@@ -197,10 +200,17 @@ def process_response(self,
197200
return response_json
198201

199202
def create_request(self, method, path=None, data=None, **kwargs):
200-
203+
# The abstraction created by request() and create_request() is moot
204+
# now since several product-specific handling have been aded.
205+
# Requires a refactor.
201206
if 'is_callinsights_request' in kwargs:
202207
url = '/'.join([CALLINSIGHTS_BASE_URL, kwargs['callinsights_request_path']])
203208
req = Request(method, url, **({'params': data} if method == 'GET' else {'json': data}))
209+
elif kwargs.get('plivo_api_v1_base_url', False):
210+
# currently used by lookup API but can be used by other APIs in future
211+
path = path or []
212+
url = '/'.join([PLIVO_API_V1] + list([str(p) for p in path]))
213+
req = Request(method, url, **({'params': data} if method == 'GET' else {'json': data}))
204214
else:
205215
path = path or []
206216
req = Request(method, '/'.join([self.base_uri, self.session.auth[0]] +
@@ -285,6 +295,9 @@ def request(self,
285295
kwargs["is_voice_request"] = True
286296
return self.request(method, path, data, **kwargs)
287297
return self.process_response(method, response, response_type, objects_type)
298+
elif kwargs.get('plivo_api_v1_base_url', False):
299+
req = self.create_request(method, path, data, plivo_api_v1_base_url=True)
300+
del kwargs['plivo_api_v1_base_url']
288301
else:
289302
req = self.create_request(method, path, data)
290303
session = self.session

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.10.2'
2+
__version__ = '4.12.0'

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.11.0',
13+
version='4.12.0',
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',
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"api_id": "a35a2e7a-fd27-42e6-910c-33382f43240e",
3+
"phone_number": "+14154305555",
4+
"country": {
5+
"name": "United States",
6+
"code_iso2": "US",
7+
"code_iso3": "USA"
8+
},
9+
"format": {
10+
"e164": "+14154305555",
11+
"national": "(415) 430-5555",
12+
"international": "+1 415-430-5555",
13+
"rfc3966": "tel:+1-415-430-5555"
14+
},
15+
"carrier": {
16+
"mobile_country_code": "310",
17+
"mobile_network_code": "150",
18+
"name": "Cingular Wireless",
19+
"type": "mobile",
20+
"ported": true
21+
},
22+
"resource_uri": "/v1/Lookup/Number/+14154305555?type=carrier"
23+
}

tests/resources/test_lookup.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from plivo import exceptions
2+
from tests.base import PlivoResourceTestCase
3+
from tests.decorators import with_response
4+
5+
6+
class LookupTest(PlivoResourceTestCase):
7+
@with_response(200)
8+
def test_get(self):
9+
number = '+14154305555'
10+
resp = self.client.lookup.get(number)
11+
self.assertResponseMatches(resp)
12+
self.assertEqual(self.client.current_request.method, 'GET')

0 commit comments

Comments
 (0)