<commit_before>__version__ = "dev"

__author__ = "<NAME>"
__email__ = "<EMAIL>"

__description__ = "A library that makes consuming a ReST API easier and more convenient"
__url__ = "https://github.com/dstufft/slumber/"

__all__ = ["Resource", "API"]

import copy
# @@@ Should we look for one with speedups?
# @@@ Python 2.5 Compatibility?
import json
import urllib
import urlparse

from slumber.http import HttpClient

class Resource(object):

    def __init__(self, domain, endpoint=None):
        self.domain = domain
        self.endpoint = endpoint

        self.http_client = HttpClient()

    def __call__(self, id):
        obj = copy.deepcopy(self)
        obj.object_id = id
        return obj

    def _request(self, method, **kwargs):
        url = urlparse.urljoin(self.domain, self.endpoint)

        if hasattr(self, "object_id"):
            url = urlparse.urljoin(url, str(self.object_id))

        if kwargs:
            url = "?".join([url, urllib.urlencode(kwargs)])

        resp, content = self.http_client.request(url, method)
        return json.loads(content)

    
    def get(self, **kwargs):
        return self._request("GET", **kwargs)<commit_msg>_request can take an url override keyword, post returns the self.get from the Location header<commit_after>__version__ = "dev"

__author__ = "<NAME>"
__email__ = "<EMAIL>"

__description__ = "A library that makes consuming a ReST API easier and more convenient"
__url__ = "https://github.com/dstufft/slumber/"

__all__ = ["Resource", "API"]

import copy
# @@@ Should we look for one with speedups?
# @@@ Python 2.5 Compatibility?
import json
import urllib
import urlparse

from slumber.http import HttpClient

class Resource(object):

    def __init__(self, domain, endpoint=None):
        self.domain = domain
        self.endpoint = endpoint

        self.http_client = HttpClient()

    def __call__(self, id):
        obj = copy.deepcopy(self)
        obj.object_id = id
        return obj

    def _request(self, method, **kwargs):
        if kwargs.has_key("url"):
            url = kwargs.pop("url")
        else:
            url = urlparse.urljoin(self.domain, self.endpoint)

            if hasattr(self, "object_id"):
                url = urlparse.urljoin(url, str(self.object_id))

        if kwargs.has_key("body"):
            body = kwargs.pop("body")
        else:
            body = None

        if kwargs:
            url = "?".join([url, urllib.urlencode(kwargs)])

        return self.http_client.request(url, method, body=body, headers={"content-type": "application/json"})
    
    def get(self, **kwargs):
        resp, content = self._request("GET", **kwargs)
        return json.loads(content)

    def post(self, data, **kwargs):
        kwargs.update({
            "body": json.dumps(data)
        })
        resp, content = self._request("POST", **kwargs)
        if resp.status == 201:
            return self.get(url=resp["location"])
        else:
            # @@@ Need to be Some sort of Error Here or Something
            return
{'fraction_non_alphanumeric': 0.12207963145771636, 'fraction_numerical': 0.002303389272787101, 'mean_word_length': 2.6233611442193085, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '6231751', 'n_tokens_mistral': 922, 'n_tokens_neox': 906, 'n_words': 250}