-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from PitterPatterPython/add-twilio
Add twilio functions for lookups and usage
- Loading branch information
Showing
5 changed files
with
150 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
from datetime import date, datetime | ||
from typing import Dict, Any, List, Set, Union, Optional | ||
from requests import Response | ||
from requests.auth import HTTPBasicAuth | ||
import sys | ||
from .broker import make_request | ||
from .helpers import check_required_env_vars, combine_env_configs, validate_date_string | ||
|
||
|
||
env_config: Dict[str, Any] = combine_env_configs() | ||
|
||
def twilio_lookup(phone_number: str, data_packages: list=[], **kwargs: Dict[str, Any]) -> Response: | ||
"""query information on a phone number so that you can make a trusted interaction with your user. | ||
With this endpoint, you can format and validate phone numbers with the free Basic Lookup request | ||
and add on data packages to get even more in-depth carrier and caller information. | ||
Args: | ||
phone_number (str): The phone number to look up | ||
data_packages (list): A Python list of fields to return. Possible values are validation, | ||
caller_name, sim_swap, call_forwarding, line_status, line_type_intelligence, identity_match, | ||
reassigned_number, sms_pumping_risk, phone_number_quality_score, pre_fill. | ||
Returns: | ||
Response: requests.Response json response from the request | ||
""" | ||
|
||
# Define required environment variables | ||
required_vars: List[str] = [ | ||
'TWILIO_API_SID', | ||
'TWILIO_API_SECRET' | ||
] | ||
|
||
# Check and ensure that required variables are present, exits if not | ||
check_required_env_vars(env_config, required_vars) | ||
|
||
# Valid set of data packages for Twilio. Compare the ones that the user passed in | ||
# to ensure that they've passed valid ones. Exit immediately if they didn't. | ||
valid_data_packages: Set = {'validation', 'caller_name', 'sim_swap', | ||
'call_forwarding', 'line_status', 'line_type_intelligence', | ||
'identity_match', 'reassigned_number', 'sms_pumping_risk', | ||
'phone_number_quality_score', 'pre_fill'} | ||
data_packages_set: Set = set(data_packages) | ||
invalid_packages = data_packages_set - valid_data_packages | ||
if len(invalid_packages) != 0: | ||
print(f'[!] Error: "{", ".join(invalid_packages)}" are not valid data packages. Valid ' | ||
f'packages include {", ".join(valid_data_packages)}', file=sys.stderr) | ||
sys.exit(1) | ||
|
||
method: str = 'get' | ||
url: str = f'https://lookups.twilio.com/v2/PhoneNumbers/{phone_number}' | ||
|
||
auth = HTTPBasicAuth(env_config['TWILIO_API_SID'], env_config['TWILIO_API_SECRET']) | ||
|
||
params: Dict = { | ||
'Fields': ','.join(data_packages), | ||
**kwargs | ||
} | ||
|
||
result: Response = make_request(method=method, url=url, auth=auth, params=params) | ||
|
||
return result | ||
|
||
def twilio_usage_report(start_date: Union[str, date], | ||
end_date: Optional[Union[str, date]]=None) -> Response: | ||
"""Return a usage report for all activities between the start_date and end_date. | ||
Args: | ||
start_date (Union[str, date]): Only include usage that has occurred on or after this | ||
date. Specify the date in GMT and format as YYYY-MM-DD | ||
end_date (Optional[Union[str, date]], optional): Only include usage that occurred on | ||
or before this date. Specify the date in GMT and format as YYYY-MM-DD. Defaults to None. | ||
Returns: | ||
Response: requests.Response json response from the request | ||
""" | ||
|
||
# Define required environment variables | ||
required_vars: List[str] = [ | ||
'TWILIO_ACCOUNT_SID', | ||
'TWILIO_API_SID', | ||
'TWILIO_API_SECRET' | ||
] | ||
|
||
# Check and ensure that required variables are present, exits if not | ||
check_required_env_vars(env_config, required_vars) | ||
|
||
if end_date is None: | ||
end_date: str = datetime.now().strftime("%Y-%m-%d") | ||
|
||
if not validate_date_string(start_date) or not validate_date_string(end_date): | ||
print(f'[!] Error: One of your start date {start_date} or end date {end_date} ' | ||
'does not match the format YYYY-MM-DD') | ||
sys.exit() | ||
|
||
|
||
method: str = 'get' | ||
url: str = f'https://api.twilio.com/2010-04-01/Accounts/{env_config["TWILIO_ACCOUNT_SID"]}/Usage/Records.json' | ||
|
||
auth = HTTPBasicAuth(env_config['TWILIO_API_SID'], env_config['TWILIO_API_SECRET']) | ||
|
||
params: Dict = { | ||
'StartDate': start_date, | ||
'EndDate': end_date | ||
} | ||
|
||
result: Response = make_request(method=method, url=url, auth=auth, params=params) | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[tool.poetry] | ||
name = "ppp-connectors" | ||
packages = [{ include = "ppp_connectors" }] | ||
version = "0.3.1" | ||
version = "0.4.0" | ||
description = "A simple, lightweight set of connectors and functions to various APIs, controlled by a central broker." | ||
authors = [ | ||
"Rob D'Aveta <[email protected]>", | ||
|