<commit_before>#!/usr/bin/env python

# Copyright 2011 <NAME>
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Generic REST client
"""

__author__    = "<NAME> <<EMAIL>>"
__copyright__ = "Copyright 2011 Nigel Small"
__license__   = "Apache License, Version 2.0"


from tornado import httpclient, curl_httpclient

try:
    import json
except ImportError:
    import simplejson as json


class Resource(object):
    """
    RESTful web service resource class, designed to work with a well-behaved
    hypermedia web service.

    :param uri:           the URI identifying this resource
    :param content_type:  the content type required for data exchange
    :param index:         a previously obtained resource index for endpoint discovery
    :param http:          HTTP object to use for requests
    """

    SUPPORTED_CONTENT_TYPES = ['application/json']

    def __init__(self, uri, content_type='application/json', index=None, http=None, **request_params):
        if content_type not in self.SUPPORTED_CONTENT_TYPES:
            raise NotImplementedError("Content type {0} not supported".format(content_type))
        self._uri = uri
        self._base_uri = None
        self._relative_uri = None
        self._content_type = content_type
        self._http = http or httpclient.HTTPClient(curl_httpclient.CurlAsyncHTTPClient)
        self._request_params = {"user_agent": "py2neo"}
        self._request_params.update(request_params)
        self._index = index

    def __repr__(self):
        """
        Return a valid Python representation of this object.
        """
        return '%s(%s)' % (self.__class__.__name__, repr(self._uri))

    def __eq__(self, other):
        """
        Determine equality of two objects based on URI.
        """
        return self._uri == other._uri

    def __ne__(self, other):
        """
        Determine inequality of two objects based on URI.
        """
        return self._uri != other._uri

    def _spawn(self, class_, *args, **kwargs):
        """
        Spawn a new resource, reusing HTTP connection.
        """
        k = {"http": self._http}
        k.update(self._request_params)
        k.update(kwargs)
        return class_(*args, **k)
    
    def __get_request_headers(self, *keys):
        return dict([
            (key, self._content_type)
            for key in keys
            if key in ['Accept', 'Content-Type']
        ])

    def _request(self, method, uri, data=None, **request_params):
        """
        Issue an HTTP request.
        
        :param method: the HTTP method to use for this call
        :param uri: the URI of the resource to access
        :param data: raw data to be passed in request payload (optional)
        :return: object created from returned content (200), C{Location} header value (201) or C{None} (204)
        :raise ValueError: when supplied data is not appropriate (400)
        :raise KeyError: when URI is not found (404)
        :raise SystemError: when a conflict occurs (409) or when an unexpected HTTP status code is received
        """
        if data is not None:
            headers = self.__get_request_headers('Accept', 'Content-Type')
        else:
            headers = self.__get_request_headers('Accept')
        try:
            params = self._request_params.copy()
            params.update(request_params)
            params.update({
                "method": method,
                "headers": headers,
                "body": data
            })
            self.__response = self._http.fetch(uri, **params)
            # for py3k compatibility...
#            if not isinstance(self.__content, str):
#                self.__content = self.__content.decode()
        except:<commit_msg>added request count<commit_after>#!/usr/bin/env python

# Copyright 2011 <NAME>
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Generic REST client
"""

__author__    = "<NAME> <<EMAIL>>"
__copyright__ = "Copyright 2011 Nigel Small"
__license__   = "Apache License, Version 2.0"


from tornado import httpclient, curl_httpclient

try:
    import json
except ImportError:
    import simplejson as json


class Resource(object):
    """
    RESTful web service resource class, designed to work with a well-behaved
    hypermedia web service.

    :param uri:           the URI identifying this resource
    :param content_type:  the content type required for data exchange
    :param index:         a previously obtained resource index for endpoint discovery
    :param http:          HTTP object to use for requests
    """

    SUPPORTED_CONTENT_TYPES = ['application/json']

    def __init__(self, uri, content_type='application/json', index=None, http=None, **request_params):
        if content_type not in self.SUPPORTED_CONTENT_TYPES:
            raise NotImplementedError("Content type {0} not supported".format(content_type))
        self._uri = uri
        self._base_uri = None
        self._relative_uri = None
        self._content_type = content_type
        self._http = http or httpclient.HTTPClient(curl_httpclient.CurlAsyncHTTPClient)
        self._request_params = {"user_agent": "py2neo"}
        self._request_params.update(request_params)
        self._index = index
        self.__request_count = 0

    def __repr__(self):
        """
        Return a valid Python representation of this object.
        """
        return '%s(%s)' % (self.__class__.__name__, repr(self._uri))

    def __eq__(self, other):
        """
        Determine equality of two objects based on URI.
        """
        return self._uri == other._uri

    def __ne__(self, other):
        """
        Determine inequality of two objects based on URI.
        """
        return self._uri != other._uri

    def _spawn(self, class_, *args, **kwargs):
        """
        Spawn a new resource, reusing HTTP connection.
        """
        k = {"http": self._http}
        k.update(self._request_params)
        k.update(kwargs)
        return class_(*args, **k)
    
    def __get_request_headers(self, *keys):
        return dict([
            (key, self._content_type)
            for key in keys
            if key in ['Accept', 'Content-Type']
        ])

    def _request(self, method, uri, data=None, **request_params):
        """
        Issue an HTTP request.
        
        :param method: the HTTP method to use for this call
        :param uri: the URI of the resource to access
        :param data: raw data to be passed in request payload (optional)
        :return: object created from returned content (200), C{Location} header value (201) or C{None} (204)
        :raise ValueError: when supplied data is not appropriate (400)
        :raise KeyError: when URI is not found (404)
        :raise SystemError: when a conflict occurs (409) or when an unexpected HTTP status code is received
        """
        if data is not None:
            headers = self.__get_request_headers('Accept', 'Content-Type')
        else:
            headers = self.__get_request_headers('Accept')
        try:
            params = self._request_params.copy()
            params.update(request_params)
            params.update({
                "method": method,
                "headers": headers,
                "body": data
            })
            self.__response = self._http.fetch(uri, **params)
            self.__request_count += 1
            # for py3k compatibility...
#            if not isinstance(self.__content, str):
#                self.__content = self.__content.decode()
        except:
{'fraction_non_alphanumeric': 0.09146270055041573, 'fraction_numerical': 0.008431900690947418, 'mean_word_length': 2.4228456913827654, 'pattern_counts': {'":': 10, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, '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': '6230154', 'n_tokens_mistral': 2317, 'n_tokens_neox': 2226, 'n_words': 807}