Session-3 | 10th Aug 2022

Visit HOME

Deatiled topics and the corresponding reference links

Sl No Topic Details last update
1 REST API & it's varities YouTube Lesson 2022-08-20
2 Introduction to Flask YouTube Lesson 2022-08-20
3 Py Program with API Features YouTube Lesson 2022-08-20

Let's first understand what is API

API is a method which allows multiple clients or browser apps to communicate with server considering security, performance & ease of use for API consumers.
Suppose we are accessing the popular app Tripview to locate the current position of the public transport. This app is basically integrating with google maps via API - Application Programming Interface.
The UI of google maps and the Tripview map is different and Tripview is sourcing data from the app through API layer.

APIs providing the platform and medium to the applications for communicationg with each other with a structured way of passing information

REST API

A REST API is an application programming interface that conforms to specific architectural constraints, like stateless communication and cacheable data (not a protocol or standard).
While REST APIs can be accessed through a number of communication protocols, most commonly, they are called over HTTPS, so the guidelines below apply to REST API endpoints that will be called over the internet.

REST API Authetication Guidelines

Accept/ Respond with JSON: REST APIs should accept JSON for request payload and also send responses to JSON because it si standard for transferring data, Server-side libraries supports decoding of JSON, JavaScript is having built-in feature to encode/decode JSON
REST API can use Express back end framework for node.js and the bode-parser middleware to parse teh json request body and at the end can response JSON through res.json

Use nouns instead of verbs in endpoint paths:

Having verbs in API endpoint makes it unnecessary long.In the below code, the path names do not have any verbs, only nouns. The verbs are in the HTTP verbs.The POST, PUT, and DELETE endpoints all take JSON as the request body, and they all return JSON as the response, including the GET endpoint.

Using Noun instead Verb-Endpoint Path

Use logical nesting on endpoints:

When an object can contain another object,endpoint should be designed in that way. It may help to avoid giving attackers unnecessary information by avoiding mirroring database structure in the endpoints.
In the below code, Get method path nesting indicates 'comments' is the child resource of '/articles/:articleId'. The same can be followed for Post, Put & Delete. Logical Nesting in Endpoint

Handle errors gracefully and return standard error codes:

Upon occurance of error, actul error message with the correct error code should be displayed to avoid confusion with API users like '400 Bad Request" or '401 Unauthorized' or '403 Forbidden' or 502 Bad Gateway' etc.

Allow filtering, sorting, and pagination:

Filtering and pagination both increase performance by reducing the usage of server resources

Details of REST API

A REST API (also known as RESTful API) is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services. Like other architectural styles, REST has its guiding principles and constraints. These principles must be satisfied if a service interface needs to be referred to as RESTful.

In simple words, in the REST architectural style, data and functionality are considered resources and are accessed using Uniform Resource Identifiers (URIs). The resources are acted upon by using a set of simple, well-defined operations. Also, the resources have to be decoupled from their representation so that clients can access the content in various formats, such as HTML, XML, plain text, PDF, JPEG, JSON, and others.

The clients and servers exchange representations of resources by using a standardized interface and protocol. Typically HTTP is the most used protocol, but REST does not mandate it. Metadata about the resource is made available and used to control caching, detect transmission errors, negotiate the appropriate representation format, and perform authentication or access control. And most importantly, every interaction with the server must be stateless. All these principles help RESTful applications to be simple, lightweight, and fast.

FLASK

Flask is a micro framework for web development in the Python language. What micro framework means specifically is that it comes with very little in terms of available features and boilerplate code at the start of a project’s development. Now that’s not to say there aren’t libraries upon libraries of features and plugins available for the Flask framework, but by default, Flask will tend to not include a feature unless you as a developer have specified otherwise.

This differs largely from frameworks like Rails or Django (the latter being another highly popular Python web framework), where a large amount of features and conveniences are generated for you on project creation. For example, both of these frameworks provide a Database-Abstraction layer that allows developers to easily read and write to databases through object models (e.g. Rails through Active Model, Django through their Object-relational Mapper).

The downside of a micro framework is obviously the lack of these features on start-up. Many if not all of the features in the more extensive frameworks exist in the Flask library, but they have to be included explicitly. The major upside, however, is that if you aren’t going to use those features, it’s a lot easier to just make a Flask app and avoid needing to delete a lot of unnecessary files and dependencies. Micro framework applications come with a lot less code bloat, and can always be scaled up with additional tools as needed.

**However there is one big downside of FLASK. IT DOES NOT support Dynamic HTML pages. FLASK vs Django

@app.route essentially a python decorator, followed by a function. It is a means mapping the URLs to a specific function that will handle the logic for that URL.

Sample Programs

Basic Calculator Function

basic_calculator_function

Using FLASK pass 3 PArameter to perform arithmatic calculation

Flask_3Params_ArithmaticCalculation