def cmd(self, method, url, params=None, data=None): if method == "GET" and url in self._cmd_cache: return self._cmd_cache[url] if method == "PUT" or method == "POST": headers = {"Content-type": "application/json" } else: headers = dict() # auth = HTTPBasicAuth(self.username, self.password) headers['Authorization'] = 'Bearer %s' % self.session_token() if data and not isinstance(data, str): data = json.dumps(data) r = requests.request(method, self.location+url, params=params, headers=headers, data=data) BigVProblem.check_response(r) if 'content-type' in r.headers and re.search('^application/(vnd\.bigv\..*\+)?json$', r.headers['content-type']): result = json.loads(r.text) else: result = r.text if method == "GET": self._cmd_cache[url] = result else: self.invalidate_cache(url) return (result)
def definitions_raw(self): if self._def_cache != None: return self._def_cache else: r = requests.get(self.location+"/definitions",headers={'Accept':'application/json'}) BigVProblem.check_response(r) # raises exception if it's >= 400 if 'content-type' in r.headers and re.search('^application/(vnd\.bigv\..*\+)?json$', r.headers['content-type']): self._def_cache = json.loads(r.text) return self._def_cache
def session_token(self): if self._session_token != None: return self._session_token else: creds = dict(username=self.username,password=self.password) if self.yubikey: creds["yubikey"] = self.yubikey st = requests.post("https://auth.bytemark.co.uk/session", data=creds) BigVProblem.check_response(st) self._session_token = st.text return self._session_token