Exemplo n.º 1
0
 def _contact_iap(self, local_endpoint, params):
     account = self.env['iap.account'].get('sms')
     params['account_token'] = account.account_token
     endpoint = self.env['ir.config_parameter'].sudo().get_param(
         'sms.endpoint', DEFAULT_ENDPOINT)
     # TODO PRO, the default timeout is 15, do we have to increase it ?
     return iap_tools.iap_jsonrpc(endpoint + local_endpoint, params=params)
Exemplo n.º 2
0
 def _rpc_remote_api(self, action, params, timeout=15):
     if self.env.registry.in_test_mode():
         return False, 'Insufficient Credit'
     url = '%s/%s' % (self.get_endpoint(), action)
     account = self.env['iap.account'].get('partner_autocomplete')
     if not account.account_token:
         return False, 'No Account Token'
     params.update({
         'db_uuid':
         self.env['ir.config_parameter'].sudo().get_param('database.uuid'),
         'account_token':
         account.account_token,
         'country_code':
         self.env.company.country_id.code,
         'zip':
         self.env.company.zip,
     })
     try:
         return iap_tools.iap_jsonrpc(url=url,
                                      params=params,
                                      timeout=timeout), False
     except (ConnectionError, HTTPError, exceptions.AccessError,
             exceptions.UserError) as exception:
         _logger.error('Autocomplete API error: %s' % str(exception))
         return False, str(exception)
     except iap_tools.InsufficientCreditError as exception:
         _logger.warning(
             'Insufficient Credits for Autocomplete Service: %s' %
             str(exception))
         return False, 'Insufficient Credit'
Exemplo n.º 3
0
 def _perform_request(self):
     """
     This will perform the request and create the corresponding leads.
     The user will be notified if he hasn't enough credits.
     """
     server_payload = self._prepare_iap_payload()
     reveal_account = self.env['iap.account'].get('reveal')
     dbuuid = self.env['ir.config_parameter'].sudo().get_param(
         'database.uuid')
     endpoint = self.env['ir.config_parameter'].sudo().get_param(
         'reveal.endpoint',
         DEFAULT_ENDPOINT) + '/iap/clearbit/1/lead_mining_request'
     params = {
         'account_token': reveal_account.account_token,
         'dbuuid': dbuuid,
         'data': server_payload
     }
     try:
         response = iap_tools.iap_jsonrpc(endpoint,
                                          params=params,
                                          timeout=300)
         return response['data']
     except iap_tools.InsufficientCreditError as e:
         self.error = 'Insufficient credits. Recharge your account and retry.'
         self.state = 'error'
         self._cr.commit()
         raise e
Exemplo n.º 4
0
 def _contact_iap(self, local_endpoint, params):
     account = self.env['iap.account'].get('reveal')
     dbuuid = self.env['ir.config_parameter'].sudo().get_param(
         'database.uuid')
     params['account_token'] = account.account_token
     params['dbuuid'] = dbuuid
     base_url = self.env['ir.config_parameter'].sudo().get_param(
         'enrich.endpoint', self._DEFAULT_ENDPOINT)
     return iap_tools.iap_jsonrpc(base_url + local_endpoint,
                                  params=params,
                                  timeout=300)
Exemplo n.º 5
0
    def _snailmail_print_valid_address(self):
        """
        get response
        {
            'request_code': RESPONSE_OK, # because we receive 200 if good or fail
            'total_cost': total_cost,
            'credit_error': credit_error,
            'request': {
                'documents': documents,
                'options': options
                }
            }
        }
        """
        endpoint = self.env['ir.config_parameter'].sudo().get_param('snailmail.endpoint', DEFAULT_ENDPOINT)
        timeout = int(self.env['ir.config_parameter'].sudo().get_param('snailmail.timeout', DEFAULT_TIMEOUT))
        params = self._snailmail_create('print')
        response = iap_tools.iap_jsonrpc(endpoint + PRINT_ENDPOINT, params=params, timeout=timeout)
        for doc in response['request']['documents']:
            if doc.get('sent') and response['request_code'] == 200:
                note = _('The document was correctly sent by post.<br>The tracking id is %s', doc['send_id'])
                letter_data = {'info_msg': note, 'state': 'sent', 'error_code': False}
                notification_data = {
                    'notification_status': 'sent',
                    'failure_type': False,
                    'failure_reason': False,
                }
            else:
                error = doc['error'] if response['request_code'] == 200 else response['reason']

                note = _('An error occured when sending the document by post.<br>Error: %s', self._get_error_message(error))
                letter_data = {
                    'info_msg': note,
                    'state': 'error',
                    'error_code': error if error in ERROR_CODES else 'UNKNOWN_ERROR'
                }
                notification_data = {
                    'notification_status': 'exception',
                    'failure_type': self._get_failure_type(error),
                    'failure_reason': note,
                }

            letter = self.browse(doc['letter_id'])
            letter.write(letter_data)
            letter.notification_ids.sudo().write(notification_data)
        self.message_id._notify_message_notification_update()
Exemplo n.º 6
0
 def _perform_reveal_service(self, server_payload):
     result = False
     account_token = self.env['iap.account'].get('reveal')
     endpoint = self.env['ir.config_parameter'].sudo().get_param('reveal.endpoint', DEFAULT_ENDPOINT) + '/iap/clearbit/1/reveal'
     params = {
         'account_token': account_token.account_token,
         'data': server_payload
     }
     result = iap_tools.iap_jsonrpc(endpoint, params=params, timeout=300)
     for res in result.get('reveal_data', []):
         if not res.get('not_found'):
             lead = self._create_lead_from_response(res)
             self.env['crm.reveal.view'].search([('reveal_ip', '=', res['ip'])]).unlink()
         else:
             self.env['crm.reveal.view'].search([('reveal_ip', '=', res['ip'])]).write({
                 'reveal_state': 'not_found'
             })
     if result.get('credit_error'):
         self.env['crm.iap.lead.helpers'].notify_no_more_credit('reveal', self._name, 'reveal.already_notified')
         return False
     else:
         self.env['ir.config_parameter'].sudo().set_param('reveal.already_notified', False)
     return True
Exemplo n.º 7
0
    def get_credits(self, service_name):
        account = self.get(service_name, force_create=False)
        credit = 0

        if account:
            route = '/iap/1/balance'
            endpoint = iap_tools.iap_get_endpoint(self.env)
            url = endpoint + route
            params = {
                'dbuuid':
                self.env['ir.config_parameter'].sudo().get_param(
                    'database.uuid'),
                'account_token':
                account.account_token,
                'service_name':
                service_name,
            }
            try:
                credit = iap_tools.iap_jsonrpc(url=url, params=params)
            except Exception as e:
                _logger.info('Get credit error : %s', str(e))
                credit = -1

        return credit