๐
STUDENT NOTES ยท DAY 15 OF 24
Talking to the Internet ๐
Weather, cricket scores, memes โ the internet is full of data. An API is the polite way for YOUR code to ask for it.
1What is an API?
๐
Py: An API is a restaurant menu for programs: the service says "here's what you can ask for and how" โ your code orders, the server delivers.
๐ค
Bittu: I ask the weather API for Pune. It answers in 0.2 seconds. No umbrella needed for negotiations.
2requests.get โ placing the order
๐ฆ
Professor Hoot: Status codes: 200 = OK โ
, 404 = not found ๐คท, 500 = server having a bad day ๐ฅ. Learn the big three.
3JSON โ the language servers speak
Servers reply in JSON โ which looks almost exactly like Python dicts and lists! .json() converts it:
Pretend Weather API โ pick a city!
โฆpress a city to send the requestโฆ
4Keep your key secret! ๐
๐
Bugly: Post your API key on GitHub and strangers will use YOUR quota within minutes. Hehehe. I love careless coders.
๐
Py: Treat keys like ATM PINs: never share, never paste in public code. We'll use environment variables in class.
5Quick quiz! ๐ง
The server replies with status 404. Meaning?
๐ก 200 OK, 404 not found, 500 server error โ the holy trinity of status codes.
r = requests.get(url). How do you turn the JSON reply into Python data?
๐ก .json() parses the reply into dicts and lists you can use immediately.
6Your mission ๐
Mission 1: In class-shared code, call the weather API for YOUR city and print the temperature.
Mission 2: Print the status code. Then break the URL on purpose and see what code you get.
Mission 3: Explore the JSON reply: print data.keys() to see what else the server sent you.
Day 15 done โ the internet answers you now! ๐
Next: put it all together โ build a real Weather App.
Go to Day 16: Weather App โ