<commit_before>import json
import urllib2

from django.conf import settings
from django.http import HttpRequest
from django_statsd.clients import statsd

from cef import log_cef as _log_cef
import commonware.log

log = commonware.log.getLogger('z.services')


def sign(receipt):
    """
    Send the receipt to the signing service.

    This could possibly be made async via celery.
    """
    destination = settings.SIGNING_SERVER
    # If no destination is set. Just ignore this request.
    if not destination:
        return

    timeout = settings.SIGNING_SERVER_TIMEOUT

    headers = {'Content-Type': 'application/json'}
    data = receipt if isinstance(receipt, basestring) else json.dumps(receipt)
    request = urllib2.Request(destination, data, headers)

    try:
        with statsd.timer('services.sign'):
            response = urllib2.urlopen(request, timeout=timeout)
    except urllib2.HTTPError, error:
        # Will occur when a 3xx or greater code is returned
        log.error('Posting to signing failed: %s'
                  % (error.code))
        return error.code
    except:
        # Will occur when some other error occurs.
        log.error('Posting to signing failed', exc_info=True)
        return

    # The list of valid statuses are here:
    # https://wiki.mozilla.org/Apps/WebApplicationReceipt/SigningService
    if response.status_code not in [400, 401, 409, 503]:
        log.error('Posting to signing failed: %s'
                  % (response.status_code))

    return response.status_code


def decode(receipt):
    """
    Decode and verify that the receipt is sound from a crypto point of view.
    Will raise errors if the receipt is not valid, returns receipt contents
    if it is valid.
    """
    raise NotImplementedError


def cef(environ, app, msg, longer):
    """Log receipt transactions to the CEF library."""
    c = {'cef.product': getattr(settings, 'CEF_PRODUCT', 'AMO'),
         'cef.vendor': getattr(settings, 'CEF_VENDOR', 'Mozilla'),
         'cef.version': getattr(settings, 'CEF_VERSION', '0'),
         'cef.device_version': getattr(settings, 'CEF_DEVICE_VERSION', '0'),
         'cef.file': getattr(settings, 'CEF_FILE', 'syslog'), }

    kwargs = {'username': getattr(request, 'amo_user', ''),
              'signature': 'RECEIPT%s' % msg.upper(),
              'msg': longer, 'config': c,
              'cs2': app, 'cs2Label': 'ReceiptTransaction'}

    if isinstance(environ, HttpRequest):
        environ = environ.META.copy()
    return _log_cef('Receipt %s' % msg, 5, environ, **kwargs)<commit_msg>fix tests<commit_after>import json
import urllib2

from django.conf import settings
from django.http import HttpRequest
from django_statsd.clients import statsd

from cef import log_cef as _log_cef
import commonware.log

log = commonware.log.getLogger('z.services')


def sign(receipt):
    """
    Send the receipt to the signing service.

    This could possibly be made async via celery.
    """
    destination = settings.SIGNING_SERVER
    # If no destination is set. Just ignore this request.
    if not destination:
        return

    timeout = settings.SIGNING_SERVER_TIMEOUT

    headers = {'Content-Type': 'application/json'}
    data = receipt if isinstance(receipt, basestring) else json.dumps(receipt)
    request = urllib2.Request(destination, data, headers)

    try:
        with statsd.timer('services.sign'):
            response = urllib2.urlopen(request, timeout=timeout)
    except urllib2.HTTPError, error:
        # Will occur when a 3xx or greater code is returned
        log.error('Posting to signing failed: %s'
                  % (error.code))
        return error.code
    except:
        # Will occur when some other error occurs.
        log.error('Posting to signing failed', exc_info=True)
        return

    # The list of valid statuses are here:
    # https://wiki.mozilla.org/Apps/WebApplicationReceipt/SigningService
    if response.status_code not in [400, 401, 409, 503]:
        log.error('Posting to signing failed: %s'
                  % (response.status_code))

    return response.status_code


def decode(receipt):
    """
    Decode and verify that the receipt is sound from a crypto point of view.
    Will raise errors if the receipt is not valid, returns receipt contents
    if it is valid.
    """
    raise NotImplementedError


def cef(environ, app, msg, longer):
    """Log receipt transactions to the CEF library."""
    c = {'cef.product': getattr(settings, 'CEF_PRODUCT', 'AMO'),
         'cef.vendor': getattr(settings, 'CEF_VENDOR', 'Mozilla'),
         'cef.version': getattr(settings, 'CEF_VERSION', '0'),
         'cef.device_version': getattr(settings, 'CEF_DEVICE_VERSION', '0'),
         'cef.file': getattr(settings, 'CEF_FILE', 'syslog'), }

    kwargs = {'username': getattr(environ, 'amo_user', ''),
              'signature': 'RECEIPT%s' % msg.upper(),
              'msg': longer, 'config': c,
              'cs2': app, 'cs2Label': 'ReceiptTransaction'}

    if isinstance(environ, HttpRequest):
        environ = environ.META.copy()
    return _log_cef('Receipt %s' % msg, 5, environ, **kwargs)
{'fraction_non_alphanumeric': 0.11003110419906688, 'fraction_numerical': 0.008553654743390357, 'mean_word_length': 2.988372093023256, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5591794', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1523, 'n_words': 466}