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)
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)
def _get_nsdpkg(self, args=None): _url = "{0}/nsd/v1/ns_descriptors".format(self._base_path) _url = self._build_url_query(_url, args) nsdpkg_list, headers = self._request(get, _url, headers=self._headers) if not nsdpkg_list: raise NsdNotFound(nsd_id=args["args"]["id"]) elif len(nsdpkg_list) > 1: raise ServerError( description="Multiple NSD with id={} found in OSM".format( args["args"]["id"])) return nsdpkg_list[0], headers
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
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)
def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except (ConnectionError, Timeout, TooManyRedirects, URLRequired) as e: raise ServerError(f'problem contacting iwf repository: {str(e)}')