import urllib3 #requests doesnt work as it lacks key password checking import json class orthancInterface: def init(self,fname): #fname is a file with configuration try: f=open(fname) except OSError as e: print("Confgiuration error: OS error({0}): {1}").format(e.errno, e.strerror) raise self.connectionConfig=json.load(f) self.http=urllib3.PoolManager() if 'SSL' in self.connectionConfig['orthanc']: sslConfig=self.connectionConfig['orthanc']['SSL'] self.http = urllib3.PoolManager(\ cert_file=sslConfig['user'],\ cert_reqs='CERT_REQUIRED',\ key_file=sslConfig['key'],\ ca_certs=sslConfig['ca']) #password=self.connectionConfig['SSL']['keyPwd'],\ doesnt work until 1.25 def getBasicAuth(self): user=self.connectionConfig['orthanc']['user'] pwd=self.connectionConfig['orthanc']['password'] return user+":"+pwd def getCoreURL(self): return self.connectionConfig['orthanc']['server'] def get(self,url,binary=False): debug=False if debug: print("GET: {0}").format(url) headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth()) try: if not binary: return self.http.request('GET',url,headers=headers) else: return self.http.request('GET',url,headers=headers,preload_content=False) #f contains json as a return value except urllib3.exceptions.HTTPError as e: print(e) def post(self,url,dataType,data): #dataType is typically json or octet-stream debug=False headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth()) headers['Content-Type']="application/"+dataType#for files #add csrf;also sets self.cookie #headers["X-LABKEY-CSRF"]=self.getCSRF() #doesn't seem like orthanc is big on security #headers["Cookie"]=self.cookie try: return self.http.request('POST',url,headers=headers,body=data) #f contains json as a return value except urllib3.exceptions.HTTPError as e: print(e) def put(self,url,data): debug=False if debug: print("PUT: {}").format(url) headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth()) headers["Content-Type"]="application/octet-stream" #add csrf #headers["X-LABKEY-CSRF"]=self.getCSRF() #headers["Cookie"]=self.cookie try: return self.http.request('PUT',url,headers=headers,body=data) #f contains json as a return value except urllib3.exceptions.HTTPError as e: print(e) def delete(self,url): debug=False if debug: print("DELETE: {0}").format(url) headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth()) try: return self.http.request('DELETE',url,headers=headers) except urllib3.exceptions.HTTPError as e: print(e) return None