Ejemplo n.º 1
0
  def callAPI(self, resourcePath, method, queryParams, postData,
              ca_cert=None, cert=None, key=None, headerParams=None, files=None):

    url = self.host + resourcePath

    mergedHeaderParams = self.defaultHeaders.copy()
    if headerParams:
        mergedHeaderParams.update(headerParams)
    headers = {}
    if mergedHeaderParams:
      for param, value in mergedHeaderParams.iteritems():
        headers[param] = ApiClient.sanitizeForSerialization(value)

    if self.cookie:
      headers['Cookie'] = ApiClient.sanitizeForSerialization(self.cookie)

    data = None

    if queryParams:
      # Need to remove None values, these should not be sent
      sentQueryParams = {}
      for param, value in queryParams.items():
        if value is not None:
          sentQueryParams[param] = ApiClient.sanitizeForSerialization(value)
      url = url + '?' + urllib.urlencode(sentQueryParams)

    if method in ['GET']:
      #Options to add statements later on and for compatibility
      pass

    elif method in ['POST', 'PUT', 'DELETE']:
      if postData:
        postData = ApiClient.sanitizeForSerialization(postData)
        if 'Content-type' not in headers:
          headers['Content-type'] = 'application/json'
          data = json.dumps(postData)
        elif headers['Content-type'] == 'multipart/form-data':
          data = self.buildMultipartFormData(postData, files)
          headers['Content-type'] = 'multipart/form-data; boundary={0}'.format(self.boundary)
          headers['Content-length'] = str(len(data))
        else:
            data = urllib.urlencode(postData)

    else:
      raise Exception('Method ' + method + ' is not recognized.')

    utils.raise_exception_invalid_scheme(url)

    response = requests.request(method, url=url, headers=headers, data=data,
                                cert=(cert, key), verify=ca_cert)
    if 'Set-Cookie' in response.headers:
      self.cookie = response.headers['Set-Cookie']
    try:
      data = json.loads(response.content)
    except ValueError:  # PUT requests don't return anything
      data = None

    return data
Ejemplo n.º 2
0
 def test_raise_exception_invalid_scheme_https(self):
     utils.raise_exception_invalid_scheme(url='https://www.openstack.org')
Ejemplo n.º 3
0
 def test_raise_exception_invalid_scheme_https(self):
     utils.raise_exception_invalid_scheme(url='https://www.openstack.org')
Ejemplo n.º 4
0
    def callAPI(self,
                resourcePath,
                method,
                queryParams,
                postData,
                ca_cert=None,
                cert=None,
                key=None,
                headerParams=None,
                files=None):

        url = self.host + resourcePath

        mergedHeaderParams = self.defaultHeaders.copy()
        if headerParams:
            mergedHeaderParams.update(headerParams)
        headers = {}
        if mergedHeaderParams:
            for param, value in mergedHeaderParams.iteritems():
                headers[param] = ApiClient.sanitizeForSerialization(value)

        if self.cookie:
            headers['Cookie'] = ApiClient.sanitizeForSerialization(self.cookie)

        data = None

        if queryParams:
            # Need to remove None values, these should not be sent
            sentQueryParams = {}
            for param, value in queryParams.items():
                if value is not None:
                    sentQueryParams[
                        param] = ApiClient.sanitizeForSerialization(value)
            url = url + '?' + urllib.urlencode(sentQueryParams)

        if method in ['GET']:
            #Options to add statements later on and for compatibility
            pass

        elif method in ['POST', 'PUT', 'DELETE']:
            if postData:
                postData = ApiClient.sanitizeForSerialization(postData)
                if 'Content-type' not in headers:
                    headers['Content-type'] = 'application/json'
                    data = json.dumps(postData)
                elif headers['Content-type'] == 'multipart/form-data':
                    data = self.buildMultipartFormData(postData, files)
                    headers[
                        'Content-type'] = 'multipart/form-data; boundary={0}'.format(
                            self.boundary)
                    headers['Content-length'] = str(len(data))
                else:
                    data = urllib.urlencode(postData)

        else:
            raise Exception('Method ' + method + ' is not recognized.')

        utils.raise_exception_invalid_scheme(url)

        response = requests.request(method,
                                    url=url,
                                    headers=headers,
                                    data=data,
                                    cert=(cert, key),
                                    verify=ca_cert)
        if 'Set-Cookie' in response.headers:
            self.cookie = response.headers['Set-Cookie']
        try:
            data = json.loads(response.content)
        except ValueError:  # PUT requests don't return anything
            data = None

        return data