<commit_before># -*- coding:utf-8 -*-
"""

    pygcm.request
    ~~~~~~~~~~~~~

    Request related modules.

"""

import json
from collections import Iterable
from pygcm.exceptions import GCMException
from pygcm.compat import urllib2, urlencode, basestring
from pygcm.base_config import SENDER_URL, DEFAULT_ENCODING


# HTTP request constants declaration
def enum(**enums):
    return type('Enum', (), enums)

method = enum(
    get = 'GET',
    post = 'POST',
    put = 'PUT',
    delete = 'DELETE',
    patch = 'PATCH'
    )

status_code = enum(
    success = 200,
    invalid_field = 400,
    auth_failed = 401,
    internal_error = 500,
    service_unavailable = 503
    )

status_group = enum(
    fail = [status_code.auth_failed,
            status_code.invalid_field],
    success = [status_code.success],
    retryable = [status_code.internal_error,
                status_code.service_unavailable]
    )


class RequestHandler(object):
    """Requests wrapper
    Handles requests holding specific configuation"""

    def __init__(self, **kwargs):
        self._url = kwargs.get('url')
        self._headers = kwargs.get('headers', {})
        self._params = kwargs.get('params', {})
        self.proxies = kwargs.get('proxies', {})

        if self.proxies:
            if isinstance(self.proxies, dict):
                urllib2.install_opener(
                    urllib2.build_opener(
                        urllib2.ProxyHandler(self.proxies)))

    @property
    def url(self):
        return self._url

    @property
    def headers(self):
        return self._headers

    @property
    def params(self):
        return self._params

    @property
    def ready(self):
        """Can add another 'ready' status """
        return self._url is not None

    def _send(self, request_type, headers=None, params=None):
        """Each send funtion sends a request.

        :param headers: should contains authorization header including api-key.
        :param params: should contains device key. (Others are options)
        """
        if request_type != method.post:
            raise GCMException("Google does not support other methods yet")

        if not self.ready:
            raise GCMException("RequestHandler is not ready to send")

        p = params or self._params
        request = urllib2.Request(self._url,
                        data=p.encode(DEFAULT_ENCODING),
                        headers=headers or self._headers)

        try:
            urllib2.urlopen(request)
        except urllib2.HTTPError as e:
            if e.code in status_group.fail:
                raise GCMException(
                    "Request failed with unexpected error : code " + e.code)
            if e.code in status_group.retryable:
                return False<commit_msg>fix for broken error msg<commit_after># -*- coding:utf-8 -*-
"""

    pygcm.request
    ~~~~~~~~~~~~~

    Request related modules.

"""

import json
from collections import Iterable
from pygcm.exceptions import GCMException
from pygcm.compat import urllib2, urlencode, basestring
from pygcm.base_config import SENDER_URL, DEFAULT_ENCODING


# HTTP request constants declaration
def enum(**enums):
    return type('Enum', (), enums)

method = enum(
    get = 'GET',
    post = 'POST',
    put = 'PUT',
    delete = 'DELETE',
    patch = 'PATCH'
    )

status_code = enum(
    success = 200,
    invalid_field = 400,
    auth_failed = 401,
    internal_error = 500,
    service_unavailable = 503
    )

status_group = enum(
    fail = [status_code.auth_failed,
            status_code.invalid_field],
    success = [status_code.success],
    retryable = [status_code.internal_error,
                status_code.service_unavailable]
    )


class RequestHandler(object):
    """Requests wrapper
    Handles requests holding specific configuation"""

    def __init__(self, **kwargs):
        self._url = kwargs.get('url')
        self._headers = kwargs.get('headers', {})
        self._params = kwargs.get('params', {})
        self.proxies = kwargs.get('proxies', {})

        if self.proxies:
            if isinstance(self.proxies, dict):
                urllib2.install_opener(
                    urllib2.build_opener(
                        urllib2.ProxyHandler(self.proxies)))

    @property
    def url(self):
        return self._url

    @property
    def headers(self):
        return self._headers

    @property
    def params(self):
        return self._params

    @property
    def ready(self):
        """Can add another 'ready' status """
        return self._url is not None

    def _send(self, request_type, headers=None, params=None):
        """Each send funtion sends a request.

        :param headers: should contains authorization header including api-key.
        :param params: should contains device key. (Others are options)
        """
        if request_type != method.post:
            raise GCMException("Google does not support other methods yet")

        if not self.ready:
            raise GCMException("RequestHandler is not ready to send")

        p = params or self._params
        request = urllib2.Request(self._url,
                        data=p.encode(DEFAULT_ENCODING),
                        headers=headers or self._headers)

        try:
            urllib2.urlopen(request)
        except urllib2.HTTPError as e:
            if e.code in status_group.fail:
                raise GCMException(
                    "Request failed with unexpected error: code " + str(e.code)
                )
            if e.code in status_group.retryable:
                return False
{'fraction_non_alphanumeric': 0.09907341411261582, 'fraction_numerical': 0.00819672131147541, 'mean_word_length': 2.1837776517300056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '3660791', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1565, 'n_words': 423}