<commit_before>import requests

from . import models, exceptions, util


class API(object):
    def __init__(self, key=None, secret=None):
        self.key = key
        self.secret = secret

        self.spaces = self.bind(
            uri='/v1/spaces.json',
            model=models.Space)

        self.space = self.bind(
            uri='/v1/spaces/{id}.json',
            model=models.Space)

        self.tickets = self.bind(
            uri=['/v1/spaces/{space_id}/tickets.json', '/v1/spaces/{space_id}/tickets/milestone/{milestone_id}.json'],
            model=models.Ticket)

        self.ticket_status = self.bind(
            uri='/v1/spaces/{space_id}/tickets/statuses/{id}',
            model=models.TicketStatus)

        self.milestones = self.bind(
            uri=['/v1/spaces/{space_id}/milestones/upcoming'],
            model=models.Milestone)

        self.user = self.bind(
            uri=['/v1/users/{id}.json', '/v1/users/{login}.json'],
            model=models.User)

        self.users = self.bind(
            uri='/v1/spaces/{space_id}/users.json',
            model=models.User)

    def _validate(self, uri_list):
        all_params = []
        for uri in uri_list:
            params = util.uri_params(uri)
            if len(set(params)) != len(params):
                raise exceptions.URIError('Duplicate params')

            if set(params) in all_params:
                raise exceptions.URIError('More than 1 URI with the same params')
            all_params.append(set(params))

    def _check_uri(self, uri, fields):
        params = util.uri_params(uri)

        if len(params) != len(fields):
            return False

        for param in params:
            if param not in fields:
                return False
        return True

    def _fetch(self, uri, model=None):
        def do_fetch():
            headers = {'X-Api-Key': self.key, 'X-Api-Secret': self.secret}
            response = requests.get('https://api.assembla.com{0}'.format(uri), headers=headers)

            if response.status_code == 401:
                raise exceptions.AuthenticationError('Authentication Failed')

            return self, response.json()

        if model is None:
            return do_fetch

        _, json = do_fetch()
        if isinstance(json, list):
            return model.instantiate_many(json, self)
        return model.instantiate_one(json, self)

    def bind(self, **config):
        uri_list = config['uri'] if isinstance(config['uri'], list) else [config['uri']]
        self._validate(uri_list)

        def handler(**kwargs):
            lazy = kwargs.get('lazy', False)
            if 'lazy' in kwargs.keys():
                del kwargs['lazy']

            if not isinstance(config['uri'], list):
                config['uri'] = [config['uri']]

            try:
                uri = next(uri for uri in config['uri'] if self._check_uri(uri, kwargs.keys()))
                uri = uri.format(**kwargs)
            except StopIteration:
                raise exceptions.ParamCountError('No URI matching the passed params')

            if lazy:
                model = config['model']
                return model(lazy_load=self._fetch(uri))
            return self._fetch(uri, model=config['model'])

        return handler
<commit_msg>All tickets should be returned.<commit_after>import requests

from . import models, exceptions, util


class API(object):
    def __init__(self, key=None, secret=None):
        self.key = key
        self.secret = secret

        self.spaces = self.bind(
            uri='/v1/spaces.json',
            model=models.Space)

        self.space = self.bind(
            uri='/v1/spaces/{id}.json',
            model=models.Space)

        self.tickets = self.bind(
            uri=['/v1/spaces/{space_id}/tickets.json', '/v1/spaces/{space_id}/tickets/milestone/{milestone_id}.json?ticket_status=all'],
            model=models.Ticket)

        self.ticket_status = self.bind(
            uri='/v1/spaces/{space_id}/tickets/statuses/{id}',
            model=models.TicketStatus)

        self.milestones = self.bind(
            uri=['/v1/spaces/{space_id}/milestones/upcoming'],
            model=models.Milestone)

        self.user = self.bind(
            uri=['/v1/users/{id}.json', '/v1/users/{login}.json'],
            model=models.User)

        self.users = self.bind(
            uri='/v1/spaces/{space_id}/users.json',
            model=models.User)

    def _validate(self, uri_list):
        all_params = []
        for uri in uri_list:
            params = util.uri_params(uri)
            if len(set(params)) != len(params):
                raise exceptions.URIError('Duplicate params')

            if set(params) in all_params:
                raise exceptions.URIError('More than 1 URI with the same params')
            all_params.append(set(params))

    def _check_uri(self, uri, fields):
        params = util.uri_params(uri)

        if len(params) != len(fields):
            return False

        for param in params:
            if param not in fields:
                return False
        return True

    def _fetch(self, uri, model=None):
        def do_fetch():
            headers = {'X-Api-Key': self.key, 'X-Api-Secret': self.secret}
            response = requests.get('https://api.assembla.com{0}'.format(uri), headers=headers)

            if response.status_code == 401:
                raise exceptions.AuthenticationError('Authentication Failed')

            return self, response.json()

        if model is None:
            return do_fetch

        _, json = do_fetch()
        if isinstance(json, list):
            return model.instantiate_many(json, self)
        return model.instantiate_one(json, self)

    def bind(self, **config):
        uri_list = config['uri'] if isinstance(config['uri'], list) else [config['uri']]
        self._validate(uri_list)

        def handler(**kwargs):
            lazy = kwargs.get('lazy', False)
            if 'lazy' in kwargs.keys():
                del kwargs['lazy']

            if not isinstance(config['uri'], list):
                config['uri'] = [config['uri']]

            try:
                uri = next(uri for uri in config['uri'] if self._check_uri(uri, kwargs.keys()))
                uri = uri.format(**kwargs)
            except StopIteration:
                raise exceptions.ParamCountError('No URI matching the passed params')

            if lazy:
                model = config['model']
                return model(lazy_load=self._fetch(uri))
            return self._fetch(uri, model=config['model'])

        return handler

{'fraction_non_alphanumeric': 0.12654600301659125, 'fraction_numerical': 0.004223227752639517, 'mean_word_length': 1.9108867427568041, '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': '4352569', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1838, 'n_words': 406}