<commit_before>import urllib
import httplib
import base64
import json
import logging

from request import encode_multipart_formdata, encode_urlencode

class OAuth2:

    def __init__(self, access_token, **kwargs):

        self.access_token = access_token

        self.hostname = kwargs.get('hostname', 'api.collection.cooperhewitt.org')
        self.endpoint = kwargs.get('endpoint', '/rest')

        logging.debug("setup API to use %s%s" % (self.hostname, self.endpoint))

        # Strictly speaking not ever necessary until of course it
        # is like when I need to call a dev server or whatever.
        # This can safely be ignored most of the time.
        # (20130403/straup)

        self.username = kwargs.get('username', None)
        self.password = kwargs.get('password', None)

        # Same same - you should only ever talk to OAuth2 using HTTP
        # (20130403/straup)

        self.use_https = kwargs.get('use_https', True)

    def execute_method(self, method, kwargs, encode=encode_urlencode):

        logging.debug("calling %s with args %s" % (method, kwargs))

        kwargs['method'] = method
        kwargs['access_token'] = self.access_token
        
        (headers, body) = encode(kwargs)

        url = self.endpoint + '/'
        logging.debug("calling %s%s" % (self.hostname, url))

        conn = httplib.HTTPSConnection(self.hostname)
        conn.request('POST', url, body, headers)

        rsp = conn.getresponse()
        body = rsp.read()

        logging.debug("response is %s" % body)

        try:
            data = json.loads(body)
        except Exception, e:

            logging.error(e)
            logging.debug(body)
            
            error = { 'code': 000, 'message': 'failed to parse JSON', 'details': body }
            data = { 'stat': 'error', 'error': error }

        # check status here...

        return data

    def call (self, method, **kwargs):
        logging.warning("The 'call' method is deprecated. Please use 'execute_method' instead.")
        self.execute_method(method, kwargs)

if __name__ == '__main__':

    import sys
    import pprint<commit_msg>first pass at requests.py implementation; basic POST stuff works<commit_after>import urllib
import httplib
import base64
import json
import logging

from request import encode_multipart_formdata, encode_urlencode

import requests

class OAuth2:

    def __init__(self, access_token, **kwargs):

        self.access_token = access_token

        self.hostname = kwargs.get('hostname', 'api.collection.cooperhewitt.org')
        self.endpoint = kwargs.get('endpoint', '/rest')
        self.proxy = kwargs.get('proxy', None)
        
        logging.debug("setup API to use %s%s" % (self.hostname, self.endpoint))

        if self.proxy:
            logging.debug("setup API to use proxy %s" % self.proxy)

        # Strictly speaking not ever necessary until of course it
        # is like when I need to call a dev server or whatever.
        # This can safely be ignored most of the time.
        # (20130403/straup)

        self.username = kwargs.get('username', None)
        self.password = kwargs.get('password', None)

    def execute_method(self, method, data, encode=encode_urlencode):

        logging.debug("calling %s with args %s" % (method, data))

        data['method'] = method
        data['access_token'] = self.access_token
        
        url = "https://" + self.hostname + self.endpoint + '/'
        logging.debug("calling %s" % url)

        more = {}

        if self.proxy:
            more["proxies"] = {"https": self.proxy }

        rsp = requests.post(url, data=data, **more)
        body = rsp.text

        logging.debug("response is %s" % body)

        try:
            data = json.loads(body)
        except Exception, e:

            logging.error(e)
            logging.debug(body)
            
            error = { 'code': 000, 'message': 'failed to parse JSON', 'details': body }
            data = { 'stat': 'error', 'error': error }

        # check status here...

        return data

    def call (self, method, **kwargs):
        logging.warning("The 'call' method is deprecated. Please use 'execute_method' instead.")
        self.execute_method(method, kwargs)

if __name__ == '__main__':

    import sys
    import pprint
{'fraction_non_alphanumeric': 0.1148491879350348, 'fraction_numerical': 0.008584686774941995, 'mean_word_length': 2.3811764705882354, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '4120147', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1227, 'n_words': 362}