Example #1
0
    def _put(self, path, **kwargs):
        """ a short version of MiteAPI._put to check the built url.
        """
        # clean kwargs (filter None and empty string)
        clean_kwargs = clean_dict(kwargs)

        data = urllib.parse.urlencode(clean_kwargs).encode('utf-8')
        api = self._api('%s.json' % path)
        return {'api': api, 'data': data, 'code': resp_code, 'method': 'PUT'}
Example #2
0
    def _post(self, path, **kwargs):
        """ a short version of MiteAPI._post to check the built url.
        """
        # clean kwargs (filter None and empty string)
        clean_kwargs = clean_dict(kwargs)

        data = bytes(json.dumps(clean_kwargs), encoding='UTF-8')
        # change content type on post
        self._headers['Content-Type'] = 'application/json'
        api = self._api('%s.json' % path)
        return {'api': api, 'data': data, 'code': resp_code, 'method': 'POST'}
Example #3
0
    def _get(self, path, **kwargs):
        """ a short version of MiteAPI._get to check the built url.
        """
        # clean kwargs (filter None and empty string)
        clean_kwargs = clean_dict(kwargs)

        data = urllib.parse.urlencode(clean_kwargs)
        if len(data) > 0:
            api = self._api('%s.json?%s' % (path, data))
        else:
            api = self._api('%s.json' % path)
        return {adapter_class: {'api': api, 'data': data}}
Example #4
0
    def _delete(self, path, **kwargs):
        """ return a dict. """
        # clean kwargs (filter None and empty string)
        clean_kwargs = clean_dict(kwargs)

        data = urllib.parse.urlencode(clean_kwargs).encode('utf-8')
        api = self._api('%s.json' % path)
        return {
            'api': api,
            'data': data,
            'code': resp_code,
            'method': 'DELETE'
        }
Example #5
0
    def _put(self, path, **kwargs):
        """ return a dict. """
        # clean kwargs (filter None and empty string)
        clean_kwargs = clean_dict(kwargs)

        data = urllib.parse.urlencode(clean_kwargs).encode('utf-8')
        api = self._api('%s.json' % path)
        req = request.Request(
            api, headers=self._headers, data=data, method='PUT')

        try:
            resp = request.urlopen(req).read()
        except urllib.error.HTTPError as e:
            resp = e.fp.read()

        return json.loads(resp.decode())
Example #6
0
    def _post(self, path, **kwargs):
        """ return a dict. """
        # clean kwargs (filter None and empty string)
        clean_kwargs = clean_dict(kwargs)

        data = bytes(json.dumps(clean_kwargs), encoding='UTF-8')
        # change content type on post
        self._headers['Content-Type'] = 'application/json'
        api = self._api('%s.json' % path)
        req = request.Request(
            api, headers=self._headers, data=data, method='POST')
        try:
            resp = request.urlopen(req, data).read()
        except urllib.error.HTTPError as e:
            resp = e.fp.read()
        # reset content type
        self._headers['Content-Type'] = 'text/json'
        return json.loads(resp.decode())
Example #7
0
    def _put(self, path, **kwargs):
        """ return a dict. """
        # clean kwargs (filter None and empty string)
        clean_kwargs = clean_dict(kwargs)

        data = urllib.parse.urlencode(clean_kwargs).encode('utf-8')
        api = self._api('%s.json' % path)
        req = request.Request(api,
                              headers=self._headers,
                              data=data,
                              method='PUT')

        try:
            resp = request.urlopen(req).read()
        except urllib.error.HTTPError as e:
            resp = e.fp.read()

        return json.loads(resp.decode())
Example #8
0
    def _post(self, path, **kwargs):
        """ return a dict. """
        # clean kwargs (filter None and empty string)
        clean_kwargs = clean_dict(kwargs)

        data = bytes(json.dumps(clean_kwargs), encoding='UTF-8')
        # change content type on post
        self._headers['Content-Type'] = 'application/json'
        api = self._api('%s.json' % path)
        req = request.Request(api,
                              headers=self._headers,
                              data=data,
                              method='POST')
        try:
            resp = request.urlopen(req, data).read()
        except urllib.error.HTTPError as e:
            resp = e.fp.read()
        # reset content type
        self._headers['Content-Type'] = 'text/json'
        return json.loads(resp.decode())
Example #9
0
    def _delete(self, path, **kwargs):
        """ return a dict. """
        # clean kwargs (filter None and empty string)
        clean_kwargs = clean_dict(kwargs)

        data = urllib.parse.urlencode(clean_kwargs).encode('utf-8')
        api = self._api('%s.json' % path)
        req = request.Request(
            api, headers=self._headers, data=data, method='DELETE')

        try:
            resp = request.urlopen(req)
            resp_txt = resp.read().strip()
            # TODO: is this right? The Mite API returns b' ' in this case
            if resp.code == 200 and not resp_txt:
                resp = b'{"success": 200}'
            else:
                resp = resp_txt
        except urllib.error.HTTPError as e:
            resp = e.fp.read()
        return json.loads(resp.decode())
Example #10
0
    def _delete(self, path, **kwargs):
        """ return a dict. """
        # clean kwargs (filter None and empty string)
        clean_kwargs = clean_dict(kwargs)

        data = urllib.parse.urlencode(clean_kwargs).encode('utf-8')
        api = self._api('%s.json' % path)
        req = request.Request(api,
                              headers=self._headers,
                              data=data,
                              method='DELETE')

        try:
            resp = request.urlopen(req)
            resp_txt = resp.read().strip()
            # TODO: is this right? The Mite API returns b' ' in this case
            if resp.code == 200 and not resp_txt:
                resp = b'{"success": 200}'
            else:
                resp = resp_txt
        except urllib.error.HTTPError as e:
            resp = e.fp.read()
        return json.loads(resp.decode())