API
API enables different software systems to communicate with each other.
Web API
Web APIs are used to enable communication between different software applications over the
internet.
RESTful API
RESTful APIs typically return data in a standard format such as JSON of XML, and use
standard HTTP status codes to indicate the success or failure of a request.
Socket Programming
Socket programming is a method of communication between two computers using a network
protocol.
Basic Example
Server Side:
import socket
# Create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to a specific address and port
server_address = ('localhost', 12345)
server_socket.bind(server_address)
# Listen for incoming connections
server_socket.listen(1)
print('Server is listening for incoming connections...')
# Accept a connection from a client
client_socket, client_address = server_socket.accept()
print(f'Connected to {client_address}')
# Send data to the client
message = 'Hello, client! How are you?'
client_socket.send(message.encode('utf-8'))
# Receive data from the client
data = client_socket.recv(1024)
print(f'Received from client: {data.decode("utf-8")}')
# Close the connection
client_socket.close()
server_socket.close()
Client Side
import socket
# Create a socket object
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the server
server_address = ('localhost', 12345)
client_socket.connect(server_address)
print('Connected to the server.')
# Receive data from the server
data = client_socket.recv(1024)
print(f'Received from server: {data.decode("utf-8")}')
# Send data to the server
message = 'I am good, thank you!'
client_socket.send(message.encode('utf-8'))
# Close the connection
client_socket.close()
RESTful API
A REST API (Representational State Transfer Application Programming Interface) is a set of
rules and conventions for building and interacting with web services.
HTTP Methods: RESTful APIs use standard HTTP methods for performing operations on
resources. The most common methods are:
GET: Retrieve a resource or a collection of resources.
POST: Create a new resource.
PUT: Update an existing resource or create a new resource if it doesn't exist.
DELETE: Remove a resource.
Different B/W PUT and PATCH
PUT (Update or Create):
Semantics:
The PUT method is used to update a resource or create a new resource if it doesn't exist.
When using PUT to update a resource, the client typically sends the entire updated representation
of the resource to the server.
Idempotent:
PUT is considered idempotent, meaning that making the same request multiple times should
have the same effect as making it once.
Use Case:
Updating an entire resource:
When we have the complete representation of a resource and we want to replace the existing
resource with a new representation.
Example
PUT /users/123
Content-Type: application/json
{
"id": 123,
"name": "Updated Name",
"email": "updated@email.com"
}
PATCH (Partial Update):
Semantics:
The PATCH method is used to apply partial modifications to a resource.
Unlike PUT, PATCH is designed to apply only the changes (or patches) to the resource, not the
entire representation.
Idempotent:
PATCH is not guaranteed to be idempotent, meaning that making the same request multiple times
may have different effects.
Use Case:
Updating part of a resource: When you only want to update specific fields or properties of a
resource, rather than replacing the entire resource.
Example
PATCH /users/123
Content-Type: application/json
{
"email": "updated@email.com"
}
Use Cases Comparison:
PUT:
● Use PUT when you have the complete updated representation of the resource.
● It's suitable for full updates and replaces the entire resource with the new representation.
● Idempotent, so it can be retried without changing the result.
PATCH:
●
● Use PATCH when you want to apply partial updates to a resource.
● It's suitable for making changes to specific fields or properties of a resource.
● Not guaranteed to be idempotent, so caution should be taken when retrying requests.
HTTP Response Code:
Architecting RESTful API
OPEN API:
Tutorial :
https://support.smartbear.com/swaggerhub/docs/en/get-started/openapi-3-0-tutorial.html
Specification: https://swagger.io/specification/
Swagger:
Live demo:
https://petstore.swagger.io/?_gl=1*fkn2kq*_gcl_au*NDY4NjI5Njg5LjE3MDIyNzEwODA.&_ga=2.
124225034.1628147164.1704344002-157843619.1702271081
Caching:
Node.js:
https://medium.com/the-node-js-collection/simple-server-side-cache-for-express-js-with-node-js-
45ff296ca0f0
Circuit breaker:
https://medium.com/javarevisited/what-is-circuit-breaker-in-microservices-a94f95f5e5ae
Audit and Log :
https://backstage.forgerock.com/docs/am/7/REST-guide/rest-logging.html?fbclid=IwAR2JIw8pb
MdX0HN-N3R73O-xhZaLdtJX33yPd3XNNpHPmGNSA2U-S8imovQ
Calling and audit:
https://developers.klaxoon.com/docs/auditlog-examples?fbclid=IwAR0KetdNgqMyOW8yX2gxq
GE7qdB63x0yAIq4DwLdt2uFZt1FFoQGZpwsGqs
API reference (audit):
https://developers.klaxoon.com/docs/auditlog-examples?fbclid=IwAR0KetdNgqMyOW8yX2gxq
GE7qdB63x0yAIq4DwLdt2uFZt1FFoQGZpwsGqs