def has_dog_handler()

in backend_complete/src/main.py [0:0]


def has_dog_handler(event, _):
    # Try to decode a JSON document from the body
    try:
        body = json.loads(event["body"])
    except json.JSONDecodeError:
        return helpers.message({"message": "Invalid JSON document"}, 400)

    # Validate the JSON document
    if "photo" not in body:
        return helpers.message({"message": "Missing 'photo' key in body"}, 400)

    # Try to extract the photo from the JSON document
    try:
        photo = base64.b64decode(body["photo"])
    except binascii.Error:
        return helpers.message({"message": "Invalid base64 string for 'photo'"}, 400)

    # Check if there is a dog
    dog = has_dog(photo)

    # Store if there was a dog or not as a custom metric
    helpers.metric("DogNoDog", "Dog", int(dog))

    return helpers.response({"dog": dog})