def __init__(self, api_version=None, panrc_tag=None, hostname=None, api_key=None, timeout=None, verify_cert=True): self._log = logging.getLogger(__name__).log self.api_version = api_version self.panrc_tag = panrc_tag self.hostname = hostname self.api_key = api_key self.timeout = timeout self.verify_cert = verify_cert self._log(DEBUG3, 'Python version: %s', sys.version) self._log(DEBUG3, 'ssl: %s', ssl.OPENSSL_VERSION) self._log(DEBUG3, 'pan-python version: %s', __version__) init_panrc = {} # .panrc args from constructor if hostname is not None: init_panrc['hostname'] = hostname if api_key is not None: init_panrc['api_key'] = api_key try: panrc = pan.rc.PanRc(tag=self.panrc_tag, init_panrc=init_panrc) except pan.rc.PanRcError as e: raise PanLicapiError(e) if 'api_key' in panrc.panrc: self.api_key = panrc.panrc['api_key'] if 'hostname' in panrc.panrc: self.hostname = panrc.panrc['hostname'] else: self.hostname = _cloud_server if self.api_key is None: raise PanLicapiError('api_key required') self.uri = 'https://' + self.hostname self.base_uri = self.uri + '/api/license' self.headers = { 'apikey': self.api_key, # requests header value must be str: # https://github.com/kennethreitz/requests/issues/3477 'version': str(int(api_version)), } try: self.http = pan.http.PanHttp(timeout=self.timeout, verify_cert=self.verify_cert) except pan.http.PanHttpError as e: raise PanLicapiError(e) if self.http.using_requests: s = 'using requests %s' % self.http.requests_version else: s = 'using urllib' self._log(DEBUG2, s)
def raise_for_status(self): if self.http_code is None: return None if not (200 <= self.http_code < 300): e = 'HTTP Error %s' % self.http_code if self.http_reason is not None: e += ': %s' % self.http_reason if self.json is not None and 'Message' in self.json: e += ' ' + self.json['Message'] raise PanLicapiError(e) return None
def _api_request(self, url, headers, data=None, params=None): self._log(DEBUG1, url) if params is not None: self._log(DEBUG1, params) self._log(DEBUG1, data) try: self.http.http_request(url=url, headers=self.headers, data=data, params=params) except pan.http.PanHttpError as e: raise PanLicapiError(str(e)) r = PanLicapiRequest(inspect.stack()[1][3]) self._set_attributes(r) return r