# This module has been derived from APIMEDIC Python SDK
# Original Source : https://github.com/priaid-eHealth/symptomchecker
import requests
import hmac, hashlib
import base64
import json
from enum import Enum
Gender = Enum('Gender', 'Male Female')
SelectorStatus = Enum('SelectorStatus', 'Man Woman Boy Girl')
class DiagnosisClient:
'Client class for priaid diagnosis health service'
#
# DiagnosisClient constructor
#
# api user username
# api user password
# priaid login url (https://authservice.priaid.ch/login)
# language
# priaid healthservice url(https://healthservice.priaid.ch)
def __init__(self, username, password, authServiceUrl, language, healthServiceUrl):
self._handleRequiredArguments(username, password, authServiceUrl, healthServiceUrl, language)
self._language = language
self._healthServiceUrl = healthServiceUrl
self._token = self._loadToken(username, password, authServiceUrl)
def _loadToken(self, username, password, url):
rawHashString = hmac.new(bytes(password, encoding='utf-8'), url.encode('utf-8')).digest()
computedHashString = base64.b64encode(rawHashString).decode()
bearer_credentials = username + ':' + computedHashString
postHeaders = {
'Authorization': 'Bearer {}'.format(bearer_credentials)
}
responsePost = requests.post(url, headers=postHeaders)
data = json.loads(responsePost.text)
return data
def _handleRequiredArguments(self, username, password, authUrl, healthUrl, language):
if not username:
raise ValueError("Argument missing: username")
if not password:
raise ValueError("Argument missing: username")
if not authUrl:
raise ValueError("Argument missing: authServiceUrl")
if not healthUrl:
raise ValueError("Argument missing: healthServiceUrl")
if not language:
raise ValueError("Argument missing: language")
def _loadFromWebService(self, action):
extraArgs = "token=" + self._token["Token"] + "&format=json&language=" + self._language
if "?" not in action:
action += "?" + extraArgs
else:
action += "&" + extraArgs
url = self._healthServiceUrl + "/" + action
response = requests.get(url)
try:
response.raise_for_status()
except requests.exceptions.HTTPError as e:
print ("----------------------------------")
print ("HTTPError: " + e.response.text )
print ("----------------------------------")
raise
try:
dataJson = response.json()
except ValueError:
raise requests.exceptions.RequestException(response=response)
data = json.loads(response.text)
return data
#
# Load all symptoms
#
# Returns list of all symptoms
def loadSymptoms(self):
return self._loadFromWebService("symptoms")
#
# Load all issues
#
# Returns list of all issues
def loadIssues(self):
return self._loadFromWebService("issues")
#
# Load detail informations about selected issue
#
#
# Returns detail informations about selected issue
def loadIssueInfo(self, issueId):
if isinstance( issueId, int ):
issueId = str(issueId)
action = "issues/{0}/info".format(issueId)
return self._loadFromWebService(action)
#
# Load calculated list of potential issues for selected parameters
#
# List of selected symptom ids
# Selected person gender (Male, Female)
# Selected person year of born
# Returns calculated list of potential issues for selected parameters
def loadDiagnosis(self, selectedSymptoms, gender, yearOfBirth):
if not selectedSymptoms:
raise ValueError("selectedSymptoms can not be empty")
# serializedSymptoms = json.dumps(selectedSymptoms)
# print(serializedSymptoms)
action = "diagnosis?symptoms=[{0}]&gender={1}&year_of_birth={2}".format(selectedSymptoms, gender, yearOfBirth)
return self._loadFromWebService(action)
#
# Load calculated list of specialisations for selected parameters
#
# List of selected symptom ids
# Selected person gender (Male, Female)
# Selected person year of born
# Returns calculated list of specialisations for selected parameters
def loadSpecialisations(self, selectedSymptoms, gender, yearOfBirth):
if not selectedSymptoms:
raise ValueError("selectedSymptoms can not be empty")
serializedSymptoms = json.dumps(selectedSymptoms)
action = "diagnosis/specialisations?symptoms={0}&gender={1}&year_of_birth={2}".format(serializedSymptoms, gender, yearOfBirth)
return self._loadFromWebService(action)
#
# Load human body locations
#
# Returns list of human body locations
def loadBodyLocations(self):
return self._loadFromWebService("body/locations")
#
# Load human body sublocations for selected human body location
#
# Human body location id
# Returns list of human body sublocations for selected human body location
def loadBodySubLocations(self, bodyLocationId):
action = "body/locations/{0}".format(bodyLocationId)
return self._loadFromWebService(action)
#
# Load all symptoms for selected human body location
#
# Human body sublocation id
# Selector status (Man, Woman, Boy, Girl)
# Returns list of all symptoms for selected human body location
def loadSublocationSymptoms(self, locationId, selectedSelectorStatus):
action = "symptoms/{0}/{1}".format(locationId, selectedSelectorStatus.name)
return self._loadFromWebService(action)
#
# Load list of proposed symptoms for selected symptoms combination
#
# List of selected symptom ids
# Selected person gender (Male, Female)
# Selected person year of born
# Returns list of proposed symptoms for selected symptoms combination
def loadProposedSymptoms(self, selectedSymptoms, gender, yearOfBirth):
if not selectedSymptoms:
raise ValueError("selectedSymptoms can not be empty")
serializedSymptoms = json.dumps(selectedSymptoms)
action = "symptoms/proposed?symptoms={0}&gender={1}&year_of_birth={2}".format(serializedSymptoms, gender, yearOfBirth)
return self._loadFromWebService(action)
#
# Load red flag text for selected symptom
#
# Selected symptom id
# Returns red flag text for selected symptom
def loadRedFlag(self, symptomId):
action = "redflag?symptomId={0}".format(symptomId)
return self._loadFromWebService(action)