Exemple #1
0
    def _exec_post(self, url=None, data=None, json=None, headers=None):

        try:
            resp = requests.post(url,
                                 data=data,
                                 json=json,
                                 verify=False,
                                 headers=headers)
        except Exception as e:
            raise ServerError(str(e))

        if resp.status_code in (200, 201, 202, 206):
            if 'application/json' in resp.headers['content-type']:
                return resp.json(), resp.headers
            else:
                return resp.text, resp.headers
        elif resp.status_code == 204:
            return None, resp.headers
        elif resp.status_code == 400:
            raise BadRequest()
        elif resp.status_code == 404:
            raise ResourceNotFound()
        else:
            if 'application/json' in resp.headers['content-type']:
                error = resp.json()
            else:
                error = resp.text
            raise ServerError(error)
Exemple #2
0
    def _exec_delete(self, url=None, params=None, headers=None):
        logger.debug('#############execute delete######')
        logger.debug('url= ' + url)
        try:
            resp = requests.delete(url,
                                   params=params,
                                   verify=False,
                                   headers=headers)
        except Exception as e:
            raise ServerError(str(e))

        if resp.status_code in (200, 201, 202, 206):
            if 'application/json' in resp.headers['content-type']:
                return resp.json(), resp.headers
            else:
                return resp.text, resp.headers
        elif resp.status_code == 204:
            return None, resp.headers
        elif resp.status_code == 400:
            raise BadRequest()
        elif resp.status_code == 404:
            raise ResourceNotFound()
        else:
            error = resp.json()
            logger.debug('############')
            logger.debug('error: ' + error)
            logger.debug('###########')
            raise ServerError(error)
Exemple #3
0
def create_subscription(nfvo_id: int, body: Dict):
    try:
        create = post(f'{url}/subscriptions', json=body, headers=accept_h)
        create.raise_for_status()
        associate = put(create.json()['_links']['nfvOrchestrators']['href'],
                        data=f'{url}/nfvOrchestrators/{nfvo_id}',
                        headers={
                            **texturi_h,
                            **accept_h
                        })
        associate.raise_for_status()
    except HTTPError as e:
        if e.response.status_code == 400:
            raise BadRequest(description=e.response.text)
        if e.response.status_code == 404:
            raise NfvoNotFound(nfvo_id)
        if e.response.status_code == 422:
            raise Unprocessable(description=e.response.text)
        else:
            raise
    return create.json()
Exemple #4
0
 def _request(req: api.request, url, json=None, params=None, headers=None):
     try:
         resp = req(url,
                    json=json,
                    params=params,
                    headers=headers,
                    verify=False,
                    stream=True)
     except (ConnectionError, Timeout, TooManyRedirects, URLRequired) as e:
         raise ServerError('OSM connection error: ' + str(e))
     try:
         resp.raise_for_status()
     except HTTPError as e:
         if e.response.status_code == 400:
             raise BadRequest(description=e.response.text)
         elif e.response.status_code == 401:
             raise Unauthorized(description=e.response.text)
         elif e.response.status_code == 403:
             raise Forbidden(description=e.response.text)
         elif e.response.status_code == 404:
             raise ResourceNotFound(description=e.response.text)
         elif e.response.status_code == 405:
             raise MethodNotAllowed(description=e.response.text)
         elif e.response.status_code == 409:
             raise Conflict(description=e.response.text)
         elif e.response.status_code == 422:
             raise Unprocessable(description=e.response.text)
         else:
             raise ServerError(description=e.response.text)
     try:
         ctype = resp.headers['content-type']
     except KeyError:
         # success but no content
         return None, resp.headers
     if 'application/json' in ctype:
         return resp.json(), resp.headers
     elif 'application/yaml' in ctype:
         return YAML.load(resp.text, Loader=YAML.SafeLoader), resp.headers
     else:
         return resp.text, resp.headers
Exemple #5
0
    def _exec_post(self, url=None, data=None, json=None, headers=None):
        logger.debug('#############execute post######')
        logger.debug('url= ' + url)
        try:
            resp = requests.post(url,
                                 data=data,
                                 json=json,
                                 verify=False,
                                 headers=headers)
        except Exception as e:
            raise ServerError(str(e))

        if resp.status_code in (200, 201, 202, 206):
            try:
                ctype = resp.headers['content-type']
            except KeyError:
                # success but no content
                return None, resp.headers
            if 'application/json' in ctype:
                return resp.json(), resp.headers
            else:
                return resp.text, resp.headers
        elif resp.status_code == 204:
            return None, resp.headers
        elif resp.status_code == 400:
            raise BadRequest()
        elif resp.status_code == 404:
            raise ResourceNotFound()
        else:
            if 'application/json' in resp.headers['content-type']:
                error = resp.json()
            else:
                error = resp.text
            logger.debug('############')
            logger.debug('error: ' + error)
            logger.debug('###########')
            raise ServerError(error)