Skip to content

Fish Info

This module provides routes and functions for retrieving detailed fish information from a JSON data source.

get_fish_info(fish_name)

Retrieve information about a specific fish by its name.

Parameters:

Name Type Description Default
fish_name str

The name of the fish to retrieve information for.

required

Returns:

Type Description
Response

A JSON response containing fish details if found, otherwise a 404 error with an error message.

Source code in src/routes/fish_info.py
Python
def get_fish_info(fish_name: str):
    """
    Retrieve information about a specific fish by its name.

    Args:
        fish_name (str): The name of the fish to retrieve information for.

    Returns:
        (Response): A JSON response containing fish details if found, otherwise
                  a 404 error with an error message.
    """
    fish_name = fish_name.lower()

    if fish_name in fish_list.keys():
        fish_data = fish_list[fish_name]

        # Return all fish details in a single JSON response
        return jsonify({
            "name": fish_data["name"],
            "image": fish_data["imageURL"],
            "sellPrice": fish_data["sellPrice"],
            "location": fish_data["location"],
            "size": fish_data["size"],
            "time": fish_data["time"],
            "nhMonths": fish_data["nhMonths"],
            "shMonths": fish_data["shMonths"]
        })

    response: Response = jsonify({"error": "Fish not found"})
    response.status_code = 404  # Set status code explicitly
    return response

fish_info_route(app)

Register the fish information retrieval route with the Flask app.

Parameters:

Name Type Description Default
app Flask

The Flask application instance.

required

Returns:

Type Description
None

This just registers the /fish-info route.

Source code in src/routes/fish_info.py
Python
def fish_info_route(app: Flask):
    """
    Register the fish information retrieval route with the Flask app.

    Args:
        app (Flask): The Flask application instance.

    Returns:
        (None): This just registers the /fish-info route.
    """
    @app.route("/fish-info/<fish_name>", methods=["GET"])
    def wrapped_get_fish_info(fish_name: str):
        """
        Handle GET requests to retrieve fish information by name.

        Args:
            fish_name (str): The name of the fish to retrieve information for.

        Returns:
            Response: JSON response with fish details or an error message.
        """
        return get_fish_info(fish_name)