Exemplo n.º 1
0
    def request(self, target, method='GET', params={}):
        assert method in ['GET', 'POST', 'PUT', 'DELETE'], "Only 'GET', 'POST', 'PUT', 'DELETE' are allowed for method."

        headers = {
            'User-Agent': 'tempodb-python/%s' % (tempodb.get_version(), ),
            'Accept-Encoding': 'gzip',
        }

        if method == 'POST':
            headers['Content-Type'] = "application/json"
            base = self.build_full_url(target)
            response = self.session.post(base, data=simplejson.dumps(params), auth=(self.key, self.secret), headers=headers)
        elif method == 'PUT':
            headers['Content-Type'] = "application/json"
            base = self.build_full_url(target)
            response = self.session.put(base, data=simplejson.dumps(params), auth=(self.key, self.secret), headers=headers)
        elif method == 'DELETE':
            base = self.build_full_url(target, params)
            response = self.session.delete(base, auth=(self.key, self.secret), headers=headers)
        else:
            base = self.build_full_url(target, params)
            response = self.session.get(base, auth=(self.key, self.secret), headers=headers)

        if response.status_code == 200:
            if response.text:
                json = simplejson.loads(response.text)
            else:
                json = ''
            #try:
            #    json = simplejson.loads(response.text)
            #except simplejson.decoder.JSONDecodeError, err:
            #    json = dict(error="JSON Parse Error (%s):\n%s" % (err, response.text))
        else:
            json = dict(error=response.text)
        return json
Exemplo n.º 2
0
 def setUp(self):
     self.client = Client('key', 'secret', 'example.com', 443, True)
     self.client.session = mock.Mock()
     self.headers = {
         'User-Agent': 'tempodb-python/%s' % tempodb.get_version(),
         'Accept-Encoding': 'gzip',
     }
     self.get_headers = self.headers
     self.delete_headers = self.headers
     self.put_headers = dict({
         'Content-Type': 'application/json',
     }, **self.headers)
     self.post_headers = self.put_headers
Exemplo n.º 3
0
 def setUp(self):
     self.client = Client('key', 'secret', 'example.com', 443, True)
     self.client.session = mock.Mock()
     self.headers = {
         'User-Agent': 'tempodb-python/%s' % tempodb.get_version(),
         'Accept-Encoding': 'gzip',
     }
     self.get_headers = self.headers
     self.delete_headers = self.headers
     self.put_headers = dict({
         'Content-Type': 'application/json',
     }, **self.headers)
     self.post_headers = self.put_headers
Exemplo n.º 4
0
    def request(self, target, method='GET', params={}):
        assert method in [
            'GET', 'POST', 'PUT', 'DELETE'
        ], "Only 'GET', 'POST', 'PUT', 'DELETE' are allowed for method."

        headers = {
            'User-Agent': 'tempodb-python/%s' % (tempodb.get_version(), ),
            'Accept-Encoding': 'gzip',
        }

        if method == 'POST':
            headers['Content-Type'] = "application/json"
            base = self.build_full_url(target)
            json_data = simplejson.dumps(params, default=DATETIME_HANDLER)
            response = self.session.post(base,
                                         data=json_data,
                                         auth=(self.key, self.secret),
                                         headers=headers)
        elif method == 'PUT':
            headers['Content-Type'] = "application/json"
            base = self.build_full_url(target)
            json_data = simplejson.dumps(params, default=DATETIME_HANDLER)
            response = self.session.put(base,
                                        data=json_data,
                                        auth=(self.key, self.secret),
                                        headers=headers)
        elif method == 'DELETE':
            base = self.build_full_url(target, params)
            response = self.session.delete(base,
                                           auth=(self.key, self.secret),
                                           headers=headers)
        else:
            base = self.build_full_url(target, params)
            response = self.session.get(base,
                                        auth=(self.key, self.secret),
                                        headers=headers)

        if response.status_code == 200:
            if response.text:
                json = simplejson.loads(response.text)
            else:
                json = ''
            # try:
            #    json = simplejson.loads(response.text)
            # except simplejson.decoder.JSONDecodeError, err:
            #    json = dict(error="JSON Parse Error (%s):\n%s" % (err, response.text))
        else:
            json = dict(error=response.text)
        return json