Зміст курсу
Professional Web API with Flask
Professional Web API with Flask
About API
Imagine you're playing an online game on your phone, and you see your friend's high score automatically updated from another part of the world. Ever wondered how that happens? This magic is often made possible by something called a Web API.
What is a Web API?
API stands for Application Programming Interface. A Web API is a set of rules that allows different software applications to communicate with each other over the Internet. Think of it like a waiter in a restaurant: you (the customer) ask for something (like data), and the waiter (API) communicates your request to the kitchen (the server) and brings back what you asked for.
Real-life Examples
- Social Media Integration
Do you know how you can sign into a new game or app using your Facebook or Google account? That's a Web API in action! These platforms offer APIs that other apps can use to verify your identity without needing to create a new username and password. - Weather Updates
Ever checked the weather on your phone? Weather apps use APIs to fetch real-time weather data from various sources. So, when you're planning to go out and check if you need an umbrella, a Web API is working behind the scenes to get you the latest forecast. - Online Shopping
When you shop online, APIs are used extensively. For example, when you view a product, the website uses an API to retrieve the product details from a database. And when you pay, the website uses another API to process the payment securely.
How Do Web APIs Work?
- Request: it starts with a request from the client. The client can be a user, web browser, external application, or other API. This is when you ask for something, like searching for your favorite band on a music streaming app;
- Processing: the API takes your request and tells the server what you want. The server is a powerful computer that stores all the information;
- Response: the server finds the information and sends it back. The API then ensures that this data is displayed to you in a way you can understand.
There are different types of the Web APIs, like:
- SOAP
- REST
- GraphQL
They have different powers and weaknesses.
For example, SOAP is like a formal waiter in a restaurant. Just like a restaurant with top-notch security, SOAP APIs are very secure. They follow strict rules, ensuring a consistent experience.
But it is highly complex, so it can be like reading a complicated menu in a foreign language or following etiquette. They need more resources, like a waiter carrying a heavy silver platter, so it’s heavier than other APIs.
The REST is like a friendly waiter. They're easy to talk to and understand. They use HTTP requests (like the way you use words to order food) to access and use data. Since they are stateless, like a waiter who doesn't remember your past orders, they don't store information between requests. This means every time you ask for something, it's like a new conversation.
REST is simple: Just like a straightforward menu in a fast food restaurant, REST APIs are easy to work with. Scalability is another strength: They can handle lots of orders (or data requests) at once. But, it is Statelessness: Sometimes, remembering past interactions (state) can be useful, but REST APIs can't do that. And, It’s Over-fetching/Under-fetching: It's like getting a fixed meal combo when you only want a part of it or need more.
And GraphQL is like having a personal chef. You tell them exactly what you want, and they make it for you. Unlike REST or SOAP, where the menu is fixed, GraphQL lets you request only the data you need, making it super efficient. GraphQL is efficient: You can get exactly what you want, no more, no less. It’s flexible: Like a chef who can cook anything, GraphQL can handle many different data requests.
But it is complex for developers: The chef (developer) needs to know how to prepare a wide range of dishes (data). And it has performance issues: Sometimes, preparing a very custom order can take longer.
We will work with REST API and look at it in the coming chapter.
Дякуємо за ваш відгук!