<@ULVA73B9P> generate a function that fetches data...
# ask-marvin
g
@Marvin generate a function that fetches data form open-mateo.com/en/docs in python
m
Alright, let's create a Python function to fetch data from the OpenMateo API. Here’s a simple script that performs this task using the
requests
library:
Copy code
python
import requests

def fetch_open_mateo_data(endpoint: str, params: dict = None) -> dict:
    """
    Fetch data from the OpenMateo API.

    Args:
        endpoint (str): The API endpoint to fetch data from.
        params (dict, optional): Parameters to include in the API request.

    Returns:
        dict: The response data from the API.
    """
    base_url = "<http://open-mateo.com/en/docs>"
    url = f"{base_url}/{endpoint}"

    try:
        response = requests.get(url, params=params)
        response.raise_for_status()  # Raise an HTTPError for bad responses
        return response.json()  # Assuming the API returns JSON
    except requests.exceptions.HTTPError as http_err:
        print(f"HTTP error occurred: {http_err}")
    except Exception as err:
        print(f"An error occurred: {err}")

    return None

# Example usage:
if __name__ == "__main__":
    data = fetch_open_mateo_data("weather", {"location": "Berlin", "date": "2023-10-01"})
    if data:
        print(data)
### Explanation - Base URL and Endpoint: Combining the base URL with the endpoint to form the full URL for the API request. - Parameters: The function accepts additional parameters for the API request, passed as a dictionary. - Handling Responses: Basic error handling to catch HTTP errors and other exceptions. The function returns the JSON response if the request is successful, otherwise
None
. ### Requirements To run this script, you need the
requests
library. If you do not have it installed, you can install it via pip:
Copy code
sh
pip install requests
If you need more specific details or modifications, feel free to ask!