Exemplo n.º 1
0
    def inner(*args, **kwargs):
        """Inner wrapper function for the decorator

        :param list args: positional arguments
        :param dict kwargs: keyword arguments

        """
        if kwargs.get('data') and not utils.is_string(kwargs.get('data')):
            kwargs['data'] = json.dumps(kwargs['data'])
        elif len(args) == 3 and not utils.is_string(args[2]):
            args = args[0], args[1], json.dumps(args[2])
        return fun(*args, **kwargs)
Exemplo n.º 2
0
    def inner(*args, **kwargs):
        """Inner wrapper function for the decorator

        :param list args: positional arguments
        :param dict kwargs: keyword arguments

        """
        if kwargs.get('data'):
            if not utils.is_string(kwargs.get('data')):
                kwargs['data'] = json.dumps(kwargs['data'])
        elif len(args) == 3 and not utils.is_string(args[2]):
            args = args[0], args[1], json.dumps(args[2])
        return fun(*args, **kwargs)
Exemplo n.º 3
0
    def _set_item(self, item, value, flags=None):
        """Internal method for setting a key/value pair with flags in the
        Key/Value service

        :param str item: The key to set
        :param mixed value: The value to set
        :param int flags: User defined flags to set
        :raises: KeyError

        """
        if utils.is_string(value):
            if utils.PYTHON3:
                value = value.encode('utf-8')
            elif not isinstance(value, unicode):
                try:
                    value.decode('ascii')
                except UnicodeDecodeError:
                    value = value.decode('utf-8')
        if value:
            item = item.lstrip('/')
        response = self._adapter.get(self._build_uri([item]))
        index = 0
        if response.status_code == 200:
            index = response.body.get('ModifyIndex')
            if response.body.get('Value') == value:
                return True
        query_params = {'cas': index}
        if flags is not None:
            query_params['flags'] = flags
        response = self._adapter.put(self._build_uri([item], query_params),
                                     value)
        if not response.status_code == 200 or not response.body:
            raise KeyError(
                'Error setting "{0}" ({1})'.format(item, response.status_code))
Exemplo n.º 4
0
    def _prepare_value(value):
        """Prepare the value passed in and ensure that it is properly encoded

        :param mixed value: The value to prepare
        :rtype: bytes

        """
        if not utils.is_string(value) or isinstance(value, bytes):
            return value
        try:
            return value.encode("utf-8")
        except UnicodeDecodeError:
            return value
Exemplo n.º 5
0
    def _prepare_value(value):
        """Prepare the value passed in and ensure that it is properly encoded

        :param mixed value: The value to prepare
        :rtype: str|unicode

        """
        if utils.is_string(value):
            if utils.PYTHON3:
                value = value.encode('utf-8')
            elif not isinstance(value, unicode):
                try:
                    value.decode('ascii')
                except UnicodeDecodeError:
                    value = value.decode('utf-8')
        return value
Exemplo n.º 6
0
    def put(self, uri, data=None, timeout=None):
        """Perform a HTTP put

        :param src uri: The URL to send the DELETE to
        :param str data: The PUT data
        :param timeout: How long to wait on the response
        :type timeout: int or float or None
        :rtype: consulate.api.Response

        """
        LOGGER.debug("PUT %s with %r", uri, data)
        headers = {"Content-Type": CONTENT_FORM if utils.is_string(data) else CONTENT_JSON}
        try:
            return api.Response(self.session.put(uri, data=data, headers=headers, timeout=timeout or self.timeout))
        except (requests.exceptions.RequestException, OSError, socket.error) as err:
            raise exceptions.RequestError(str(err))
Exemplo n.º 7
0
    def put(self, uri, data=None):
        """Perform a HTTP put

        :param src uri: The URL to send the DELETE to
        :param str data: The PUT data
        :rtype: consulate.api.Response

        """
        LOGGER.debug("PUT %s with %r", uri, data)
        headers = {
            'Content-Type': CONTENT_FORM
            if utils.is_string(data) else CONTENT_JSON
        }
        return self._process_response(self.session.put(uri,
                                                       data=data,
                                                       headers=headers,
                                                       timeout=self.timeout))
Exemplo n.º 8
0
Arquivo: kv.py Projeto: 40a/consulate
    def _prepare_value(value):
        """Prepare the value passed in and ensure that it is properly encoded

        :param mixed value: The value to prepare
        :rtype: bytes

        """
        if not utils.is_string(value) or isinstance(value, bytes):
            return value
        try:
            if utils.PYTHON3:
                return value.encode('utf-8')
            elif isinstance(value, unicode):
                return value.encode('utf-8')
        except UnicodeDecodeError:
            return value
        return value
Exemplo n.º 9
0
    def put(self, uri, data=None):
        """Perform a HTTP put

        :param src uri: The URL to send the DELETE to
        :param str data: The PUT data
        :rtype: consulate.api.Response

        """
        LOGGER.debug("PUT %s with %r", uri, data)
        headers = {
            'Content-Type':
            CONTENT_FORM if utils.is_string(data) else CONTENT_JSON
        }
        return self._process_response(
            self.session.put(uri,
                             data=data,
                             headers=headers,
                             timeout=self.timeout))
Exemplo n.º 10
0
    def put(self, uri, data=None):
        """Perform a HTTP put

        :param src uri: The URL to send the DELETE to
        :param str data: The PUT data
        :rtype: consulate.api.Response

        """
        LOGGER.debug("PUT %s with %r", uri, data)
        if utils.is_string(data):
            headers = {'Content-Type': CONTENT_FORM}
        else:
            headers = {'Content-Type': CONTENT_JSON}
        if not utils.PYTHON3 and data:
            data = data.encode('utf-8')
        response = self.session.put(uri, data=data, headers=headers)
        return api.Response(response.status_code,
                            response.content,
                            response.headers)
Exemplo n.º 11
0
 def test_monitor(self):
     for offset, line in enumerate(self.consul.agent.monitor()):
         self.assertTrue(utils.is_string(line))
         self.consul.agent.metrics()
         if offset > 1:
             break