Exemplo n.º 1
0
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 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 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 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 lambdaDumpOnlineS3(event, *args):
    print(f'ecommece col list is {ECOMMERCE_COL_LIST}')
    # get all products from db
    df: pd.DataFrame = pd.DataFrame([i.data for i in ProductDatabase.scan()])
    # get online list from ECOMMERCE_COL_LIST
    onlineList: List[str] = yaml.load(requests.get(ECOMMERCE_COL_LIST).content)
    # filter df for item
    ## condition 1 master online is true
    condition1 = df['master_online'] == True
    ## condition 2 hema_name_en is not blank
    condition2 = df['hema_name_en'] != ''
    ## filtered df
    onlineDf: pd.DataFrame = df[condition1 & condition2].loc[:, onlineList]
    ### log shape and size
    print('shape is:', onlineDf.shape)
    print('size is:',
          sys.getsizeof(onlineDf.to_json(orient='split')) / 1e6, 'Mb')

    # save file as gzip
    key = 'onlineData'
    bucket = INVENTORY_BUCKET_NAME
    path = '/tmp/inventory.json'
    ## export to gzip
    onlineDf.to_json(path, orient='split', compression='gzip')
    ## upload file to s3
    S3.saveFile(key=key,
                path=path,
                bucket=bucket,
                ExtraArgs={
                    **ExtraArgs.gzip,
                    **ExtraArgs.publicRead
                })
    return Response.returnSuccess()
Exemplo n.º 6
0
 def returnLambdaResponse(lambdaResponse:dict):
   try:
     return Response.fromDict(lambdaResponse).body
   except:
     logging.exception(f'error parsing body, perhaps there is no body in response\
     response is {lambdaResponse}')
     logging.error(lambdaResponse)
Exemplo n.º 7
0
def getData(hashKeyName: str, mainClass: Model):
    '''
    create a new basket
  '''
    # get data
    try:
        r: Optional[Model] = next(mainClass.query(hashKeyName), None)
    except Exception as e:
        return Response.returnError(f'failed to query with error {e}')

    # product not found
    if not r:
        return Response.returnError(f'not found')
        #success
    else:
        return Response.returnSuccess(r.toDict())
Exemplo n.º 8
0
 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
Exemplo n.º 9
0
 def allQuery(self):
   functionName = self.functionNames.allQuery()
   lambdaResponse = self.lambdaClient.invoke(
     functionName = functionName, input = {}
   )
   url = Response.fromDict(lambdaResponse).body['url']
   result = Requests.getContentFromUrl(url)
   return result
Exemplo n.º 10
0
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'})
Exemplo n.º 11
0
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())')
Exemplo n.º 12
0
 def generalInvoke(self, functionName, payload):
   lambdaResponse = self.lambdaClient.invoke(
     functionName = functionName, input = payload
   )
   try:
     response:Response = Response.fromDict(lambdaResponse)
   except:
     print('unable to parse response')
     print(lambdaResponse)
     raise Exception(errorString())
   if response.statusCode == 200:
     return response.body
   else:
     print('error')
     return response.body
   return self.returnLambdaResponse(lambdaResponse)
Exemplo n.º 13
0
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 lambdaAllQueryFeather(event, *args):
    key = 'allData'
    bucket = INVENTORY_BUCKET_NAME
    url = ProductDatabase.allQuery(bucket=INVENTORY_BUCKET_NAME, key=key)
    hashCode = pdUtils.loadRemoteHash(key=key, bucket=bucket, useUrl=True)
    return Response.getReturn(body={'url': url, 'hash': hashCode})
def lambdaDumpToS3(event, _):
    result = ProductDatabase.cacheDb(limit=500)
    lastItem = result.iloc[0].to_json()
    return Response.returnSuccess({'result': json.loads(lastItem)})
def lambdaUpdateProduct(event, _):
    products = Event.parseBody(event)['products']
    convertedProducts = ProductDatabase.convertIntCols(products)
    result = ProductDatabase.valueUpdate2({'items': convertedProducts})
    return Response.getReturn(body=result)
Exemplo n.º 17
0
def lambda_handler(event, *args):
    body = Event.parseBody(event)
    return Response.returnSuccess(body={'message': 'helloWorld'})
Exemplo n.º 18
0
def edit(event, *args):
    return Response.getReturn(body = {'method':'edit'})
Exemplo n.º 19
0
def remove(event, *args):
    return Response.getReturn(body = {'method':'remove'})
Exemplo n.º 20
0
def get(event, *args):
    return Response.getReturn(body = {'method':'get'})
Exemplo n.º 21
0
def add(event, *args):
    return Response.getReturn(body = {'method':'add'})
Exemplo n.º 22
0
def helloworld(event, *args):
  print(Event.parseBody(event))
  return Response.returnSuccess()
def lambdaAllQuery(event, *args):
    url = ProductDatabase.allQuery(bucket=INVENTORY_BUCKET_NAME,
                                   key='allData-json.zl')
    return Response.getReturn(body={'url': url})