Skip to content

Commit

Permalink
adding proactive connect info to README.md and renaming some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed May 3, 2023
1 parent 4ca4940 commit 0781532
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 22 deletions.
89 changes: 89 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ need a Vonage account. Sign up [for free at vonage.com][signup].
- [NCCO Builder](#ncco-builder)
- [Verify API](#verify-api)
- [Number Insight API](#number-insight-api)
- [Proactive Connect API](#proactive-connect-api)
- [Account API](#account-api)
- [Number Management API](#number-management-api)
- [Pricing API](#pricing-api)
Expand Down Expand Up @@ -537,6 +538,94 @@ client.number_insight.get_advanced_number_insight(number='447700900000')

Docs: [https://developer.nexmo.com/api/number-insight#getNumberInsightAdvanced](https://developer.nexmo.com/api/number-insight?utm_source=DEV_REL&utm_medium=github&utm_campaign=python-client-library#getNumberInsightAdvanced)

## Proactive Connect API

Full documentation for the [Proactive Connect API](https://developer.vonage.com/en/proactive-connect/overview) is available here.

These methods help you manage lists of contacts when using the API:

### Find all lists
```python
client.proactive_connect.list_all_lists()
```

### Create a list
Lists can be created manually or imported from Salesforce.

```python
params = {'name': 'my list', 'description': 'my description', 'tags': ['vip']}
client.proactive_connect.create_list(params)
```

### Get a list
```python
client.proactive_connect.get_list(LIST_ID)
```

### Update a list
```python
params = {'name': 'my list', 'tags': ['sport', 'football']}
client.proactive_connect.update_list(LIST_ID, params)
```

### Delete a list
```python
client.proactive_connect.delete_list(LIST_ID)
```

### Sync a list from an external datasource
```python
params = {'name': 'my list', 'tags': ['sport', 'football']}
client.proactive_connect.sync_list_from_datasource(LIST_ID)
```

These methods help you work with individual items in a list:
### Find all items in a list
```python
client.proactive_connect.list_all_items(LIST_ID)
```

### Create a new list item
```python
data = {'firstName': 'John', 'lastName': 'Doe', 'phone': '123456789101'}
client.proactive_connect.create_item(LIST_ID, data)
```

### Get a list item
```python
client.proactive_connect.get_item(LIST_ID, ITEM_ID)
```

### Update a list item
```python
data = {'firstName': 'John', 'lastName': 'Doe', 'phone': '447007000000'}
client.proactive_connect.update_item(LIST_ID, ITEM_ID, data)
```

### Delete a list item
```python
client.proactive_connect.delete_item(LIST_ID, ITEM_ID)
```

### Download all items in a list as a .csv file
```python
FILE_PATH = 'path/to/the/downloaded/file/location'
client.proactive_connect.download_list_items(LIST_ID, FILE_PATH)
```

### Upload items from a .csv file into a list
```python
FILE_PATH = 'path/to/the/file/to/upload/location'
client.proactive_connect.upload_list_items(LIST_ID, FILE_PATH)
```

This method helps you work with events emitted by the Proactive Connect API when in use:

### List all events
```python
client.proactive_connect.list_events()
```

## Account API

### Get your account balance
Expand Down
28 changes: 14 additions & 14 deletions src/vonage/proactive_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,6 @@ def create_item(self, list_id: str, data: dict):
auth_type=self._auth_type,
)

def download_list_items(self, list_id: str, file_path) -> list[dict]:
uri = f'https://{self._client.proactive_connect_host()}/v0.1/bulk/lists/{list_id}/items/download'
logger.debug(f"GET request sent to {repr(uri)}")
response = requests.get(
uri,
headers=self._client._add_jwt_to_request_headers(),
)
if 200 <= response.status_code < 300:
with open(file_path, 'wb') as file:
file.write(response.content)
else:
return self._client.parse(self._client.proactive_connect_host(), response)

def get_item(self, list_id: str, item_id: str):
return self._client.get(
self._client.proactive_connect_host(),
Expand All @@ -109,7 +96,20 @@ def delete_item(self, list_id: str, item_id: str):
auth_type=self._auth_type,
)

def import_list_items(self, list_id: str, file_path: str):
def download_list_items(self, list_id: str, file_path) -> list[dict]:
uri = f'https://{self._client.proactive_connect_host()}/v0.1/bulk/lists/{list_id}/items/download'
logger.debug(f"GET request sent to {repr(uri)}")
response = requests.get(
uri,
headers=self._client._add_jwt_to_request_headers(),
)
if 200 <= response.status_code < 300:
with open(file_path, 'wb') as file:
file.write(response.content)
else:
return self._client.parse(self._client.proactive_connect_host(), response)

def upload_list_items(self, list_id: str, file_path: str):
uri = f'https://{self._client.proactive_connect_host()}/v0.1/bulk/lists/{list_id}/items/import'
with open(file_path, 'rb') as csv_file:
logger.debug(f"POST request sent to {repr(uri)}")
Expand Down
16 changes: 8 additions & 8 deletions tests/test_proactive_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def test_download_list_items(proc):
)

proc.download_list_items(list_id, os.path.join(os.path.dirname(__file__), 'data/proactive_connect/list_items.csv'))
items = read_csv_file(os.path.join(os.path.dirname(__file__), 'data/proactive_connect/list_items.csv'))
items = _read_csv_file(os.path.join(os.path.dirname(__file__), 'data/proactive_connect/list_items.csv'))
assert items[0]['favourite_number'] == '0'
assert items[1]['least_favourite_number'] == '0'

Expand Down Expand Up @@ -535,7 +535,7 @@ def test_update_item_error_invalid_data(proc):


@responses.activate
def test_get_item_404(proc):
def test_update_item_404(proc):
list_id = '346d17c4-79e6-4a25-8b4e-b777a83f6c30'
item_id = 'd91c39ed-7c34-4803-a139-34bb4b7c6d53'
data = {'first_name': 'John', 'last_name': 'Doe', 'phone': '447007000000'}
Expand Down Expand Up @@ -589,21 +589,21 @@ def test_delete_item_404(proc):


@responses.activate
def test_import_list_items_from_csv(proc):
def test_upload_list_items_from_csv(proc):
list_id = '246d17c4-79e6-4a25-8b4e-b777a83f6c30'
file_path = os.path.join(os.path.dirname(__file__), 'data/proactive_connect/csv_to_upload.csv')
stub(
responses.POST,
f'https://api-eu.vonage.com/v0.1/bulk/lists/{list_id}/items/import',
fixture_path='proactive_connect/import_from_csv.json',
fixture_path='proactive_connect/upload_from_csv.json',
)

response = proc.import_list_items(list_id, file_path)
response = proc.upload_list_items(list_id, file_path)
assert response['inserted'] == 3


@responses.activate
def test_import_list_items_from_csv_404(proc):
def test_upload_list_items_from_csv_404(proc):
list_id = '346d17c4-79e6-4a25-8b4e-b777a83f6c30'
file_path = os.path.join(os.path.dirname(__file__), 'data/proactive_connect/csv_to_upload.csv')
stub(
Expand All @@ -614,7 +614,7 @@ def test_import_list_items_from_csv_404(proc):
)

with raises(ClientError) as err:
proc.import_list_items(list_id, file_path)
proc.upload_list_items(list_id, file_path)
assert (
str(err.value)
== 'The requested resource does not exist: Not Found (https://developer.vonage.com/en/api-errors)'
Expand All @@ -636,7 +636,7 @@ def test_list_events(proc):
assert lists['_embedded']['events'][0]['run_id'] == '7d0d4e5f-6453-4c63-87cf-f95b04377324'


def read_csv_file(path):
def _read_csv_file(path):
with open(os.path.join(os.path.dirname(__file__), path)) as csv_file:
reader = csv.DictReader(csv_file)
dict_list = [row for row in reader]
Expand Down

0 comments on commit 0781532

Please sign in to comment.