|
1 | | -# Getting Started with bandwidth |
2 | | - |
3 | | -Bandwidth's set of APIs |
4 | | - |
5 | | -## Install the Package |
6 | | - |
7 | | -The package is compatible with Python versions ```2 >=2.7.9``` and ```3 >=3.4```. |
8 | | -Install the package from PyPi using the following pip command: |
9 | | - |
10 | | -```python |
11 | | -pip install bandwidth-sdk==5.2.0 |
12 | | -``` |
13 | | - |
14 | | -You can also view the package at: |
15 | | -https://pypi.python.org/pypi/bandwidth-sdk |
16 | | - |
17 | | -## Initialize the API Client |
18 | | - |
19 | | -The following parameters are configurable for the API Client. |
20 | | - |
21 | | -| Parameter | Type | Description | |
22 | | -| --- | --- | --- | |
23 | | -| `messaging_basic_auth_user_name` | `string` | The username to use with basic authentication | |
24 | | -| `messaging_basic_auth_password` | `string` | The password to use with basic authentication | |
25 | | -| `voice_basic_auth_user_name` | `string` | The username to use with basic authentication | |
26 | | -| `voice_basic_auth_password` | `string` | The password to use with basic authentication | |
27 | | -| `environment` | Environment | The API environment. <br> **Default: `Environment.PRODUCTION`** | |
28 | | -| `timeout` | `float` | The value to use for connection timeout. <br> **Default: 60** | |
29 | | -| `max_retries` | `int` | The number of times to retry an endpoint call if it fails. <br> **Default: 3** | |
30 | | -| `backoff_factor` | `float` | A backoff factor to apply between attempts after the second try. <br> **Default: 0** | |
31 | | - |
32 | | -The API client can be initialized as following. |
33 | | - |
34 | | -```python |
35 | | -from bandwidth.bandwidth_client import BandwidthClient |
36 | | - |
37 | | -client = BandwidthClient( |
38 | | - messaging_basic_auth_user_name='MessagingBasicAuthUserName', |
39 | | - messaging_basic_auth_password='MessagingBasicAuthPassword', |
40 | | - voice_basic_auth_user_name='VoiceBasicAuthUserName', |
41 | | - voice_basic_auth_password='VoiceBasicAuthPassword', |
42 | | - environment = Environment.PRODUCTION,) |
43 | | -``` |
44 | | - |
45 | | -API calls return an `ApiResponse` object that includes the following fields: |
46 | | - |
47 | | -| Field | Description | |
48 | | -| --- | --- | |
49 | | -| `status_code` | Status code of the HTTP response | |
50 | | -| `reason_phrase` | Reason phrase of the HTTP response | |
51 | | -| `headers` | Headers of the HTTP response as a dictionary | |
52 | | -| `text` | The body of the HTTP response as a string | |
53 | | -| `request` | HTTP request info | |
54 | | -| `errors` | Errors, if they exist | |
55 | | -| `body` | The deserialized body of the HTTP response | |
56 | | - |
57 | | -## Authorization |
58 | | - |
59 | | -This API does not require authentication. |
60 | | - |
61 | | -## API Reference |
62 | | - |
63 | | -### List of APIs |
64 | | - |
65 | | -* |
66 | | - |
| 1 | +# Bandwidth Python SDK |
| 2 | + |
| 3 | +Bandwidth's API docs can be found at https://dev.bandwidth.com |
| 4 | + |
| 5 | +Python specific docs can be found at https://dev.bandwidth.com/sdks/python.html |
| 6 | + |
| 7 | +# Python SDK |
| 8 | + |
| 9 | +## Download & Install |
| 10 | + |
| 11 | +``` |
| 12 | +pip install bandwidth-sdk |
| 13 | +``` |
| 14 | + |
| 15 | +## Initialize Bandwidth Client |
| 16 | + |
| 17 | +```python |
| 18 | +from bandwidth.bandwidth_client import BandwidthClient |
| 19 | + |
| 20 | +from bandwidth.messaging.models.message_request import MessageRequest |
| 21 | +from bandwidth.messaging.exceptions.generic_client_exception import GenericClientException |
| 22 | +from bandwidth.messaging.exceptions.path_client_exception import PathClientException |
| 23 | + |
| 24 | +from bandwidth.voice.models.api_create_call_request import ApiCreateCallRequest |
| 25 | +from bandwidth.voice.models.modify_call_recording_state import ModifyCallRecordingState |
| 26 | +from bandwidth.voice.exceptions.error_response_exception import ErrorResponseException |
| 27 | +from bandwidth.voice.bxml.response import Response |
| 28 | +from bandwidth.voice.bxml.verbs import * |
| 29 | + |
| 30 | +##Initialize client |
| 31 | +voice_basic_auth_user_name = 'username' |
| 32 | +voice_basic_auth_password = 'password' |
| 33 | +messaging_basic_auth_user_name = 'token' |
| 34 | +messaging_basic_auth_password = 'secret' |
| 35 | + |
| 36 | +bandwidth_client = BandwidthClient( |
| 37 | + voice_basic_auth_user_name=voice_basic_auth_user_name, |
| 38 | + voice_basic_auth_password=voice_basic_auth_password, |
| 39 | + messaging_basic_auth_user_name=messaging_basic_auth_user_name, |
| 40 | + messaging_basic_auth_password=messaging_basic_auth_password) |
| 41 | +``` |
| 42 | + |
| 43 | +## Create Phone Call |
| 44 | + |
| 45 | +```python |
| 46 | +voice_client = bandwidth_client.voice_client.client |
| 47 | +account_id = "1" |
| 48 | + |
| 49 | +##Create phone call |
| 50 | +body = ApiCreateCallRequest() |
| 51 | +body.mfrom = "+17777777777" |
| 52 | +body.to = "+16666666666" |
| 53 | +body.application_id = "3-d-4-b-5" |
| 54 | +body.answer_url = "https://test.com" |
| 55 | + |
| 56 | +try: |
| 57 | + response = voice_client.create_call(account_id, body=body) |
| 58 | + print(response.body.call_id) #c-3f758f24-a59bb21e-4f23-4d62-afe9-53o2ls3o4saio4l |
| 59 | + print(response.status_code) #201 |
| 60 | +except ErrorResponseException as e: |
| 61 | + print(e.description) #Invalid from: must be an E164 telephone number |
| 62 | + print(e.response_code) #400 |
| 63 | +``` |
| 64 | + |
| 65 | +## Generate BXML |
| 66 | + |
| 67 | +```python |
| 68 | +response = Response() |
| 69 | +speak_sentence = SpeakSentence( |
| 70 | + sentence="Test", |
| 71 | + voice="susan", |
| 72 | + locale="en_US", |
| 73 | + gender="female" |
| 74 | +) |
| 75 | + |
| 76 | +response.add_verb(speak_sentence) |
| 77 | +print(response.to_bxml()) |
| 78 | +``` |
| 79 | + |
| 80 | +## Send Text Message |
| 81 | + |
| 82 | +```python |
| 83 | +messaging_client = bandwidth_client.messaging_client.client |
| 84 | +account_id = "1" |
| 85 | + |
| 86 | +body = MessageRequest() |
| 87 | +body.application_id = "1-d-b" |
| 88 | +body.to = ["+17777777777"] |
| 89 | +body.mfrom = "+18888888888" |
| 90 | +body.text = "Greetings!" |
| 91 | + |
| 92 | +try: |
| 93 | + response = messaging_client.create_message(account_id, body=body) |
| 94 | + print(response.body.id) #1570819529611mexbyfr7ugrouuxy |
| 95 | + print(response.status_code) #202 |
| 96 | +except GenericClientException as e: |
| 97 | + print(e.description) #Your request could not be accepted. |
| 98 | + print(e.response_code) #400 |
| 99 | +except PathClientException as e: |
| 100 | + print(e.message) #Access is denied |
| 101 | + print(e.response_code) #403 |
| 102 | +``` |
0 commit comments