|
23 | 23 | from messagebird.voice_recording import VoiceRecordingsList, VoiceRecording |
24 | 24 | from messagebird.voice_transcription import VoiceTranscriptionsList, VoiceTranscriptionsView |
25 | 25 | from messagebird.call_flow import CallFlow, CallFlowList, CallFlowNumberList |
| 26 | +from messagebird.number import Number, NumberList |
26 | 27 |
|
27 | 28 | ENDPOINT = 'https://rest.messagebird.com' |
28 | 29 | CLIENT_VERSION = '1.4.1' |
|
46 | 47 | VOICE_TRANSCRIPTIONS_PATH = 'transcriptions' |
47 | 48 | VOICE_WEB_HOOKS_PATH = 'webhooks' |
48 | 49 |
|
| 50 | +NUMBER_TYPE = 'number' |
| 51 | +NUMBER_API_ROOT = 'https://numbers.messagebird.com/v1/' |
| 52 | +NUMBER_PATH = 'phone-numbers' |
| 53 | +NUMBER_AVAILABLE_PATH = 'available-phone-numbers' |
| 54 | + |
49 | 55 |
|
50 | 56 | class ErrorException(Exception): |
51 | 57 | def __init__(self, errors): |
@@ -81,6 +87,9 @@ def _get_http_client(self, type=REST_TYPE): |
81 | 87 | if type == VOICE_TYPE: |
82 | 88 | return HttpClient(VOICE_API_ROOT, self.access_key, USER_AGENT) |
83 | 89 |
|
| 90 | + if type == NUMBER_TYPE: |
| 91 | + return HttpClient(NUMBER_API_ROOT, self.access_key, USER_AGENT) |
| 92 | + |
84 | 93 | return HttpClient(ENDPOINT, self.access_key, USER_AGENT) |
85 | 94 |
|
86 | 95 | def request(self, path, method='GET', params=None, type=REST_TYPE): |
@@ -498,6 +507,31 @@ def call_flow_numbers_add(self, call_flow_id, numbers=()): |
498 | 507 | def _format_query(self, limit, offset): |
499 | 508 | return 'limit=' + str(limit) + '&offset=' + str(offset) |
500 | 509 |
|
| 510 | + def available_numbers_list(self, country, params={}, limit=20, offset=0): |
| 511 | + """Retrieve a list of phone numbers available for purchase.""" |
| 512 | + params['limit'] = limit |
| 513 | + params['offset'] = offset |
| 514 | + return NumberList().load(self.request(NUMBER_AVAILABLE_PATH + '/' + str(country), 'GET', params, NUMBER_TYPE)) |
| 515 | + |
| 516 | + def purchase_number(self, number, country, billingIntervalMonths=1): |
| 517 | + params = {'number': str(number), 'countryCode': str(country), 'billingIntervalMonths': int(billingIntervalMonths)} |
| 518 | + return Number().load(self.request(NUMBER_PATH, 'POST', params, NUMBER_TYPE)) |
| 519 | + |
| 520 | + def update_number(self, number, tags): |
| 521 | + params = {'tags': tags} |
| 522 | + return Number().load(self.request(NUMBER_PATH + '/' + str(number), 'PATCH', params, NUMBER_TYPE)) |
| 523 | + |
| 524 | + def delete_number(self, number): |
| 525 | + self.request(NUMBER_PATH + '/' + str(number), 'DELETE', None, NUMBER_TYPE) |
| 526 | + |
| 527 | + def purchased_numbers_list(self, params={}, limit=20, offset=0): |
| 528 | + params['limit'] = limit |
| 529 | + params['offset'] = offset |
| 530 | + return NumberList().load(self.request(NUMBER_PATH, 'GET', params, NUMBER_TYPE)) |
| 531 | + |
| 532 | + def purchased_number(self, number): |
| 533 | + return Number().load(self.request(NUMBER_PATH + '/' + number, 'GET', None, NUMBER_TYPE)) |
| 534 | + |
501 | 535 | @staticmethod |
502 | 536 | def generate_voice_calls_url(call_id=None, leg_id=None, recording_id=None): |
503 | 537 | uri = VOICE_API_ROOT + '/' + VOICE_PATH + '/' |
|
0 commit comments