<commit_before>from requests.utils import parse_header_links

from .sentry_api_client.api import get_api_instance


class Patrol:

    def __init__(self, sentry_api_token, timeout=None):
        self.api = get_api_instance(sentry_api_token, timeout)

    def events(self, organization, project):
        all_events = []
        events = self.api.project_events.list(organization, project)
        all_events.extend(events.body)

        links = parse_header_links(events.headers.get('Link'))
        while links[1]['results'] == 'true':
            events = self.api.project_events.list(organization, project)
            all_events.extend(events.body)

        return all_events

    def issues(self, organization, project):
        all_issues = []
        issues = self.api.project_issues.list(organization, project)
        all_issues.extend(issues.body)

        links = parse_header_links(issues.headers.get('Link'))
        while links[1]['results'] == 'true':
            issues = self.api.project_issues.list(organization, project)
            all_issues.extend(issues.body)

        return all_issues<commit_msg>Use generator on patrol + Fix pagination (#11)<commit_after>import requests

from .sentry_api_client.api import get_api_instance


class Patrol:

    def __init__(self, sentry_api_token, timeout=None):
        self.headers = {
            'Authorization': 'Bearer {}'.format(sentry_api_token)
        }
        self.timeout = timeout
        self.api = get_api_instance(sentry_api_token, timeout)

    def _fetch_resources(self, endpoint, organization, project):
        endpoint = getattr(self.api, endpoint)
        method = getattr(endpoint, 'list')

        resources = method(organization, project)
        yield from resources.body

        next_link = resources.client_response.links['next']
        while next_link['results'] == 'true':
            response = requests.get(next_link['url'], timeout=self.timeout, headers=self.headers)
            yield from response.json()
            next_link = response.links['next']

    def events(self, organization, project):
        return self._fetch_resources('project_events', organization, project)

    def issues(self, organization, project):
        return self._fetch_resources('project_issues', organization, project)
{'fraction_non_alphanumeric': 0.10448410970831519, 'fraction_numerical': 0.0017414018284719198, 'mean_word_length': 3.224264705882353, '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': '61077', 'n_tokens_mistral': 645, 'n_tokens_neox': 619, 'n_words': 130}