Seminar 11: Object-based VS Resource-centered architectures
Goal: To understand the difference between object-based architectural styles for example pure RPCs or RMI and resource-based architectural styles that involves resource/data centers like REST in distributed systems.
Background: An application built with a resource-centered architecture does more of its processing internally (e.g. as opposed to calling external components) but uses external resources as input. E.g. its core processing consists of retrieving static resources and then doing more calculating internally. They are typically used in big, open systems, because of the advantages it brings. In ROA architectures you would typically find RESTful services. RESTful services are nowadays typically implemented with just JSON over HTTP, or XML over HTTP.
On the other hand, object-based architecture is based on the division of responsibilities for an application or system into individual reusable and self-sufficient objects. It maps the application to real world objects for making it more understandable and makes it easy to manage errors during execution. E.g, Pure RPC and RMI
REST APIs
A REST API is a way for computer systems to communicate using the HTTP technologies found in web browsers and servers. For example, retrieving and updating account information in a social media application.
Restful Architecture
- State and functionality are divided into distributed resources - Accessibility via normal HTTP commands of GET, POST, PUT or DELETE.
- The architecture is client/server, stateless, layered, and supports caching - typical architecture can be that of a web server - server and web browser - client
General steps for code migration using REST API in Python:
- Identify the parts of your existing code that need to be migrated and decide which functionality should be exposed as REST API endpoints.
- Create a Flask web application to serve as the RESTful API endpoint.
- Define the necessary routes and endpoints for the API in your Flask application. For example, you might create a route to retrieve data from the database, another route to create new data, and so on.
- Implement the functionality of each API endpoint by calling the relevant code in your existing application or system.
- Test the API endpoints using a tool like Restman/Postman or a similar testing framework.
Exercise: We have an existing codebase that handles calculator application and we need to migrate the functionality to a REST API endpoint. So we are required to create an API endpoint to perform basic operations such as addition, subtraction, multiplication and division.
Open a command prompt and install the Flask and requests modules using pip
$ pip install Flask $ pip install requests
Flask is a widely used micro web framework for creating APIs in Python. Code for implementation is given here (Code)
Also any download the API endpoint tool. For chrome browsers RestMan
In the code,
- We define four REST API endpoints for the four components of our calculator application
- Each endpoint receives a JSON payload containing the operands for the arithmetic operation and returns a JSON response containing the result
- Test the REST API endpoints using a simple client-side application in python
- This sends a POST request to the /api/add endpoint with the operands 2 and 3 in the JSON payload. url is http://localhost:5000/api/add in the extension of your browser (Restman/Postman)
- Deploy the Python Flask application to a production environment such as server or cloud platform
Results should be similar to the below screenshots
Task: Migrate the functionality of any part of an application or function to a REST API endpoint using similar procedure from the exercise above. e.g dictionary, inventory updates e.t.c
Deliverables: For the task, please contain the source python file, and also the screenshot of results in terminals and endpoint tool. Then put all the file in a single zip.
Relative links: