def lambda_handler(event, context): try: auth_parameters = ParameterParser( event["requestContext"]["authorizer"]["claims"]) user_id = auth_parameters.get_required("cognito:username") holdings = get_holdings(user_id) holdings = json.dumps(holdings) return { "statusCode": 200, "body": holdings, "headers": { 'Access-Control-Allow-Headers': '*', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'OPTIONS,HEAD,POST,GET,PUT,DELETE' } } except ClientException as e: return e.get_client_response() except Exception as e: return { "statusCode": 500, "body": "Internal error", "headers": { 'Access-Control-Allow-Headers': '*', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'OPTIONS,HEAD,POST,GET,PUT,DELETE' } }
def lambda_handler(event, context): try: if event["body"] is None: raise InvalidRequest("Request body is required") body = json.loads(event["body"]) parameters = ParameterParser(body) auth_parameters = ParameterParser( event["requestContext"]["authorizer"]["claims"]) userId = auth_parameters.get_required("cognito:username") symbol = parameters.get_required("symbol") positionId = parameters.get_required("positionId") transactionDate = parameters.get_required("transactionDate") transactionId = parameters.get_required("transactionId") delete_transaction(userId, symbol, positionId, transactionDate, transactionId) return { "statusCode": 200, "body": "success", 'headers': { 'Access-Control-Allow-Headers': '*', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'OPTIONS,HEAD,POST,GET,PUT,DELETE' }, } except ClientException as e: return e.get_client_response()
def lambda_handler(event, context): try: auth_parameters = ParameterParser( event["requestContext"]["authorizer"]["claims"]) user_id = auth_parameters.get_required("cognito:username") bucket = os.environ["SANDBOX_SCRIPTS_BUCKET"] response = s3_client.list_objects(Bucket=bucket, Prefix=user_id) objects = response.get("Contents", []) return {"statusCode": 200, "body": json.dumps(objects, default=str)} except ClientException as e: return e.get_client_response()
def lambda_handler(event, context): try: path = event["pathParameters"] path_params = ParameterParser(path) auth_parameters = ParameterParser( event["requestContext"]["authorizer"]["claims"]) user_id = auth_parameters.get_required("cognito:username") symbol = path_params.get_required("symbol") holding = get_holding(user_id, symbol) holding = json.dumps(holding) return { "statusCode": 200, "body": holding, 'headers': { 'Access-Control-Allow-Headers': '*', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'OPTIONS,HEAD,POST,GET,PUT,DELETE' }, } except ClientException as e: return e.get_client_response()
def lambda_handler(event, context): try: if event["body"] is None: raise InvalidRequest("Request body is required") body = json.loads(event["body"]) parameters = ParameterParser(body) auth_parameters = ParameterParser( event["requestContext"]["authorizer"]["claims"]) user_id = auth_parameters.get_required("cognito:username") script_name = parameters.get_required("scriptName") script_body = parameters.get_required("scriptBody") object_key = os.path.join(user_id, script_name) bucket = os.environ["SANDBOX_SCRIPTS_BUCKET"] s3_client.put_object(Bucket=bucket, Body=bytes(script_body, "utf-8"), Key=object_key) return {"statusCode": 200, "body": "success"} except ClientException as e: return e.get_client_response()
def lambda_handler(event, context): try: auth_parameters = ParameterParser( event["requestContext"]["authorizer"]["claims"]) user_id = auth_parameters.get_required("cognito:username") portfolio = get_portfolio(user_id) portfolio = json.dumps(portfolio) return { "statusCode": 200, "body": portfolio, "headers": { 'Access-Control-Allow-Headers': '*', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'OPTIONS,HEAD,POST,GET,PUT,DELETE' } } except ClientException as e: log.warn(e) return e.get_client_response() except Exception as e: log.error(e) traceback.print_exc() return { "statusCode": 500, "body": "Internal error", "headers": { 'Access-Control-Allow-Headers': '*', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'OPTIONS,HEAD,POST,GET,PUT,DELETE' } }
def lambda_handler(event, context): try: path_parameters = ParameterParser(event["pathParameters"]) auth_parameters = ParameterParser( event["requestContext"]["authorizer"]["claims"]) user_id = auth_parameters.get_required("cognito:username") script_name = path_parameters.get_required("scriptName") object_key = os.path.join(user_id, script_name) bucket = os.environ["SANDBOX_SCRIPTS_BUCKET"] s3_client.delete_object(Bucket=bucket, Key=object_key) return {"statusCode": 200, "body": "success"} except ClientException as e: return e.get_client_response()
def lambda_handler(event, context): try: path_parameters = ParameterParser(event["pathParameters"]) auth_parameters = ParameterParser( event["requestContext"]["authorizer"]["claims"]) user_id = auth_parameters.get_required("cognito:username") script_name = path_parameters.get_required("scriptName") object_key = os.path.join(user_id, script_name) script_body = download_script(object_key) response = {"scriptName": script_name, "scriptBody": script_body} return {"statusCode": 200, "body": json.dumps(response)} except ClientException as e: return e.get_client_response()
def test_simple(): parser = ParameterParser({"hello": 3}) assert parser.get_required("hello") == 3
def test_optional(): parser = ParameterParser({"hello": 3}) assert parser.get_optional("hello", None) == 3
def test_default(): parser = ParameterParser({"hello": 3}) assert parser.get_optional("non_existent", None) is None
def test_failure(): parser = ParameterParser({"hello": 3}) with pytest.raises(InvalidRequest): parser.get_required("non_existent")
def test_nested(): parser = ParameterParser({"hello": {"world": 2}}) assert parser.get_required("hello.world") == 2