Пример #1
0
 def _exec_get(self, url=None, params=None, headers=None):
     try:
         resp = requests.get(url,
                             params=params,
                             verify=False,
                             stream=True,
                             headers=headers)
     except Exception as e:
         raise ServerError(str(e))
     if resp.status_code in (200, 201, 202):
         if 'application/json' in resp.headers['content-type']:
             return resp.json(), resp.headers
         elif 'application/yaml' in resp.headers['content-type']:
             return YAML.load(resp.text, Loader=YAML.SafeLoader), \
                 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 == 401:
         raise Unauthorized()
     elif resp.status_code == 404:
         raise ResourceNotFound()
     else:
         if 'application/json' in resp.headers['content-type']:
             error = resp.json()
         elif 'application/yaml' in resp.headers['content-type']:
             error = YAML.load(resp.text, Loader=YAML.SafeLoader)
         else:
             error = resp.text
         raise ServerError(error)
Пример #2
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)
Пример #3
0
 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._exec_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
Пример #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
Пример #5
0
 def wrapper(*args, **kwargs):
     try:
         return func(*args, **kwargs)
     except (ConnectionError, Timeout, TooManyRedirects, URLRequired) as e:
         raise ServerError('problem contacting site inventory: ' + str(e))