예제 #1
0
    def send(self,
             devname,
             method='get',
             api='',
             headers={},
             params=None,
             data=None,
             verify=False,
             retAs='json',
             **kwargs):
        session = self._get_session()
        url = '{}{}'.format(self.base_url, api)

        self._log("REST [{}]: {}".format(method.upper(), url))
        if params: self._log("params:\n{}".format(pprint.pformat(params)))
        if data: self._log("data:\n{}".format(pprint.pformat(data)))
        if 'json' in kwargs:
            self._log("json:\n{}".format(pprint.pformat(kwargs.get('json'))))

        res = None
        try:
            if 'timeout' not in kwargs: kwargs['timeout'] = self.timeout
            with warnings.catch_warnings():
                #from requests.packages.urllib3.exceptions import InsecureRequestWarning
                #warnings.filterwarnings("ignore", category=InsecureRequestWarning)
                res = session.request(method.lower(),
                                      url,
                                      params=params,
                                      data=data,
                                      verify=verify,
                                      **kwargs)
            if res:
                self._log("REST [STATUS]: {} {}".format(
                    res.status_code, res.reason))
                if not res.ok:
                    self._log("request headers:\n{}".format(
                        pprint.pformat(res.request.headers)))
                    self._log("respond headers:\n{}".format(
                        pprint.pformat(res.headers)))
                self._log("text:\n{}".format(res.text))
        except Exception as e:
            print(utils.stack_trace(e))
            #msg = utils.stack_trace(traceback.format_exc())
            #self._log('REST Request error: {}\n{}'.format(e, msg))
            #pprint.pprint(e)
            #pprint.pprint(msg)
            raise e

        if res is not None and retAs:
            if retAs == 'json':
                try:
                    return res.json()
                except Exception as e:
                    self._log('REST Output error: {}'.format(e))
                    return {"text": res.text}
            elif retAs == 'text':
                return res.text

        return self._result(method.upper(), res, kwargs.get('json', data))
예제 #2
0
 def get(self, path, *args, **kwargs):
     session = self._get_session()
     try:
         url = self._get_url(path, *args, **kwargs)
         retval = session.get(url, verify=False, timeout=self.timeout)
         return self._result("GET", retval, None)
     except Exception as e:
         print(utils.stack_trace(e))
         raise e
예제 #3
0
 def get(self, path, *args, **kwargs):
     username = kwargs.pop("rest_username", None)
     password = kwargs.pop("rest_password", None)
     session = self._get_session(username, password)
     try:
         timeout = kwargs.pop("rest_timeout", self.timeout)
         url = self._get_url(path, *args, **kwargs)
         retval = session.get(url, verify=False, timeout=timeout)
         return self._result("GET", retval, None)
     except Exception as e:
         print(utils.stack_trace(e))
         raise e
예제 #4
0
 def _json(self, retval, default={}):
     try:
         return retval.json() if retval.text else {}
     except Exception as exp:
         print(utils.stack_trace(exp))
         return default