Skip to content

Commit

Permalink
Update GET helper function, add custom columns to create profile (#1055)
Browse files Browse the repository at this point in the history
* Update mobilecommons.py

* Fix bugs I created haha

* Fix phones issue in get_profiles

* Update mobilecommons.py

* linting issues
  • Loading branch information
cmdelrio authored May 23, 2024
1 parent 66f288e commit d3a8c8d
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions parsons/mobilecommons/mobilecommons.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,18 @@ def _mc_get_request(
response_dict = self._parse_get_request(endpoint=endpoint, params=page_params)
# Check to see if page was empty if num parameter is available
if page_indicator == "num":
empty_page = int(response_dict["response"][first_data_key]["num"]) > 0
empty_page = int(response_dict["response"][first_data_key]["num"]) == 0

if not empty_page:
# Extract data
response_table = Table(response_dict["response"][first_data_key][second_data_key])
response_data = response_dict["response"][first_data_key][second_data_key]
# When only one row of data it is returned as dict instead of list, which
# cannot be put into table
if isinstance(response_data, dict):
response_data = [response_data]

response_table = Table(response_data)

# Append to final table
final_table.concat(response_table)
final_table.materialize()
Expand Down Expand Up @@ -347,6 +354,9 @@ def get_profiles(
custom_cols = "true" if include_custom_columns else "false"
subscriptions = "true" if include_subscriptions else "false"

if phones:
phones = ",".join(phones)

params = {
"phone_number": phones,
"from": _format_date(first_date),
Expand Down Expand Up @@ -376,9 +386,10 @@ def create_profile(
city=None,
state=None,
opt_in_path_id=None,
custom_column_values=None,
):
"""
A function for creating a single MobileCommons profile
A function for creating or updating a single MobileCommons profile
`Args:`
phone: str
Expand All @@ -400,9 +411,12 @@ def create_profile(
opt_in_path_id: str
ID of the opt-in path to send new profile through. This will determine the welcome
text they receive.
custom_column_values: dict
Dictionary with custom column names as keys and custom column values
as dictionary values
`Returns:`
ID of created profile
ID of created/updated profile
"""

params = {
Expand All @@ -418,5 +432,8 @@ def create_profile(
**self.default_params,
}

if custom_column_values:
params = params.merge(custom_column_values)

response = self._mc_post_request("profile_update", params=params)
return response["profile"]["id"]

0 comments on commit d3a8c8d

Please sign in to comment.