Package: guard-exception
guard_exception- guard all exception for swift run pythoninstall- pip install guard-exceptionfunctions- guardExceptionWithOutParams: execute function without parameters
- guardException: execute function with parameters
- guardSearchKeyException: look for a key
- guardSearchKeryRecursiveException: look for a key recursively
- guardExceptionIsIterable: return 1 or 0, if exists key on dicexampleimportguard_exceptionasgegg=ge.guard_exception()defdivide(a,b):returna/bprint(gg.guardException(divide,a=1,b=0))# result cli# {'data:': None, 'error': 'division by zero'}example on swift with PythonKitimportPythonKitlethvac=Python.import("hvac")letge=Python.import("guard_exception")letgg=ge.guard_exception()letclient=hvac.Client(url:"http://0.0.0.0:8200")letuser="incorrect_user"letparams=Python.dict()params["username"]=PythonObject(user)params["password"]=PythonObject("incorrect_password")print(gg.guardException(client.login,url:"v1/auth/userpass/login/\(user)",use_token:true,json:params))// result: cli//{'data': None, 'error': 'invalid username or password, on post http://0.0.0.0:8200/v1/auth/userpass/login/incorrect_user'}example complete on swift// guardException// guardSearchKeyException// guardSearchKeryRecursiveExceptionimportVaporimportPythonKitlethvac=Python.import("hvac")letge=Python.import("guard_exception")letgg=ge.guard_exception()// token structure for authentication// Content: help resolve the api response -> jsonpublicstructToken:Content{publicvarclient_token:String!publicvaraccessor:String!publicvarpolicies:[String]!publicvartoken_policies:[String]!}// swift class for authenticationpublicclassautentication{//MARK:- Propertiesletclient=hvac.Client(url:"http://0.0.0.0:8200")vartoken:Token?//MARK:- Initializerspublicinit(){}publicfunclogin(user:String,password:String)->Token{letparams=Python.dict()params["username"]=PythonObject(user)params["password"]=PythonObject(password)letgenerated=Python.dict(gg.guardException(self.client.login,url:"v1/auth/userpass/login/\(user)",use_token:true,json:params))// result genereted/*{'data':{'request_id': '45464674xxxxxxxb','lease_id': '','renewable': False,'lease_duration': 0,'data': None,'wrap_info': None,'warnings': None,'auth': {'client_token': 'hvs.CAESasdfasdfxxxxxxxxxxxxxxxxxxx','accessor': 'TvExxxxxxxxxxxxxxx1ESqjW','policies': ['default'],'token_policies': ['default'],'metadata': {'username': 'rafael'},'lease_duration': 2764800,'renewable': True,'entity_id': 'dadxxxf8-xxxx-xxxxx-xxxxx-xxxxxxxxxxx','token_type': 'service','orphan': True,'mfa_requirement': None,'num_uses': 0}},'error': None}*/letauth=gg.guardExceptionIsIterable(generated,key:"data")ifInt(auth["data"])!>0{letclient_token=String(gg.guardSearchKeryRecursiveException(generated,key:["data","auth","client_token"])["data"])letaccessor=String(gg.guardSearchKeryRecursiveException(generated,key:["data","auth","accessor"])["data"])letpolicies=[String](gg.guardSearchKeryRecursiveException(generated,key:["data","auth","policies"])["data"])!lettoken_policies=[String](gg.guardSearchKeryRecursiveException(generated,key:["data","auth","token_policies"])["data"])!ifclient_token!=nil{self.token=Token(client_token:client_token,accessor:accessor,policies:policies,token_policies:token_policies)}}returnself.token??Token(client_token:nil,accessor:nil,policies:[],token_policies:[])}}