RPC vs REST, Understanding Using Python Implementation

Coderrohanpahwa
Stackademic
Published in
3 min readJan 30, 2024

--

There are several types of APIs, each serving different purposes and catering to various needs of Developers. Here are some common types:

  1. RestAPI
  2. GraphQL APIs
  3. RPC (Remote Call Procedure)
  4. SOAP APIs
  5. Streaming APIs

In this blog, we will be talking about what RPC is and how it is different from RESTful Architecture.

Before moving forward, you must know what REST Apis are and how we utilise them.

What are RPC?

RPC stands for Remote Procedure Call that allows a computer program to execute on another computer , without the programmer explicitly coding the details for this remote interaction. It is used to call other processes on the remote systems like a local system.

Implementation of REST and RPCs

As we all know in REST API, we perform operations using GET, PUT , POST, PATCH and DELETE methods, we call these methods and we get the response, but same does not happen in case of RPC protocol. In RPC protocol, server has a function and we made a request on an endpoint which has a function name and arguments with it and response is returned. Let me show how we can implement using python.

REST API :

from flask import Flask, jsonify, request

app = Flask(__name__)

# Sample data - In a real-world application, you would use a database
tasks = [
{
'id': 1,
'title': 'Task 1',
'description': 'This is task 1',
'done': False
},
{
'id': 2,
'title': 'Task 2',
'description': 'This is task 2',
'done': False
}
]

# GET request to fetch all tasks
@app.route('/tasks', methods=['GET'])
def get_tasks():
return jsonify({'tasks': tasks})

# GET request to fetch a single task by its id
@app.route('/tasks/<int:task_id>', methods=['GET'])
def get_task(task_id):
task = [task for task in tasks if task['id'] == task_id]
if len(task) == 0:
return jsonify({'error': 'Task not found'}), 404
return jsonify({'task': task[0]})

# POST request to create a new task
@app.route('/tasks', methods=['POST'])
def create_task():
if not request.json or not 'title' in request.json:
return jsonify({'error': 'The title is required'}), 400
task = {
'id': tasks[-1]['id'] + 1,
'title': request.json['title'],
'description': request.json.get('description', ""),
'done': False
}
tasks.append(task)
return jsonify({'task': task}), 201

# PUT request to update an existing task
@app.route('/tasks/<int:task_id>', methods=['PUT'])
def update_task(task_id):
task = [task for task in tasks if task['id'] == task_id]
if len(task) == 0:
return jsonify({'error': 'Task not found'}), 404
if not request.json:
return jsonify({'error': 'No data provided'}), 400
task[0]['title'] = request.json.get('title', task[0]['title'])
task[0]['description'] = request.json.get('description', task[0]['description'])
task[0]['done'] = request.json.get('done', task[0]['done'])
return jsonify({'task': task[0]})

# DELETE request to delete a task
@app.route('/tasks/<int:task_id>', methods=['DELETE'])
def delete_task(task_id):
task = [task for task in tasks if task['id'] == task_id]
if len(task) == 0:
return jsonify({'error': 'Task not found'}), 404
tasks.remove(task[0])
return jsonify({'result': True})

if __name__ == '__main__':
app.run(debug=True)

Example of a REST GET Request

Rest Get Call Example

Similarly, we can make POST, PUT, PATCH and DELETE request and get the response in Rest APIs.

Now, I will show how RPC architecture works.

import xmlrpc.server

class MyServer:
def add(self, a,b):
return a+b

# creating an instance of SimpleXMLRPCServer
server = xmlrpc.server.SimpleXMLRPCServer(("localhost", 8000))


# MyServer() creates an instance of the class MyServer, and the register_instance method of the XML-RPC server registers this instance.
server.register_instance(MyServer())


# enters a loop to continuously listen for and handle incoming XML-RPC requests
server.serve_forever()

Here I have implemented an RPC server using xmlrpc package, (JSON is also supported using json-rpc package). You can see here I have make a class MyServer in which I have a method called add.

Conclusion:
We have examined the implementation of both Rest and RPCs. The decision on whether to implement Rest or RPCs depends on the specific use case. RPCs are employed when there is a need to invoke remote functions on a server.

Stackademic 🎓

Thank you for reading until the end. Before you go:

--

--