Beispiel #1
0
    def _request(self, url_path, query_params, json, expected_codes,
                 json_response):
        # Construct the URL using url_path, query_params and the api key
        url = url_path
        if FLAGS.api_key:
            if query_params:
                url = '%s?key=%s&%s' % (url_path, FLAGS.api_key, query_params)
            else:
                url = '%s?key=%s' % (url_path, FLAGS.api_key)
        elif query_params:
            url = '%s?%s' % (url_path, query_params)

        # Prepare headers
        headers = {'Content-Type': 'application/json'}
        if FLAGS.auth_token:
            headers['Authorization'] = 'Bearer ' + FLAGS.auth_token

        self._conn.request('POST', url, json, headers)
        response = esp_utils.Response(self._conn.getresponse())

        # Make sure that the response status code is in 200s, 300s or 400s
        # and that the response payload is valid JSON
        if not response.status_code in expected_codes:
            print esp_utils.red('Invalid status code %d: url=%s, json=%s' %
                                (response.status_code, url, json))
            self._unexpected_errors += 1
        elif json_response and not response.is_json():
            print esp_utils.red('Response is not json: url=%s, json=%s' %
                                (url, json))
            self._unexpected_errors += 1

        self._total_requests += 1
Beispiel #2
0
 def _call_http(self,
                path,
                api_key=None,
                auth=None,
                data=None,
                method=None):
     """Makes a http call and returns its response."""
     url = path
     if api_key:
         url += '?key=' + api_key
     headers = {'Content-Type': 'application/json'}
     if auth:
         headers['Authorization'] = 'Bearer ' + auth
     body = json.dumps(data) if data else None
     if not method:
         method = 'POST' if data else 'GET'
     if FLAGS.verbose:
         print 'HTTP: %s %s' % (method, url)
         print 'headers: %s' % str(headers)
         print 'body: %s' % body
     self.conn.request(method, url, body, headers)
     response = esp_utils.Response(self.conn.getresponse())
     if FLAGS.verbose:
         print 'Status: %s, body=%s' % (response.status_code, response.text)
     return response
Beispiel #3
0
 def _get_status(self):
     self._status_conn.request('GET', '/endpoints_status')
     response = esp_utils.Response(self._status_conn.getresponse())
     status = response.json()
     if len(status['processes']) == 0:
         sys.exit(esp_utils.red('Cloud not get ESP status'))
     return status