Skip to content

Commit

Permalink
add items and events endpoints, mocks and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed May 3, 2023
1 parent 550027e commit 4ca4940
Show file tree
Hide file tree
Showing 13 changed files with 533 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/vonage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ def delete(self, host, request_uri, auth_type=None):

def parse(self, host, response: Response):
logger.debug(f"Response headers {repr(response.headers)}")
print(f'response.status_code is: {response.status_code}')
if response.status_code == 401:
raise AuthenticationError("Authentication failed. Check you're using a valid authentication method.")
elif response.status_code == 204 or response.status_code == 202:
Expand Down
74 changes: 73 additions & 1 deletion src/vonage/proactive_connect.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from .errors import ProactiveConnectError

import requests
import logging

logger = logging.getLogger("vonage")


class ProactiveConnect:
def __init__(self, client):
Expand Down Expand Up @@ -50,7 +55,74 @@ def sync_list_from_datasource(self, list_id: str):
auth_type=self._auth_type,
)

def _check_pagination_params(self, page=None, page_size=None) -> dict:
def list_all_items(self, list_id: str, page: int = None, page_size: int = None):
params = self._check_pagination_params(page, page_size)
return self._client.get(
self._client.proactive_connect_host(),
f'/v0.1/bulk/lists/{list_id}/items',
params,
auth_type=self._auth_type,
)

def create_item(self, list_id: str, data: dict):
params = {'data': data}
return self._client.post(
self._client.proactive_connect_host(),
f'/v0.1/bulk/lists/{list_id}/items',
params,
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(),
f'/v0.1/bulk/lists/{list_id}/items/{item_id}',
auth_type=self._auth_type,
)

def update_item(self, list_id: str, item_id: str, data: dict):
params = {'data': data}
return self._client.put(
self._client.proactive_connect_host(),
f'/v0.1/bulk/lists/{list_id}/items/{item_id}',
params,
auth_type=self._auth_type,
)

def delete_item(self, list_id: str, item_id: str):
return self._client.delete(
self._client.proactive_connect_host(),
f'/v0.1/bulk/lists/{list_id}/items/{item_id}',
auth_type=self._auth_type,
)

def import_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)}")
response = requests.post(uri, headers=self._client._add_jwt_to_request_headers(), files={'file': csv_file})
return self._client.parse(self._client.proactive_connect_host(), response)

def list_events(self, page: int = None, page_size: int = None):
params = self._check_pagination_params(page, page_size)
return self._client.get(
self._client.proactive_connect_host(), '/v0.1/bulk/events', params, auth_type=self._auth_type
)

def _check_pagination_params(self, page: int = None, page_size: int = None) -> dict:
params = {}
if page is not None:
if type(page) == int and page > 0:
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import platform

import pytest
import csv


# Ensure our client isn't being configured with real values!
Expand Down
3 changes: 3 additions & 0 deletions tests/data/proactive_connect/csv_to_upload.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'alice','1234'
'bob','5678'
'charlie','9012'
3 changes: 3 additions & 0 deletions tests/data/proactive_connect/import_from_csv.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"inserted": 3
}
11 changes: 11 additions & 0 deletions tests/data/proactive_connect/item.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "d91c39ed-7c34-4803-a139-34bb4b7c6d53",
"list_id": "246d17c4-79e6-4a25-8b4e-b777a83f6c30",
"data": {
"firstName": "John",
"lastName": "Doe",
"phone": "123456789101"
},
"created_at": "2023-05-02T21:07:25.790Z",
"updated_at": "2023-05-02T21:07:25.790Z"
}
9 changes: 9 additions & 0 deletions tests/data/proactive_connect/item_400.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "https://developer.vonage.com/en/api-errors",
"title": "Request data did not validate",
"detail": "Bad Request",
"instance": "8e2dd3f1-1718-48fc-98de-53e1d289d0b4",
"errors": [
"data must be an object"
]
}
40 changes: 40 additions & 0 deletions tests/data/proactive_connect/list_all_items.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"total_items": 2,
"page": 1,
"page_size": 100,
"order": "asc",
"_embedded": {
"items": [
{
"id": "04c7498c-bae9-40f9-bdcb-c4eabb0418fe",
"created_at": "2023-05-02T21:04:47.507Z",
"updated_at": "2023-05-02T21:04:47.507Z",
"list_id": "246d17c4-79e6-4a25-8b4e-b777a83f6c30",
"data": {
"test": 0,
"test2": 1
}
},
{
"id": "d91c39ed-7c34-4803-a139-34bb4b7c6d53",
"created_at": "2023-05-02T21:07:25.790Z",
"updated_at": "2023-05-02T21:07:25.790Z",
"list_id": "246d17c4-79e6-4a25-8b4e-b777a83f6c30",
"data": {
"phone": "123456789101",
"lastName": "Doe",
"firstName": "John"
}
}
]
},
"total_pages": 1,
"_links": {
"first": {
"href": "https://api-eu.vonage.com/v0.1/bulk/lists/246d17c4-79e6-4a25-8b4e-b777a83f6c30/items?page_size=100&order=asc&page=1"
},
"self": {
"href": "https://api-eu.vonage.com/v0.1/bulk/lists/246d17c4-79e6-4a25-8b4e-b777a83f6c30/items?page_size=100&order=asc&page=1"
}
}
}
70 changes: 70 additions & 0 deletions tests/data/proactive_connect/list_events.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"total_items": 1,
"page": 1,
"page_size": 100,
"total_pages": 1,
"_links": {
"self": {
"href": "https://api-eu.vonage.com/v0.1/bulk/events?page_size=100&page=1"
},
"prev": {
"href": "https://api-eu.vonage.com/v0.1/bulk/events?page_size=100&page=1"
},
"next": {
"href": "https://api-eu.vonage.com/v0.1/bulk/events?page_size=100&page=1"
},
"first": {
"href": "https://api-eu.vonage.com/v0.1/bulk/events?page_size=100&page=1"
}
},
"_embedded": {
"events": [
{
"occurred_at": "2022-08-07T13:18:21.970Z",
"type": "action-call-succeeded",
"id": "e8e1eb4d-61e0-4099-8fa7-c96f1c0764ba",
"job_id": "c68e871a-c239-474d-a905-7b95f4563b7e",
"src_ctx": "et-e4ab4b75-9e7c-4f26-9328-394a5b842648",
"action_id": "26c5bbe2-113e-4201-bd93-f69e0a03d17f",
"data": {
"url": "https://postman-echo.com/post",
"args": {},
"data": {
"from": ""
},
"form": {},
"json": {
"from": ""
},
"files": {},
"headers": {
"host": "postman-echo.com",
"user-agent": "got (https://github.com/sindresorhus/got)",
"content-type": "application/json",
"content-length": "11",
"accept-encoding": "gzip, deflate, br",
"x-amzn-trace-id": "Root=1-62efbb9e-53636b7b794accb87a3d662f",
"x-forwarded-port": "443",
"x-nexmo-trace-id": "8a6fed94-7296-4a39-9c52-348f12b4d61a",
"x-forwarded-proto": "https"
}
},
"run_id": "7d0d4e5f-6453-4c63-87cf-f95b04377324",
"recipient_id": "14806904549"
},
{
"occurred_at": "2022-08-07T13:18:20.289Z",
"type": "recipient-response",
"id": "8c8e9894-81be-4f6e-88d4-046b6c70ff8c",
"job_id": "c68e871a-c239-474d-a905-7b95f4563b7e",
"src_ctx": "et-e4ab4b75-9e7c-4f26-9328-394a5b842648",
"data": {
"from": "441632960411",
"text": "hello there"
},
"run_id": "7d0d4e5f-6453-4c63-87cf-f95b04377324",
"recipient_id": "441632960758"
}
]
}
}
4 changes: 4 additions & 0 deletions tests/data/proactive_connect/list_items.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"favourite_number","least_favourite_number"
0,1
1,0
0,0
File renamed without changes.
11 changes: 11 additions & 0 deletions tests/data/proactive_connect/update_item.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "d91c39ed-7c34-4803-a139-34bb4b7c6d53",
"created_at": "2023-05-02T21:07:25.790Z",
"updated_at": "2023-05-03T19:50:33.207Z",
"list_id": "246d17c4-79e6-4a25-8b4e-b777a83f6c30",
"data": {
"first_name": "John",
"last_name": "Doe",
"phone": "447007000000"
}
}
Loading

0 comments on commit 4ca4940

Please sign in to comment.