def lambdaProductsFromList(event, *args): productsFromList = Event.parseDataClass(ProductsFromList, event) result: pd.DataFrame = ProductDatabase.productsFromList( productsFromList.iprcodes) results: List[ProductDatabase] = ProductDatabase.fromDf(result) resultDicts: List[dict] = ProductDatabase.toListDict(results) return Response.returnSuccess(resultDicts)
def updateWithS3(self, data, inputKeyName = 'input-data-name', invocationType = InvocationType.event, user= None, pw= None): # put users if not specified user = user or self.user; pw = pw or self.pw # extract function name and inputbucket name inputBucketName = self.functionNames.inputBucket() functionName = self.functionNames.updateS3() logging.info(f'bucket is {inputBucketName}') # save data to s3 S3.save(key = inputKeyName, objectToSave = data , bucket = inputBucketName, user=user, pw=pw) logging.info(f'data is saved to s3, invoking ingestion function') # call lambda function inputValue = Event(body = json.dumps({ 'key': inputKeyName })).to_dict() logging.info(f'input to lambda is {inputValue}') lambdaResponse = self.lambdaClient.invoke( functionName= functionName , input=inputValue, invocationType= invocationType ) logging.info(f'lambdaResponse is {lambdaResponse}') if invocationType == 'Event': return "successfully sent event, please watch your slack" if lambdaResponse: return self.returnLambdaResponse(lambdaResponse)
def lambdaSingleQuery(event, _): key, value = Event.from_dict(event).firstKey() try: result = ProductDatabase.singleProductQuery({key: value}).to_dict() except Exception as e: return Response.returnError(f'{e}') return Response.returnSuccess(body=result)
def createData(event:dict, hashKeyName: str,mainClass:Model, schemaUrl:Optional[str] = None ,schemaFormat:str ='yaml', *args): ''' create a new basket ''' # parse output query:dict = Event.parseBody(event) # check schema if provided if schemaUrl: try: validateUrl(schemaUrl,format_ = schemaFormat) except ValidationError as e: return Response.returnSuccess(f'{e}') # check for key if hashKeyName not in query: return Response.returnError(message=f'missing {hashKeyName}') # check if object exist if next(mainClass.query(query[hashKeyname]),None): return Response.returnError(message=f'item with the same hash key exists') # make pynamodb object item:mainClass = mainClass.fromDict(query) # try to save try: item.save() return Response.returnSuccess(body=item.to_dict()) except ValidationError as e: # error validation handle return Response.returnError(f'validation error \n {e}') except Exception as e: # error handle return Response.returnError(f'unknown error \n {e} \n errorString())')
def dtconvert(event, context): body = Event.parseBody(event) if not all(key in body for key in ['date', 'formatter']): return Response.returnError(message='missing date or formatter keys') timeStamp = datetime.strptime(body['date'], body['formatter']).timestamp() return Response.returnSuccess(body = str(timeStamp))
def generalInvoke(self, body={}, function='unauth'): functionName = self.functionName.getName(function) event = Event.getInput(body=body) response = self.lambda_.invoke(functionName=functionName, input=event) try: return Response.parseBody(response) except: return response
def querySingleProduct(self, iprcode = '0171670', user=None, pw=None): '''query a single product''' #extract function name functionName = self.functionNames.singleQuery() query = {'iprcode': iprcode} try: inputValue = Event.getInput(query) lambdaResponse = self.lambdaClient.invoke( functionName = functionName , input = inputValue ) return self.returnLambdaResponse(lambdaResponse) except: print('calling lambda failed')
def lambdaUpdateS3(event, _): inputKeyName = Event.from_dict(event).key() try: updateResult = ProductDatabase.updateS3Input( inputBucketName=INPUT_BUCKET_NAME, key=inputKeyName) except: ProductDatabase.notify(f'error updating with s3 {errorString()}') return Response.returnError(errorString()) ProductDatabase.notify(f'success update {updateResult}') return Response.getReturn(body=updateResult)
def updateData(event: dict, hashKeyName: str, mainClass: Model, schemaUrl: Optional[str] = None, schemaFormat: str = 'yaml', *args): ''' updating data based on the new input event:dict: object gathered from apiGatewayProxy hashKeyName:str: the name of the hash key mainClass:Model: pynamodb model class for this object schemaUrl:Optional[str]: url of the input schema for validation schemaFormat:Enum['yaml', 'json'] ''' # parse output query: dict = Event.parseBody(event) # check schema if provided if schemaUrl: try: validateUrl(schemaUrl, format_=schemaFormat) except ValidationError as e: return Response.returnSuccess(f'{e}') # check for key if hashKeyName not in query: return Response.returnError(message=f'missing {hashKeyName}') # check if object exist item: mainClass = next(mainClass.query(query[hashKeyName]), None) if not item: return Response.returnError( message=f'item with the same hash key doesnt exist') # update the data item.update(query) # try to save try: item.save() return Response.returnSuccess(body=item.toDict()) except ValidationError as e: # error validation handle return Response.returnError(f'validation error \n {e}') except Exception as e: # error handle return Response.returnError(f'unknown error \n {e} \n errorString())')
def lineBot(event, *args): e = Event.from_dict(event) body = e.getBody() try: replyToken = getReplyToken(body) except: return Response.getReturn(statusCode=400, body={'error': 'cant get reply token'}) try: sendReply(replyToken, ACCESSTOKEN) except: return Response.getReturn(statusCode=400, body={'error': 'response token not valid'}) response = {'replyToken': replyToken} return Response.getReturn(body=response, statusCode=200, headers={'hello': 'hello'})
def queryList(self,iprcodes:List[str])->List[dict]: return self.generalInvoke(functionName=self.functionNames.listQuery() ,payload=Event.getInput({'iprcodes': iprcodes}))
def lambda_handler(event, *args): body = Event.parseBody(event) return Response.returnSuccess(body={'message': 'helloWorld'})
def helloworld(event, *args): print(Event.parseBody(event)) return Response.returnSuccess()
def lambdaUpdateProduct(event, _): products = Event.parseBody(event)['products'] convertedProducts = ProductDatabase.convertIntCols(products) result = ProductDatabase.valueUpdate2({'items': convertedProducts}) return Response.getReturn(body=result)