<commit_before>import json

import requests

from .entity import Entity
from .utils import dict_merge


class Resource(dict):
    def __init__(self, url=None, name=None, params=None, config=None):
        self._url = url or ''
        self.name = name
        self.params = params or {}

        _config = config or {}
        _config_defaults = {
            'auth': None,
            'use_extensions': False,
            'secure': True,
            'prefix': '/',
            'serializer': 'json',
            'verify': True
        }

        config = dict_merge(_config_defaults, _config)
        self.config = Entity(config)

    def _get_url(self):
        scheme = 'https://' if self.config.secure else 'http://'

        url = scheme + self.config.host + self.config.prefix
        if not url.endswith('/'):
            url += '/'
        resource_url = self._url.replace('_/', '/').rstrip('/')
        if resource_url.startswith('/'):
            resource_url = resource_url[1:]
        url += resource_url

        if self.config.use_extensions:
            url += '.json'

        return url

    def _expand_url(self, part):
        prefix = self._url
        if not self._url.endswith('/'):
            prefix += '/'
        return '%s%s' % (prefix, part)

    def _prepare_data(self, **kwargs):
        if self.config.serializer == 'json':
            return json.dumps(kwargs)
        else:
            return kwargs

    def create(self, **kwargs):
        data = self._prepare_data(**kwargs)
        response = requests.post(self.url, data=data,
                                 auth=self.config['auth'],
                                 verify=self.config['verify'])
        return Entity(response.json())

    def update(self, **kwargs):
        data = self._prepare_data(**kwargs)
        response = requests.put(self.url, data=data,
                                auth=self.config['auth'],
                                verify=self.config['verify'])
        return Entity(response.json())

    def delete(self, **kwargs):
        requests.delete(self.url, params=kwargs,
                        auth=self.config['auth'],
                        verify=self.config['verify'])

    def __getattr__(self, name):
        if name == 'url':
            return self._get_url()
        elif name in self.__dict__:
            return self.__dict__[name]

        # Handle special attributes used with ipdb/ipython tab completion.
        elif name in ['_getAttributeNames', 'trait_names']:
            return super(Resource, self).__getattribute__(name)

        return Resource(self._expand_url(name), name, config=self.config)

    def __getitem__(self, name):
        if isinstance(name, dict):
            params = name
            filtered = Resource(self._url, self.name, params, self.config)
            return filtered
        elif isinstance(name, slice):<commit_msg>Use PUT for create and POST for update<commit_after>import json

import requests

from .entity import Entity
from .utils import dict_merge


class Resource(dict):
    def __init__(self, url=None, name=None, params=None, config=None):
        self._url = url or ''
        self.name = name
        self.params = params or {}

        _config = config or {}
        _config_defaults = {
            'auth': None,
            'use_extensions': False,
            'secure': True,
            'prefix': '/',
            'serializer': 'json',
            'verify': True
        }

        config = dict_merge(_config_defaults, _config)
        self.config = Entity(config)

    def _get_url(self):
        scheme = 'https://' if self.config.secure else 'http://'

        url = scheme + self.config.host + self.config.prefix
        if not url.endswith('/'):
            url += '/'
        resource_url = self._url.replace('_/', '/').rstrip('/')
        if resource_url.startswith('/'):
            resource_url = resource_url[1:]
        url += resource_url

        if self.config.use_extensions:
            url += '.json'

        return url

    def _expand_url(self, part):
        prefix = self._url
        if not self._url.endswith('/'):
            prefix += '/'
        return '%s%s' % (prefix, part)

    def _prepare_data(self, **kwargs):
        if self.config.serializer == 'json':
            return json.dumps(kwargs)
        else:
            return kwargs

    def create(self, **kwargs):
        data = self._prepare_data(**kwargs)
        response = requests.put(self.url, data=data,
                                auth=self.config['auth'],
                                verify=self.config['verify'])
        return Entity(response.json())

    def update(self, **kwargs):
        data = self._prepare_data(**kwargs)
        response = requests.post(self.url, data=data,
                                 auth=self.config['auth'],
                                 verify=self.config['verify'])
        return Entity(response.json())

    def delete(self, **kwargs):
        requests.delete(self.url, params=kwargs,
                        auth=self.config['auth'],
                        verify=self.config['verify'])

    def __getattr__(self, name):
        if name == 'url':
            return self._get_url()
        elif name in self.__dict__:
            return self.__dict__[name]

        # Handle special attributes used with ipdb/ipython tab completion.
        elif name in ['_getAttributeNames', 'trait_names']:
            return super(Resource, self).__getattribute__(name)

        return Resource(self._expand_url(name), name, config=self.config)

    def __getitem__(self, name):
        if isinstance(name, dict):
            params = name
            filtered = Resource(self._url, self.name, params, self.config)
            return filtered
        elif isinstance(name, slice):
{'fraction_non_alphanumeric': 0.12261064232822455, 'fraction_numerical': 0.00034441191665231615, 'mean_word_length': 1.8470588235294119, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '183468', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1542, 'n_words': 378}