<commit_before>"""
Created on 8 Nov 2012

@author: plish
"""

import json
from httplib2 import Http

try:
    from urllib import urlencode
except ImportError:
    from urllib.parse import urlencode

from trolly.organisation import Organisation
from trolly.board import Board
from trolly.list import List
from trolly.card import Card
from trolly.checklist import Checklist
from trolly.member import Member

from trolly import Unauthorised, ResourceUnavailable


class Client(object):
    """
    A class that has all the logic for communicating with Trello and returning
    information to the user
    """

    def __init__(self, api_key, user_auth_token=None):
        """
        Takes the API key and User Auth Token, which are needed for all Trello
        API calls to allow access to requested information
        """
        self.api_key = api_key
        self.user_auth_token = user_auth_token

        self.client = Http()

    def add_authorisation(self, query_params):
        """
        Adds the API key and user auth token to the query parameters
        """
        query_params['key'] = self.api_key

        if self.user_auth_token:
            query_params['token'] = self.user_auth_token

        return query_params

    def clean_path(self, path):
        """
        Ensure the path has a preceding /
        """
        if path[0] != '/':
            path = '/' + path
        return path

    def check_errors(self, uri, response):
        """
        Check HTTP reponse for known errors
        """
        if response.status == 401:
            raise Unauthorised(uri, response)

        if response.status != 200:
            raise ResourceUnavailable(uri, response)

    def build_uri(self, path, query_params):
        """
        Build the URI for the API call.
        """
        url = 'https://api.trello.com/1' + self.cleanPath(path)
        url += '?' + urlencode(query_params)

        return url

    def fetch_json(self, uri_path, http_method='GET', query_params={}, body=None, headers={}):
        """
        Make a call to Trello API and capture JSON response. Raises an error
        when it fails.
        """
        query_params = self.add_authorisation(query_params)
        uri = self.build_uri(uri_path, query_params)

        if http_method in ( "POST", "PUT", "DELETE" ) and 'Content-Type' not in headers:
            headers['Content-Type'] = 'application/json'

        headers['Accept'] = 'application/json'
        response, content = self.client.request(
            uri=uri,
            method=http_method,
            body=body,
            headers=headers
        )

        self.check_errors(uri, response)

        return json.loads(content.decode())
<commit_msg>updated parameters and fixed some more pep8<commit_after>"""
Created on 8 Nov 2012

@author: plish
"""

import json
from httplib2 import Http

try:
    from urllib import urlencode
except ImportError:
    from urllib.parse import urlencode

from trolly.organisation import Organisation
from trolly.board import Board
from trolly.list import List
from trolly.card import Card
from trolly.checklist import Checklist
from trolly.member import Member

from trolly import Unauthorised, ResourceUnavailable


class Client(object):
    """
    A class that has all the logic for communicating with Trello and returning
    information to the user
    """

    def __init__(self, api_key, user_auth_token=None):
        """
        Takes the API key and User Auth Token, which are needed for all Trello
        API calls to allow access to requested information
        """
        self.api_key = api_key
        self.user_auth_token = user_auth_token

        self.client = Http()

    def add_authorisation(self, query_params):
        """
        Adds the API key and user auth token to the query parameters
        """
        query_params['key'] = self.api_key

        if self.user_auth_token:
            query_params['token'] = self.user_auth_token

        return query_params

    def clean_path(self, path):
        """
        Ensure the path has a preceding /
        """
        if path[0] != '/':
            path = '/' + path
        return path

    def check_errors(self, uri, response):
        """
        Check HTTP reponse for known errors
        """
        if response.status == 401:
            raise Unauthorised(uri, response)

        if response.status != 200:
            raise ResourceUnavailable(uri, response)

    def build_uri(self, path, query_params):
        """
        Build the URI for the API call.
        """
        url = 'https://api.trello.com/1' + self.cleanPath(path)
        url += '?' + urlencode(query_params)

        return url

    def fetch_json(self, uri_path, http_method='GET', query_params=None, body=None, headers=None):
        """
        Make a call to Trello API and capture JSON response. Raises an error
        when it fails.
        """
        query_params = query_params or {}
        headers = headers or {}

        query_params = self.add_authorisation(query_params)
        uri = self.build_uri(uri_path, query_params)

        if http_method in ("POST", "PUT", "DELETE") and 'Content-Type' not in headers:
            headers['Content-Type'] = 'application/json'

        headers['Accept'] = 'application/json'
        response, content = self.client.request(
            uri=uri,
            method=http_method,
            body=body,
            headers=headers
        )

        self.check_errors(uri, response)

        return json.loads(content.decode())

{'fraction_non_alphanumeric': 0.0889609220241311, 'fraction_numerical': 0.005222402305060328, 'mean_word_length': 2.3138424821002386, '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': '6671456', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1525, 'n_words': 486}