🎓 All Courses | 📚 Python Programming Syllabus

📋 Study this course on TaskLoco
⚡ Key Concept #python-programming#apis#rest#requests

Consuming a REST API — Full Example

Let's use the free Open Meteo weather API (no key required).


import requests

def get_weather(latitude, longitude):
    url = "https://api.open-meteo.com/v1/forecast"
    params = {
        "latitude": latitude,
        "longitude": longitude,
        "current_weather": True,
        "temperature_unit": "fahrenheit"
    }

    try:
        response = requests.get(url, params=params, timeout=10)
        response.raise_for_status()
        data = response.json()
        weather = data["current_weather"]
        return {
            "temp": weather["temperature"],
            "wind": weather["windspeed"],
            "code": weather["weathercode"]
        }
    except requests.RequestException as e:
        print(f"Error: {e}")
        return None

result = get_weather(40.7128, -74.0060)  # New York
if result:
    print(f"Temperature: {result['temp']}°F")

Patterns used: params dict for query strings, timeout, raise_for_status, try/except, nested JSON access.


YouTube • Top 10
Python Programming: Working with APIs — Real Example
Tap to Watch ›
📸
Google Images • Top 10
Python Programming: Working with APIs — Real Example
Tap to View ›

Reference:

Wikipedia: REST

image for linkhttps://en.wikipedia.org/wiki/Representational_state_transfer

📚 Python Programming — Full Course Syllabus
📋 Study this course on TaskLoco

TaskLoco™ — The Sticky Note GOAT