<s> import logging <EOL> from server import FHIRServer , FHIRUnauthorizedException , FHIRNotFoundException <EOL> __version__ = '<STR_LIT>' <EOL> __author__ = '<STR_LIT>' <EOL> __license__ = '<STR_LIT>' <EOL> __copyright__ = "<STR_LIT>" <EOL> scope_default = '<STR_LIT>' <EOL> scope_haslaunch = '<STR_LIT>' <EOL> scope_patientlaunch = '<STR_LIT>' <EOL> class FHIRClient ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , settings = None , state = None , save_func = lambda x : x ) : <EOL> self . app_id = None <EOL> """<STR_LIT>""" <EOL> self . server = None <EOL> self . scope = scope_default <EOL> self . redirect = None <EOL> """<STR_LIT>""" <EOL> self . launch_token = None <EOL> """<STR_LIT>""" <EOL> self . launch_context = None <EOL> """<STR_LIT>""" <EOL> self . wants_patient = True <EOL> """<STR_LIT>""" <EOL> self . patient_id = None <EOL> self . _patient = None <EOL> if save_func is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . _save_func = save_func <EOL> if state is not None : <EOL> self . from_state ( state ) <EOL> elif settings is not None : <EOL> if not '<STR_LIT>' in settings : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if not '<STR_LIT>' in settings : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . app_id = settings [ '<STR_LIT>' ] <EOL> self . redirect = settings . get ( '<STR_LIT>' ) <EOL> self . patient_id = settings . get ( '<STR_LIT>' ) <EOL> self . scope = settings . get ( '<STR_LIT>' , self . scope ) <EOL> self . launch_token = settings . get ( '<STR_LIT>' ) <EOL> self . server = FHIRServer ( self , base_uri = settings [ '<STR_LIT>' ] ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> @ property <EOL> def desired_scope ( self ) : <EOL> """<STR_LIT>""" <EOL> scope = self . scope <EOL> if self . launch_token is not None : <EOL> scope = '<STR_LIT:U+0020>' . join ( [ scope_haslaunch , scope ] ) <EOL> elif self . patient_id is None and self . wants_patient : <EOL> scope = '<STR_LIT:U+0020>' . join ( [ scope_patientlaunch , scope ] ) <EOL> return scope <EOL> @ property <EOL> def ready ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . server . ready if self . server is not None else False <EOL> def prepare ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . server : <EOL> if self . server . ready : <EOL> return True <EOL> return self . server . prepare ( ) <EOL> return False <EOL> @ property <EOL> def authorize_url ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . server . authorize_uri if self . server is not None else None <EOL> def handle_callback ( self , url ) : <EOL> """<STR_LIT>""" <EOL> ctx = self . server . handle_callback ( url ) if self . server is not None else None <EOL> self . _handle_launch_context ( ctx ) <EOL> def reauthorize ( self ) : <EOL> """<STR_LIT>""" <EOL> ctx = self . server . reauthorize ( self . server ) if self . server is not None else None <EOL> self . _handle_launch_context ( ctx ) <EOL> return self . launch_context is not None <EOL> def _handle_launch_context ( self , ctx ) : <EOL> logging . debug ( "<STR_LIT>" . format ( ctx ) ) <EOL> if '<STR_LIT>' in ctx : <EOL> self . patient_id = ctx [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in ctx : <EOL> logging . warning ( "<STR_LIT>" ) <EOL> self . launch_context = ctx <EOL> self . save_state ( ) <EOL> @ property <EOL> def patient ( self ) : <EOL> if self . _patient is None and self . patient_id is not None and self . ready : <EOL> import models . patient <EOL> try : <EOL> logging . debug ( "<STR_LIT>" . format ( self . patient_id ) ) <EOL> self . _patient = models . patient . Patient . read ( self . patient_id , self . server ) <EOL> except FHIRUnauthorizedException as e : <EOL> if self . reauthorize ( ) : <EOL> logging . debug ( "<STR_LIT>" <EOL> . format ( self . patient_id ) ) <EOL> self . _patient = models . patient . Patient . read ( self . patient_id , self . server ) <EOL> except FHIRNotFoundException as e : <EOL> logging . warning ( "<STR_LIT>" . format ( self . patient_id ) ) <EOL> self . patient_id = None <EOL> self . save_state ( ) <EOL> return self . _patient <EOL> def human_name ( self , human_name_instance ) : <EOL> """<STR_LIT>""" <EOL> if human_name_instance is None : <EOL> return '<STR_LIT>' <EOL> parts = [ ] <EOL> for n in [ human_name_instance . prefix , human_name_instance . given , human_name_instance . family , human_name_instance . suffix ] : <EOL> if n is not None : <EOL> parts . extend ( n ) <EOL> return '<STR_LIT:U+0020>' . join ( parts ) if len ( parts ) > <NUM_LIT:0> else '<STR_LIT>' <EOL> def reset_patient ( self ) : <EOL> self . launch_token = None <EOL> self . launch_context = None <EOL> self . patient_id = None <EOL> self . _patient = None <EOL> self . save_state ( ) <EOL> @ property <EOL> def state ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . app_id , <EOL> '<STR_LIT>' : self . scope , <EOL> '<STR_LIT>' : self . redirect , <EOL> '<STR_LIT>' : self . patient_id , <EOL> '<STR_LIT>' : self . server . state , <EOL> '<STR_LIT>' : self . launch_token , <EOL> '<STR_LIT>' : self . launch_context , <EOL> } <EOL> def from_state ( self , state ) : <EOL> assert state <EOL> self . app_id = state . get ( '<STR_LIT>' ) or self . app_id <EOL> self . scope = state . get ( '<STR_LIT>' ) or self . scope <EOL> self . redirect = state . get ( '<STR_LIT>' ) or self . redirect <EOL> self . patient_id = state . get ( '<STR_LIT>' ) or self . patient_id <EOL> self . launch_token = state . get ( '<STR_LIT>' ) or self . launch_token <EOL> self . launch_context = state . get ( '<STR_LIT>' ) or self . launch_context <EOL> self . server = FHIRServer ( self , state = state . get ( '<STR_LIT>' ) ) <EOL> def save_state ( self ) : <EOL> self . _save_func ( self . state ) </s>
