def _log_request(logger, event): method = get_or_default(event, "httpMethod", "unknown") path = get_or_default(event, "path", "unknown") path_params = get_or_default(event, "pathParameters", {}) querystring = get_or_default(event, "queryStringParameters", {}) logger.info(f"START: {method} {path}/{path_params}?{querystring}") logger.info(event)
def _parse_request(event): request = {} # Parse S3 objects from the event with S3 bucket name and S3 object key if get_or_default(event, "Records"): request["documents"] = [] for record in event["Records"]: document = {} document["s3_bucket_name"] = get_or_default( record, "s3.bucket.name") document["s3_object_key"] = get_or_default(record, "s3.object.key") request["documents"].append(document) return request
def _parse_request(event): request = {} request["partnerId"] = get_or_default(event, "pathParameters.partnerId") partner_id = request["partnerId"] if partner_id is not None and str(partner_id).isdigit(): request["partnerId"] = int(partner_id) request["partnerUuid"] = get_or_default( event, "queryStringParameters.partnerUuid") request["partnerName"] = get_or_default( event, "queryStringParameters.partnerName") request["partnerStatus"] = get_or_default( event, "queryStringParameters.partnerStatus") return request
def _validate_request(request): errors = [] # Validate S3 objects with S3 bucket name and S3 object key attributes = ["s3_bucket_name", "s3_object_key"] documents = get_or_default(request, "documents") if not documents: errors.append("mandatory docuemnts are not provided") return errors for document in request["documents"]: for attribute in attributes: if not get_or_default(document, attribute): errors.append(f"mandatory {attribute} is not provided") return errors
def _validate_request(request): errors = [] if not request: errors.append("request body is empty or invalid JSON") return errors attributes = [ "partnerUuid", "productExternalId", "pirateSourceExternalId", "detectionTs", "infringementUrl", "infringementStatus", ] for attribute in attributes: if get_or_default(request, attribute) is None: errors.append(f"mandatory {attribute} is not provided") if errors: return errors partner_uuid = request["partnerUuid"] if partner_uuid is not None and not re.match( r"^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$", partner_uuid): errors.append("mandatory partnerUuid must be a UUID") product_external_id = request["productExternalId"] # pylint: disable=len-as-condition if product_external_id is not None and not 0 < len( product_external_id) < 51: errors.append("mandatory productExternalId is too short or too long") pirate_source_external_id = request["pirateSourceExternalId"] if (pirate_source_external_id is not None and not 0 < len(pirate_source_external_id) < 51): errors.append( "mandatory pirateSourceExternalId is too short or too long") detection_ts = request["detectionTs"] if detection_ts is not None and not is_valid_timestamp( detection_ts, TS_FORMAT): errors.append(f"mandatory detectionTs must be valid: {TS_FORMAT}") infringement_url = request["infringementUrl"] if infringement_url is not None and not 7 < len(infringement_url) < 501: errors.append("mandatory infringementUrl is too short or too long") infringement_status = request["infringementStatus"] if infringement_status is not None and not 1 < len( infringement_status) < 31: errors.append("mandatory infringementStatus is too short or too long") if "infringementScreenshot" not in request: request["infringementScreenshot"] = None infringement_screenshot = request["infringementScreenshot"] if infringement_screenshot is not None and not get_or_default( request, "infringementScreenshot.screenshotUrl"): errors.append( "optional infringementScreenshot is missing screenshotUrl array") return errors
def _validate_product_record(record): errors = [] attributes = [ "partner_uuid", "product_external_id", "product_title", "first_protection_ts", "registration_ts", "protection_status", ] for attribute in attributes: if not get_or_default(record, attribute): errors.append(f"mandatory {attribute} is not provided") attribute = "product_image_url" if attribute in record and not get_or_default(record, attribute): errors.append(f"optional {attribute} is not provided") return errors
def _parse_request(event): request = {} request["partnerId"] = get_or_default(event, "queryStringParameters.partnerId") partner_id = request["partnerId"] if partner_id is not None and str(partner_id).isdigit(): request["partnerId"] = int(partner_id) request["productId"] = get_or_default(event, "pathParameters.productId") product_id = request["productId"] if product_id is not None and str(product_id).isdigit(): request["productId"] = int(product_id) request["productTitle"] = get_or_default( event, "queryStringParameters.productTitle" ) request["protectionStatus"] = get_or_default( event, "queryStringParameters.protectionStatus" ) return request
def _parse_request(event): request = {} request["partnerId"] = get_or_default(event, "queryStringParameters.partnerId") partner_id = request["partnerId"] if partner_id is not None and str(partner_id).isdigit(): request["partnerId"] = int(partner_id) request["pirateSourceId"] = get_or_default(event, "pathParameters.pirateSourceId") pirate_source_id = request["pirateSourceId"] if pirate_source_id is not None and str(pirate_source_id).isdigit(): request["pirateSourceId"] = int(pirate_source_id) request["pirateSourceName"] = get_or_default( event, "queryStringParameters.pirateSourceName" ) request["pirateSourceType"] = get_or_default( event, "queryStringParameters.pirateSourceType" ) return request
def _parse_request(event): request = {} body = get_or_default(event, "body") if body == "null": return request try: request = json.loads(body) except Exception: return request return request
def decorated(logger, f1, event, context, *args, **kwargs): request_id = get_or_default(event, "requestContext.requestId", str(uuid.uuid4())) def execution_context_filter(log_record): log_record.request_id = request_id return True logger.addFilter(execution_context_filter) result = original(logger, f1, event, context, *args, **kwargs) logger.removeFilter(execution_context_filter) return result
def _validate_infringement_record(record): errors = [] attributes = [ "partner_uuid", "product_external_id", "pirate_source_external_id", "detection_ts", "infringement_url", "infringement_status", ] for attribute in attributes: if not get_or_default(record, attribute): errors.append(f"mandatory {attribute} is not provided") return errors
def _validate_config(local_config): errors = [] # Validate RDS endpoint and credentials attributes = [ "db_host", "db_port", "db_name", "db_user", "db_password", "db_connect_timeout", ] for attribute in attributes: if not get_or_default(local_config, attribute): errors.append(f"mandatory {attribute} is not provided") return errors
def _parse_request(event): request = {} request["partnerId"] = get_or_default(event, "queryStringParameters.partnerId") partner_id = request["partnerId"] if partner_id is not None and str(partner_id).isdigit(): request["partnerId"] = int(partner_id) request["partnerUuid"] = get_or_default( event, "queryStringParameters.partnerUuid") request["productId"] = get_or_default(event, "queryStringParameters.productId") product_id = request["productId"] if product_id is not None and str(product_id).isdigit(): request["productId"] = int(product_id) request["pirateSourceId"] = get_or_default( event, "queryStringParameters.pirateSourceId") pirate_source_id = request["pirateSourceId"] if pirate_source_id is not None and str(pirate_source_id).isdigit(): request["pirateSourceId"] = int(pirate_source_id) request["infringementStatus"] = get_or_default( event, "queryStringParameters.infringementStatus") request["sinceTs"] = get_or_default(event, "queryStringParameters.sinceTs") since_ts = request["sinceTs"] if since_ts is not None: request["sinceTs"] = re.sub(r" (\d+)$", r"+\1", since_ts) request["tillTs"] = get_or_default(event, "queryStringParameters.tillTs") till_ts = request["tillTs"] if till_ts is not None: request["tillTs"] = re.sub(r" (\d+)$", r"+\1", till_ts) request["limit"] = get_or_default(event, "queryStringParameters.limit") limit = request["limit"] if limit is not None and str(limit).isdigit(): request["limit"] = int(limit) request["offset"] = get_or_default(event, "queryStringParameters.offset") offset = request["offset"] if offset is not None and str(offset).isdigit(): request["offset"] = int(offset) return request