<commit_before>"""Python wrapper for Breeze ChMS API: http://www.breezechms.com/api

This API wrapper allows churches to build custom functionality integrated with
Breeze Church Management System.

Usage:
  from breeze import breeze

  breeze_api = breeze.BreezeApi(
      breeze_url='https://demo.breezechms.com',
      api_key='5c2d2cbacg3...')
  people = breeze_api.GetPeople();

  for person in people:
    print '%s %s' % (person['first_name'], person['last_name'])
"""

__author__ = '<EMAIL> (<NAME>)'

import logging
import requests

from utils import make_enum

ENDPOINTS = make_enum(
    'BreezeApiURL',
    PEOPLE='/api/people',
    EVENTS='/api/events',
    PROFILE_FIELDS='/api/profile',
    CONTRIBUTIONS='/api/giving',
    FUNDS='/api/funds',
    PLEDGES='/api/pledges')


class BreezeError(Exception):
    """Exception for BreezeApi."""
    pass


class BreezeApi(object):
    """A wrapper for the Breeze REST API."""

    def __init__(self, breeze_url, api_key,
                 dry_run=False,
                 connection=requests.Session()):
        """Instantiates the BreezeApi with your Breeze account information.

        Args:
          breeze_url: Fully qualified domain for your organizations Breeze
                      service.
          api_key: Unique Breeze API key. For instructions on finding your
                   organizations API key, see:
                   http://breezechms.com/docs#extensions_api
          dry_run: Enable no-op mode, which disables requests from being made.
                   When combined with debug, this allows debugging requests
                   without affecting data in your Breeze account."""

        self.breeze_url = breeze_url
        self.api_key = api_key
        self.dry_run = dry_run
        self.connection = connection

        # TODO(alex): use urlparse to check url format.
        if not (self.breeze_url and self.breeze_url.startswith('https://') and
                self.breeze_url.find('.breezechms.')):
            raise BreezeError('You must provide your breeze_url as ',
                              'subdomain.breezechms.com')

        if not self.api_key:
            raise BreezeError('You must provide an API key.')

    def _request(self, endpoint, params=None, headers=None, timeout=60):
        """Makes an HTTP request to a given url.

        Args:
          endpoint: URL where the service can be accessed.
          params: Query parameters to append to endpoint url.
          headers: HTTP headers; used for authenication parameters.
          timeout: Timeout in seconds for HTTP request.

        Returns:
          HTTP response

        Throws:
          BreezeError if connection or request fails."""
        headers.update({
          'Content-Type': 'application/json',
          'Api-Key': self.api_key}
          )

        if params is None:
            params = {}
        keywords = dict(params=params, headers=headers, timeout=timeout)
        url = '%s%s' % (self.breeze_url, endpoint)

        logging.debug('Making request to %s', url)
        if self.dry_run:
            return
<commit_msg>Check for headers being None.<commit_after>"""Python wrapper for Breeze ChMS API: http://www.breezechms.com/api

This API wrapper allows churches to build custom functionality integrated with
Breeze Church Management System.

Usage:
  from breeze import breeze

  breeze_api = breeze.BreezeApi(
      breeze_url='https://demo.breezechms.com',
      api_key='5c2d2cbacg3...')
  people = breeze_api.GetPeople();

  for person in people:
    print '%s %s' % (person['first_name'], person['last_name'])
"""

__author__ = '<EMAIL> (<NAME>)'

import logging
import requests

from utils import make_enum

ENDPOINTS = make_enum(
    'BreezeApiURL',
    PEOPLE='/api/people',
    EVENTS='/api/events',
    PROFILE_FIELDS='/api/profile',
    CONTRIBUTIONS='/api/giving',
    FUNDS='/api/funds',
    PLEDGES='/api/pledges')


class BreezeError(Exception):
    """Exception for BreezeApi."""
    pass


class BreezeApi(object):
    """A wrapper for the Breeze REST API."""

    def __init__(self, breeze_url, api_key,
                 dry_run=False,
                 connection=requests.Session()):
        """Instantiates the BreezeApi with your Breeze account information.

        Args:
          breeze_url: Fully qualified domain for your organizations Breeze
                      service.
          api_key: Unique Breeze API key. For instructions on finding your
                   organizations API key, see:
                   http://breezechms.com/docs#extensions_api
          dry_run: Enable no-op mode, which disables requests from being made.
                   When combined with debug, this allows debugging requests
                   without affecting data in your Breeze account."""

        self.breeze_url = breeze_url
        self.api_key = api_key
        self.dry_run = dry_run
        self.connection = connection

        # TODO(alex): use urlparse to check url format.
        if not (self.breeze_url and self.breeze_url.startswith('https://') and
                self.breeze_url.find('.breezechms.')):
            raise BreezeError('You must provide your breeze_url as ',
                              'subdomain.breezechms.com')

        if not self.api_key:
            raise BreezeError('You must provide an API key.')

    def _request(self, endpoint, params=None, headers=None, timeout=60):
        """Makes an HTTP request to a given url.

        Args:
          endpoint: URL where the service can be accessed.
          params: Query parameters to append to endpoint url.
          headers: HTTP headers; used for authenication parameters.
          timeout: Timeout in seconds for HTTP request.

        Returns:
          HTTP response

        Throws:
          BreezeError if connection or request fails."""
        if headers is None:
            headers = {}
        headers.update({
          'Content-Type': 'application/json',
          'Api-Key': self.api_key}
          )

        if params is None:
            params = {}
        keywords = dict(params=params, headers=headers, timeout=timeout)
        url = '%s%s' % (self.breeze_url, endpoint)

        logging.debug('Making request to %s', url)
        if self.dry_run:
            return

{'fraction_non_alphanumeric': 0.09486731288733513, 'fraction_numerical': 0.0019068806610519624, 'mean_word_length': 2.2730109204368176, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '2174223', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1756, 'n_words': 533}