Introduction to Microservices and APIs
Overview
- Microservices: Services invoked remotely over the internet, providing access to data and services without needing to host them locally.
- APIs (Application Programming Interfaces): Also known as microservices; provide lightweight services available via simple interfaces and protocols over the internet.
- Use Cases: Message sending, text translation, streaming service searches, accessing COVID-19 data, airline timetables, geographic data, food nutrition data, weather data, etc.
- Internal Use: Companies like Facebook and Twitter use microservices internally to manage various functionalities.
What is an API?
- Functions called remotely over the internet, akin to local function calls but executed on different machines.
- Example: A client program calls an 'add' function that sends parameters over the internet to a server, which computes the result and sends it back.
- Communication: Standard web protocols like HTTP are used to package parameters and results.
- HTTP Methods: GET, POST
- Data Formats: JSON (JavaScript Object Notation) and XML (Extensible Markup Language)
JSON and XML
- JSON:
- Syntax used in JavaScript for representing objects (similar to Python dictionaries).
- Key-value pairs structure, can represent any JavaScript data structure.
- Simple text format, easy to transport over the internet.
- XML:
- More complex than JSON, can be extended using a schema.
- Describes data structures in a structured text format.
- Independent of programming languages.
Public Microservices and API Aggregators
- API Aggregators: Provide access to many microservices, making it easier to search and experiment with APIs.
- Example: RapidAPI, offers documentation and gateways to APIs (free and paid accounts available).
Accessing Microservices with Python
- Python’s support for HTTP protocols makes it easy to access microservices.
- Example: Using OpenWeatherMap API to get weather data.
- Requires API key and structured URL with city name and country code.
Example Code: Fetching Weather Data
import requests
import json
from apikey import API_key
url = "http://api.openweathermap.org/data/2.5/weather?q=Sydney,AU&appid=" + API_key
response = requests.get(url)
weather_data = json.loads(response.text)
print(weather_data)
Building Your Own Microservice with Python
- Framework: Bottle, a simple web framework for Python.
- Steps:
- Install Bottle module.
- Define endpoint using decorators.
- Write function to handle requests and return results.
- Run Bottle server.
Example Code: Bottle Microservice
Server Code
from bottle import route, run
import json
@route('/add/<a:int>/<b:int>')
def add(a, b):
result = {'sum': a + b}
return json.dumps(result)
run(host='localhost', port=8080)
Client Code
import requests
import json
url = "http://localhost:8080/add/1/2"
response = requests.get(url)
result = json.loads(response.text)
print(result)
Scalability and Cloud Services
- Scalability Concerns: Simple web servers like Bottle are not scalable for large numbers of clients.
- Cloud Solutions: AWS Lambda, API Gateway, etc., provide scalable cloud functions.
Conclusion
- Microservices and APIs are integral to modern internet systems, providing scalable and maintainable solutions for various functionalities.
- Available in many forms, both through public access and cloud services, making it easier to implement and manage.
Example Aggregators: RapidAPI