# Path: wham/apis/twitter/twitter_bearer_auth.py
# class BearerAuth(requests.auth.AuthBase):
#     """Request bearer access token for oAuth2 authentication.
# 
#     :param consumer_key: Twitter application consumer key
#     :param consumer_secret: Twitter application consumer secret
#     :param proxies: Dictionary of proxy URLs (see documentation for python-requests).
#     """
# 
#     def __init__(self, consumer_key, consumer_secret, proxies=None):
#         self._consumer_key = consumer_key
#         self._consumer_secret = consumer_secret
#         self.proxies = proxies
#         self._bearer_token = self._get_access_token()
# 
#     def _get_access_token(self):
#         token_url = '%s://%s.%s/%s' % (PROTOCOL,
#                                        REST_SUBDOMAIN,
#                                        DOMAIN,
#                                        OAUTH2_TOKEN_ENDPOINT)
#         auth = self._consumer_key + ':' + self._consumer_secret
#         b64_bearer_token_creds = base64.b64encode(auth.encode('utf8'))
#         params = {'grant_type': 'client_credentials'}
#         headers = {}
#         headers['User-Agent'] = USER_AGENT
#         headers['Authorization'] = 'Basic ' + \
#                                    b64_bearer_token_creds.decode('utf8')
#         headers[
#             'Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
#         try:
#             response = requests.post(
#                 token_url,
#                 params=params,
#                 headers=headers,
#                 proxies=self.proxies)
# 
#             data = response.json()
#             return data['access_token']
#         except Exception as e:
#             raise Exception(
#                 'Error while requesting bearer access token: %s' %
#                 e)
# 
#     def __call__(self, r):
#         auth_list = [
#             self._consumer_key,
#             self._consumer_secret,
#             self._bearer_token]
#         if all(auth_list):
#             r.headers['Authorization'] = "Bearer %s" % self._bearer_token
#             return r
#         else:
#             raise Exception('Not enough keys passed to Bearer token manager.')
# 
# Path: wham/fields.py
# class WhamDateTimeField(WhamFieldMixin, models.DateTimeField):
# 
#     def __init__(self, *args, **kwargs):
#         self.wham_format = kwargs.pop('wham_format', None)
#         return super(WhamDateTimeField, self).__init__(*args, **kwargs)
# 
# class WhamForeignKey(models.ForeignKey):
# 
#     def __init__(self, *args, **kwargs):
#         self.wham_result_path = kwargs.pop('wham_result_path', None)
#         self.wham_endpoint = kwargs.pop('wham_endpoint', None)
#         self.wham_results_path = kwargs.pop('wham_results_path', ())
#         self.wham_pk_param = kwargs.pop('wham_pk_param', None)
#         self.wham_params = kwargs.pop('wham_params', {})
#         return super(WhamForeignKey, self).__init__(*args, **kwargs)
# 
#     def get_result_path(self):
#         result_path = self.wham_result_path
#         if not result_path:
#             return (self.name,)
#         else:
#             return result_path
# 
#     @property
#     def type_repr(self):
#         return 'foreign key'


from copy import deepcopy
from urllib import urlencode
from django.conf import settings
from django.db.models import ForeignKey
from django.template import Template, Context
from django.template.loader import render_to_string
from datetime import datetime
from django.utils import timezone
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from wham.apis.twitter.twitter_bearer_auth import BearerAuth as TwitterBearerAuth
from wham.fields import WhamDateTimeField, WhamForeignKey
import requests
import time
import string
                                                 wham_meta_search_attributes['defaults'])


    @property
    def is_many_related_manager(self):
        ducktype_attributes = ('_add_items', '_clear_items', '_remove_items')
        for attr in ducktype_attributes:
            if not hasattr(self, attr):
                return False
        return True

    def get_api_key(self):
        return getattr(settings, self.wham_meta.api_key_settings_name)

    def add_auth_params(self, params):
        if self._wham_meta.auth_for_public_get == 'API_KEY':
            params[self.api_key_param] = self.get_api_key()
        if self._wham_meta.requires_oauth_token:
            params['token'] = self.get_oauth_token()





    def make_get_request(self, url_tail, params=None, fetch_live=False, depth=1):

        session = requests.Session()
        if self._wham_meta.auth_for_public_get == 'TWITTER':
            twitter_auth = getattr(settings, 'twitter_auth', None)
            if not twitter_auth:

# Path: wham/apis/twitter/twitter_bearer_auth.py
# class BearerAuth(requests.auth.AuthBase):
#     """Request bearer access token for oAuth2 authentication.
# 
#     :param consumer_key: Twitter application consumer key
#     :param consumer_secret: Twitter application consumer secret
#     :param proxies: Dictionary of proxy URLs (see documentation for python-requests).
#     """
# 
#     def __init__(self, consumer_key, consumer_secret, proxies=None):
#         self._consumer_key = consumer_key
#         self._consumer_secret = consumer_secret
#         self.proxies = proxies
#         self._bearer_token = self._get_access_token()
# 
#     def _get_access_token(self):
#         token_url = '%s://%s.%s/%s' % (PROTOCOL,
#                                        REST_SUBDOMAIN,
#                                        DOMAIN,
#                                        OAUTH2_TOKEN_ENDPOINT)
#         auth = self._consumer_key + ':' + self._consumer_secret
#         b64_bearer_token_creds = base64.b64encode(auth.encode('utf8'))
#         params = {'grant_type': 'client_credentials'}
#         headers = {}
#         headers['User-Agent'] = USER_AGENT
#         headers['Authorization'] = 'Basic ' + \
#                                    b64_bearer_token_creds.decode('utf8')
#         headers[
#             'Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
#         try:
#             response = requests.post(
#                 token_url,
#                 params=params,
#                 headers=headers,
#                 proxies=self.proxies)
# 
#             data = response.json()
#             return data['access_token']
#         except Exception as e:
#             raise Exception(
#                 'Error while requesting bearer access token: %s' %
#                 e)
# 
#     def __call__(self, r):
#         auth_list = [
#             self._consumer_key,
#             self._consumer_secret,
#             self._bearer_token]
#         if all(auth_list):
#             r.headers['Authorization'] = "Bearer %s" % self._bearer_token
#             return r
#         else:
#             raise Exception('Not enough keys passed to Bearer token manager.')
# 
# Path: wham/fields.py
# class WhamDateTimeField(WhamFieldMixin, models.DateTimeField):
# 
#     def __init__(self, *args, **kwargs):
#         self.wham_format = kwargs.pop('wham_format', None)
#         return super(WhamDateTimeField, self).__init__(*args, **kwargs)
# 
# class WhamForeignKey(models.ForeignKey):
# 
#     def __init__(self, *args, **kwargs):
#         self.wham_result_path = kwargs.pop('wham_result_path', None)
#         self.wham_endpoint = kwargs.pop('wham_endpoint', None)
#         self.wham_results_path = kwargs.pop('wham_results_path', ())
#         self.wham_pk_param = kwargs.pop('wham_pk_param', None)
#         self.wham_params = kwargs.pop('wham_params', {})
#         return super(WhamForeignKey, self).__init__(*args, **kwargs)
# 
#     def get_result_path(self):
#         result_path = self.wham_result_path
#         if not result_path:
#             return (self.name,)
#         else:
#             return result_path
# 
#     @property
#     def type_repr(self):
#         return 'foreign key'


# Path: wham/models.py
from copy import deepcopy
from urllib import urlencode
from django.conf import settings
from django.db.models import ForeignKey
from django.template import Template, Context
from django.template.loader import render_to_string
from datetime import datetime
from django.utils import timezone
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from wham.apis.twitter.twitter_bearer_auth import BearerAuth as TwitterBearerAuth
from wham.fields import WhamDateTimeField, WhamForeignKey
import requests
import time
import string

                                                 wham_meta_search_attributes['defaults'])


    @property
    def is_many_related_manager(self):
        ducktype_attributes = ('_add_items', '_clear_items', '_remove_items')
        for attr in ducktype_attributes:
            if not hasattr(self, attr):
                return False
        return True

    def get_api_key(self):
        return getattr(settings, self.wham_meta.api_key_settings_name)

    def add_auth_params(self, params):
        if self._wham_meta.auth_for_public_get == 'API_KEY':
            params[self.api_key_param] = self.get_api_key()
        if self._wham_meta.requires_oauth_token:
            params['token'] = self.get_oauth_token()





    def make_get_request(self, url_tail, params=None, fetch_live=False, depth=1):

        session = requests.Session()
        if self._wham_meta.auth_for_public_get == 'TWITTER':
            twitter_auth = getattr(settings, 'twitter_auth', None)
            if not twitter_auth:

                settings.twitter_auth = TwitterBearerAuth(settings.TWITTER_API_KEY, settings.TWITTER_API_SECRET)