<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-


import json
import logging

import requests

from geocodio.data import Address
from geocodio.data import Location
from geocodio.data import LocationCollection
from geocodio import exceptions

logger = logging.getLogger(__name__)

ALLOWED_FIELDS = [
    "acs-demographics",
    "acs-economics",
    "acs-families",
    "acs-housing",
    "acs-social",
    "cd",
    "cd113",
    "cd114",
    "cd115",
    "cd116",
    "census",
    "stateleg",
    "school",
    "timezone",
]


def protect_fields(f):
    def wrapper(*args, **kwargs):
        fields = kwargs.get("fields", [])
        for field in fields:
            if field not in ALLOWED_FIELDS:
                raise ValueError("'{0}' is not a valid field value".format(field))

        return f(*args, **kwargs)

    return wrapper


def error_response(response):
    """
    Raises errors matching the response code
    """
    if response.status_code >= 500:
        raise exceptions.GeocodioServerError

    elif response.status_code == 403:
        raise exceptions.GeocodioAuthError

    elif response.status_code == 422:
        raise exceptions.GeocodioDataError(response.json()["error"])

    else:
        raise exceptions.GeocodioError(
            "Unknown service error (HTTP {0})".format(response.status_code)
        )


def json_points(points):
    """
    Returns a list of points [(lat, lng)...] as a JSON formatted list of
    strings.

    >>> json_points([(1,2), (3,4)])
    '["1,2", "3,4"]'
    """
    return json.dumps(["{0},{1}".format(point[0], point[1]) for point in points])


class GeocodioClient(object):
    """
    Client connection for Geocod.io API
    """

    BASE_URL = "http://api.geocod.io/v1.3/{verb}"

    def __init__(self, key, order="lat"):
        """
        """
        self.API_KEY = key
        if order not in ("lat", "lng"):
            raise ValueError("Order but be either `lat` or `lng`")

        self.order = order

    def _req(self, method="get", verb=None, headers={}, params={}, data={}):
        """
        Method to wrap all request building

        :return: a Response object based on the specified method and request values.<commit_msg>Make the Geocodio API version configurable<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-


import json
import logging

import requests

from geocodio.data import Address
from geocodio.data import Location
from geocodio.data import LocationCollection
from geocodio import exceptions

logger = logging.getLogger(__name__)

ALLOWED_FIELDS = [
    "acs-demographics",
    "acs-economics",
    "acs-families",
    "acs-housing",
    "acs-social",
    "cd",
    "cd113",
    "cd114",
    "cd115",
    "cd116",
    "census",
    "stateleg",
    "school",
    "timezone",
]


def protect_fields(f):

    def wrapper(*args, **kwargs):
        fields = kwargs.get("fields", [])
        for field in fields:
            if field not in ALLOWED_FIELDS:
                raise ValueError("'{0}' is not a valid field value".format(field))

        return f(*args, **kwargs)

    return wrapper


def error_response(response):
    """
    Raises errors matching the response code
    """
    if response.status_code >= 500:
        raise exceptions.GeocodioServerError

    elif response.status_code == 403:
        raise exceptions.GeocodioAuthError

    elif response.status_code == 422:
        raise exceptions.GeocodioDataError(response.json()["error"])

    else:
        raise exceptions.GeocodioError(
            "Unknown service error (HTTP {0})".format(response.status_code)
        )


def json_points(points):
    """
    Returns a list of points [(lat, lng)...] as a JSON formatted list of
    strings.

    >>> json_points([(1,2), (3,4)])
    '["1,2", "3,4"]'
    """
    return json.dumps(["{0},{1}".format(point[0], point[1]) for point in points])


class GeocodioClient(object):
    """
    Client connection for Geocod.io API
    """

    def __init__(self, key, order="lat", version="1.3"):
        """
        """
        self.BASE_URL = "http://api.geocod.io/v{version}/{{verb}}".format(
            version=version
        )
        self.API_KEY = key
        if order not in ("lat", "lng"):
            raise ValueError("Order but be either `lat` or `lng`")

        self.order = order

    def _req(self, method="get", verb=None, headers={}, params={}, data={}):
        """
        Method to wrap all request building

        :return: a Response object based on the specified method and request values.
{'fraction_non_alphanumeric': 0.13751375137513752, 'fraction_numerical': 0.016721672167216723, 'mean_word_length': 2.6455493183640737, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '4181914', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1409, 'n_words': 378}