Prompt Details
Model
Chat - GPT-4 (gpt-4)
Token size
268 ($0.00800 / call)
Example input
[language]: Python, [API base URL]: https://api.openweathermap.org/data/2.5, [authentication method]: API key, [endpoint]: /weather, [input parameters]: city_name, api_key, [desired output]: dictionary with temperature, humidity, and weather description.
Example output
import requests
def get_weather_data(city_name: str, api_key: str) -> dict:
"""
Retrieve the current weather data for a given city using the OpenWeatherMap RESTful API.
Args:
city_name (str): The name of the city for which the weather data is requested.
api_key (str): The user's unique OpenWeatherMap API key.
Returns:
dict: A dictionary containing the current temperature, humidity, and weather description for the given city.
If an error occurs or the city is not found, the dictionary will contain an error message.
"""
# Define the base URL and endpoint for the API request
base_url = "https://api.openweathermap.org/data/2.5"
endpoint = "/weather"
# Create the request URL by combining the base URL, endpoint, city name, and API key
request_url = f"{base_url}{endpoint}?q={city_name}&appid={api_key}"
# Send the request to the API and store the response
response = requests.get(request_url)
# Check if the response status code indicates success (200)
if response.status_code == 200:
# Parse the JSON response
data = response.json()
# Extract the required information (temperature, humidity, and weather description)
temperature = data['main']['temp']
humidity = data['main']['humidity']
weather_description = data['weather'][0]['description']
# Return the extracted information as a dictionary
return {
"temperature": temperature,
"humidity": humidity,
"weather_description": weather_description
}
# If the response status code indicates an error, return an appropriate error message
elif response.status_code == 404:
return {"error": "City not found."}
else:
return {"error": f"An error occurred while fetching the weather data. (Status Code: {response.status_code})"}
# Example usage:
api_key = "your_api_key"
city_name = "San Francisco"
weather_data = get_weather_data(city_name, api_key)
print(weather_data)
Get this prompt + 9 more with Select$14 your first month, then $19/mo$14 first month, then $19/mo
View SelectBy purchasing this prompt, you agree to our terms of service
GPT-4
API Integration Expert simplifies your API interactions by leveraging ChatGPT-4's capabilities to generate custom code snippets tailored to your specific RESTful API. Define your API details, input parameters, and desired output, and let the AI handle authentication, requests, and response parsing. Save time, enhance reliability, and streamline your API integration tasks with this powerful prompt, designed to accelerate development and improve code quality.
...more
Added over 1 month ago

