def test_remove_empty_keys(): # Assert key removed when None test_dict = {'a': None, 'b': 2} assert json_format.remove_empty_keys(test_dict) == {'b': 2} # Assert key not removed when None test_dict = {'a': 1, 'b': 2} assert json_format.remove_empty_keys(test_dict) == {'a': 1, 'b': 2} # Assert that a nested empty string is removed test_dict = {'a': '', 'b': 2} assert json_format.remove_empty_keys(test_dict) == {'b': 2}
def update_agent(self, agent_id, name=None, full_name=None, send_invite=False): """ Update an agent. `Args:` agent_id: str The agent id. name: str The name of the agent. full_name: str The full name of the agent. phone_number: str The valid phone number of the agent. send_invite: boolean Send an invitation to the agent. `Returns:` dict """ agent = {'name': name, 'fullName': full_name, 'sendInvite': send_invite} # Remove empty args in dictionary agent = json_format.remove_empty_keys(agent) logger.info(f'Updating agent {agent_id}.') return self._request(f'agents/{agent_id}', req_type="PUT", payload=agent)
def create_agent(self, group_id, name, full_name, phone_number, send_invite=False, email=None): """ Create an agent. `Args:` group_id: str The group id to assign the agent. name: str The name of the agent. full_name: str The full name of the agent. phone_number: str The valid phone number of the agent. send_invite: boolean Send an invitation to the agent. email: The email address of the agent. `Returns:` dict """ agent = {'name': name, 'fullName': full_name, 'phoneNumber': phone_number, 'sendInvite': send_invite, 'email': email} # Remove empty args in dictionary agent = json_format.remove_empty_keys(agent) logger.info(f'Generating {full_name} agent.') return self._request(f'groups/{group_id}/agents', req_type="POST", payload=agent)
def get_account_usage(self, category=None, start_date=None, end_date=None, time_period=None, group_by=None, exclude_null=False): """ Get Twilio account usage. `Args:` category: str Filter to a specfic type of usage category. The list of possibilities can be found `here <https://www.twilio.com/docs/usage/api/usage-record?code-sample=code-last-months-usage-for-all-usage-categories-4&code-language=Python&code-sdk-version=5.x#usage-all-categories>`_. start_date: str Filter to usage from a specific start date (ex. ``2019-01-01``). end_date: str Filter to usage from a specific end date (ex. ``2019-01-01``). time_period: str A convenience method to filter usage. Can be one of ``today``, ``yesterday``, ``this_month``, ``last_month``. group_by: str The time interval to group usage by. Can be one of ``daily``, ``monthly`` or ``yearly``. exclude_null: boolean Exclude rows that have no usage. `Returns:` Parsons Table See :ref:`parsons-table` for output options. """ # noqa: E501,E261 # Add populated arguments args = {'category': category, 'start_date': start_date, 'end_date': end_date} args = json_format.remove_empty_keys(args) # Parse out the time_periods if time_period == 'today': r = self.client.usage.records.today.list(**args) elif time_period == 'yesterday': r = self.client.usage.records.yesterday.list(**args) elif time_period == 'this_month': r = self.client.usage.records.this_month.list(**args) elif time_period == 'last_month': r = self.client.usage.records.last_month.list(**args) # Parse out the group by elif group_by == 'daily': r = self.client.usage.records.daily.list(**args) elif group_by == 'monthly': r = self.client.usage.records.monthly.list(**args) elif group_by == 'yearly': r = self.client.usage.records.yearly.list(**args) else: r = self.client.usage.records.list(**args) tbl = self.table_convert(r) if exclude_null: tbl.remove_null_rows('count', null_value='0') return tbl
def create_lead(self, group_id, phone_number, first_name, last_name=None, email=None, notes=None, follow_up=None, custom_fields=None, tag_ids=None): """ Create a lead. `Args:` group_id: str The group id to assign the leads. first_name: str The first name of the lead. phone_number: str The phone number of the lead. last_name: str The last name of the lead. email: str The email address of the lead. notes: str The notes for the lead. follow_up: str Follow up for the lead. custom_fields: dict A dictionary of custom fields, with key as the value name, and value as the value. tag_ids: list A list of tag ids. `Returns:` ``None`` """ lead = { 'firstName': first_name, 'lastName': last_name, 'email': email, 'phoneNumber': phone_number, 'notes': notes, 'followUp': follow_up, 'customFields': custom_fields, 'tagIds': tag_ids } # Remove empty args in dictionary lead = json_format.remove_empty_keys(lead) logger.info(f'Generating lead for {first_name} {last_name}.') return self._request(f'groups/{group_id}/leads', req_type="POST", payload=lead)
def update_lead(self, lead_id, first_name=None, last_name=None, email=None, global_opt_out=None, notes=None, follow_up=None, tag_ids=None): """ Update a lead. `Args`: lead_id: str The lead id first_name: str The first name of the lead last_name: str The last name of the lead email: str The email address of the lead global_opt_out: boolean Opt out flag for the lead notes: str The notes for the lead follow_up: str Follow up for the lead tag_ids: list Tags to apply to lead `Returns:` dict """ lead = { 'leadId': lead_id, 'firstName': first_name, 'lastName': last_name, 'email': email, 'globalOptedOut': global_opt_out, 'notes': notes, 'followUp': follow_up, 'tagIds': tag_ids } # Remove empty args in dictionary lead = json_format.remove_empty_keys(lead) logger.info(f'Updating lead for {first_name} {last_name}.') return self._request(f'leads/{lead_id}', req_type="PUT", payload=lead)