def _send_fax(self, subject, body, comm): """Send the message as a fax""" api = PhaxioApi( settings.PHAXIO_KEY, settings.PHAXIO_SECRET, raise_errors=True, ) files = [f.ffile for f in comm.files.all()] callback_url = 'https://%s%s' % ( settings.MUCKROCK_URL, reverse('phaxio-callback'), ) api.send(to=self.email, header_text=subject[:50], string_data=body, string_data_type='text', files=files, batch=True, batch_delay=settings.PHAXIO_BATCH_DELAY, batch_collision_avoidance=True, callback_url=callback_url, **{'tag[comm_id]': comm.pk}) comm.delivered = 'fax'
def send_fax(comm_id, subject, body, **kwargs): """Send a fax using the Phaxio API""" api = PhaxioApi( settings.PHAXIO_KEY, settings.PHAXIO_SECRET, raise_errors=True, ) try: comm = FOIACommunication.objects.get(pk=comm_id) except FOIACommunication.DoesNotExist as exc: send_fax.retry( countdown=300, args=[comm_id, subject, body], kwargs=kwargs, exc=exc, ) files = [f.ffile for f in comm.files.all()] callback_url = 'https://%s%s' % ( settings.MUCKROCK_URL, reverse('phaxio-callback'), ) try: results = api.send(to=comm.foia.email, header_text=subject[:45], string_data=body, string_data_type='text', files=files, batch=True, batch_delay=settings.PHAXIO_BATCH_DELAY, batch_collision_avoidance=True, callback_url=callback_url, **{'tag[comm_id]': comm.pk}) except PhaxioError as exc: logger.error( 'Send fax error, will retry: %s', exc, exc_info=sys.exc_info(), ) send_fax.retry( countdown=300, args=[comm_id, subject, body], kwargs=kwargs, exc=exc, ) comm.delivered = 'fax' comm.fax_id = results['faxId'] comm.save()
def send_fax(comm_id, subject, body, error_count, **kwargs): """Send a fax using the Phaxio API""" api = PhaxioApi( settings.PHAXIO_KEY, settings.PHAXIO_SECRET, raise_errors=True, ) try: comm = FOIACommunication.objects.get(pk=comm_id) except FOIACommunication.DoesNotExist as exc: send_fax.retry( countdown=300, args=[comm_id, subject, body, error_count], kwargs=kwargs, exc=exc, ) files = [f.ffile for f in comm.files.all()] callback_url = '{}{}'.format( settings.MUCKROCK_URL, reverse('phaxio-callback'), ) fax = FaxCommunication.objects.create( communication=comm, sent_datetime=timezone.now(), to_number=comm.foia.fax, ) try: results = api.send(to=comm.foia.fax.as_e164, header_text=subject[:45], string_data=body, string_data_type='text', files=files, batch=True, batch_delay=settings.PHAXIO_BATCH_DELAY, batch_collision_avoidance=True, callback_url=callback_url, **{ 'tag[fax_id]': fax.pk, 'tag[error_count]': error_count, }) fax.fax_id = results['faxId'] fax.save() except PhaxioError as exc: FaxError.objects.create( fax=fax, datetime=timezone.now(), recipient=comm.foia.fax, error_type='apiError', error_code=exc.args[0], ) fatal_errors = { ('Phone number is not formatted correctly or invalid. ' 'Please check the number and try again.'), } if exc.args[0] in fatal_errors: comm.foia.fax.status = 'error' comm.foia.fax.save() ReviewAgencyTask.objects.ensure_one_created( agency=comm.foia.agency, resolved=False, ) else: logger.error( 'Send fax error, will retry: %s', exc, exc_info=sys.exc_info(), ) send_fax.retry( countdown=300, args=[comm_id, subject, body, error_count], kwargs=kwargs, exc=exc, )
def test_raise_errors_option(self): api = PhaxioApi(self.key, self.secret, raise_errors=False) response = api.send(to='invalid_number', string_data='test') self.assertFalse(response['success'])
def test_valid_request(self): api = PhaxioApi(self.key, self.secret, raise_errors=True) response = api.send(to='8138014253', string_data='test') self.assertTrue(response['success'])
def send_fax(comm_id, subject, body, **kwargs): """Send a fax using the Phaxio API""" api = PhaxioApi( settings.PHAXIO_KEY, settings.PHAXIO_SECRET, raise_errors=True, ) try: comm = FOIACommunication.objects.get(pk=comm_id) except FOIACommunication.DoesNotExist as exc: send_fax.retry( countdown=300, args=[comm_id, subject, body], kwargs=kwargs, exc=exc, ) files = [f.ffile for f in comm.files.all()] callback_url = 'https://%s%s' % ( settings.MUCKROCK_URL, reverse('phaxio-callback'), ) fax = FaxCommunication.objects.create( communication=comm, sent_datetime=datetime.now(), to_number=comm.foia.fax, ) try: results = api.send( to=comm.foia.fax.as_e164, header_text=subject[:45], string_data=body, string_data_type='text', files=files, batch=True, batch_delay=settings.PHAXIO_BATCH_DELAY, batch_collision_avoidance=True, callback_url=callback_url, **{'tag[fax_id]': fax.pk} ) except PhaxioError as exc: logger.error( 'Send fax error, will retry: %s', exc, exc_info=sys.exc_info(), ) FaxError.objects.create( fax=fax, datetime=datetime.now(), recipient=comm.foia.fax, error_type='apiError', error_code=exc.args[0], ) send_fax.retry( countdown=300, args=[comm_id, subject, body], kwargs=kwargs, exc=exc, ) fax.fax_id = results['faxId'] fax.save()