(use the Space key to navigate through all slides)
Prof. Andrea Gallegati |
Prof. Dario Abbondanza |
Allow different programs to communicate efficiently.
They bridge between software components, making reusability, modularity, and automation easier.
✅ Operating System APIs
open()
, read()
, write()
✅ Library APIs
with open("file.txt", "r") as file:
data = file.read()
print(data)
import math
# Uses math API to
# calculate the square root
print(math.sqrt(25))
✅ Hardware APIs
import RPi.GPIO as GPIO
# Turn on a device connected to pin 18
GPIO.output(18, GPIO.HIGH)
Method | What It Does |
---|---|
GET | Retrieve data (e.g., weather, user details) |
POST | Send new data (e.g., register a user) |
PUT | Update existing data (e.g., change user email) |
DELETE | Remove data (e.g., delete a user account) |
🔹 This requests GitHub API data and prints the response.
import requests
response = requests.get("https://api.github.com")
print(response.json()) # Convert response to a dictionary
Most modern APIs follow the REST model (Representational State Transfer), but not all.
✅ Uses standard HTTP methods (GET, POST, etc.).
✅ Returns structured data (usually JSON).
✅ Stateless (each request does not depend on previous ones).
🔹 This fetches user information from a public API.
import requests
url = "https://jsonplaceholder.typicode.com/users/1"
response = requests.get(url)
print(response.json()) # Display user data
🔹 This fetches country data using GraphQL.
import requests
url = "https://countries.trevorblades.com/"
query = {"query": "{ country(code: \"IT\") { name, capital } }"}
response = requests.post(url, json=query)
print(response.json())
A Framework created by Hakim El Hattab and contributors
to make stunning HTML presentations