import requests ''' Demo to show that we can call an API from Python. Author: Tim Pierson, Dartmouth CS61, Spring 2026 Modified from Gemini example ''' if __name__ == "__main__": # Replace with your actual local or hosted URL url = "http://localhost:8080/restaurants" try: # 1. Send the GET request response = requests.get(url) # 2. Check the status code we set in the Flask route if response.status_code == 200: restaurants = response.json() # Automatically parses the 'jsonify' data print(f"Retrieved {len(restaurants)} restaurants:") for r in restaurants: print(f"ID: {r[0]} | Name: {r[1]} | Cuisine: {r[3]}") elif response.status_code == 500: print("Server Error:", response.json().get("message")) else: print(f"Unexpected Error: {response.status_code}") except requests.exceptions.ConnectionError: print("Could not connect to the server. Make sure your Flask app is running!") ''' #try doing a post and sending some data #uncomment to run # Replace with your actual local or hosted URL url = "http://localhost:8080/restaurants" try: # 1. Send the POST request data = {"RestaurantID": 1112, "RestaurantName": "Test Restaurant", "Boro": "Manhattan", "Building": "123", "Street": "Main Street Avenue", "ZipCode": 10069, "Phone": 1234567, "Latitude": 111.11, "Longitude": 222.22, "CuisineID": 1 } response = requests.post(url, json=data) # 2. Check the status code we set in the Flask route if response.status_code == 201: resp = response.json() # Automatically parses the 'jsonify' data print(f"Received: {resp}") elif response.status_code == 500: print("Server Error:", response.json().get("message")) else: print(f"Unexpected Error: {response.status_code}") except requests.exceptions.ConnectionError: print("Could not connect to the server. Make sure your Flask app is running!") '''