Skip to content

(A). REST API Tutorial Overview

Zamathuli Luthuli edited this page Nov 30, 2023 · 9 revisions

Introduction

Before venturing into this tutorial, let us expand on understanding an API, REST, REST API, and REST API with Flask.

What is an API?

API stands for 'Application Programming Interface.' It is a set of rules, routines, and tools for building software programs. APIs enable software applications to communicate and share data and functionality; below is a reference diagram for your review.

APIs define the methods, functions, and data structures for interacting with a specific software component or service. They offer an easier way for developers to use another software system without worrying about the technical intricacies.

Example:

Let's assume you have a mobile app that needs to access data from a server. In this scenario, the API acts as a middleman between the app (the client) and the server, allowing them to exchange data and information.

Once again, APIs are a bridge for software applications to communicate and share data with other systems or services. They are essential in software development and fundamental to creating efficient and feature-rich applications.

How does an API work?

First, APIs come in various types, each serving specific purposes and use cases. The standard APIs are web APIs, which use an HTTP protocol to enable communication between applications over the Internet. Web APIs allow applications to access endpoint URLs for sending and receiving requests and responses. Each endpoint corresponds to a particular operation or functionality provided by the API.

This tutorial will primarily focus on Web APIs (Restful APIs). For an in-depth understanding of APIs types, follow this link: Common Types of APIs.

Here are the basic steps of how an API works:

  1. The source API publishes the API documentation, which defines the rules and specifications for interacting with an API. The documentation provides information on available endpoints, request methods, data formats, authentication requirements, and usage policies.
  2. An API consumer or client (such as a mobile or web app) sends a call (an HTTP request) to the API. The request includes an endpoint URL, parameters, headers, and sometimes an API key or authentication token.
  3. An API server processes the incoming request. It determines if the request is valid and if the consumer has the required permissions and performs the required actions.
  4. After processing the request, an API server generates a response. The response usually includes an HTTP status code, confirmation of the action's success status, headers, and the requested data. Depending on the API's design, the data can be in various formats, such as JSON, XML, or HTML.
  5. An API consumer receives the response from the API and can then use the provided data or functionality in its application. For example, if it's a weather API, the consumer's application can display weather information to the user.

Example:

The following example illustrates a basic 'Python Library' that saves data to a database. The 'library' has an API: the 'save_to_db' and 'get_from_db' functions, which it makes available to other programs (or parts of programs); so, those programs should use these functions to store and retrieve data from a database.

def save_to_db(what_to_save):
    pass

def get_from_db(query):
    pass

Therefore, the library exposes its functions through an API, enabling other programs to use its functionality without writing their database access code. That reduces code duplication and simplifies the building of more complex applications by leveraging the library's functionality. Please see the illustration shown in the diagram below.

What is REST?

REST stands for Representational State Transfer, a software architectural style for building web applications. It is a set of constraints that define how web services should communicate, built around the concept of resources and identified by URIs (Uniform Resource Identifiers). REST mainly applies when creating web APIs, enabling communication between different software systems over the Internet.

The main idea behind REST is to utilize an HTTP protocol as a communication mechanism between clients and servers. RESTful services provide a uniform interface for accessing and manipulating resources. Standard HTTP request methods, often called CRUD (Create, Read, Update, Delete) operations, are crucial in defining this interface. Following consistent operations enhances accessibility and promotes scalability, simplicity, and interoperability in distributed systems.

What is a REST API?

A REST API is a web API that uses HTTP and follows the principles of the REST architecture. The client and server communicate by exchanging representations of resources, typically in formats like JSON or XML. Hence, a REST API relies on a client-server architecture. Here is a simple diagram depicting the REST API.

What is a resource?

In a REST API, a resource is an object or representation of data that clients can access and manipulate using HTTP request methods through a unique URI (Uniform Resource Identifier). A resource can have a unique identifier, such as a user, a product, a blog post, or a photo.

For example, an e-commerce website might have a 'products' resource that allows clients to retrieve, create, update, or delete products. Each product would have a unique URI, such as /products/123, where 123 is the product ID.

A client can interact with a RESTful API by forwarding a request to a specific URL, along with any required parameters and authentication credentials. The server will respond with a representation of the requested resource in a particular format, such as JSON, XML, or HTML.

What is a client?

In a REST API, a client is a software application that makes requests to a server for resources or services over the HTTP protocol. The client can be a web browser, a mobile application, a desktop application, or any other software that can send and receive HTTP requests.

The main characteristics or constraints of a REST API are:

  1. Client-Server Architecture: The architecture comprises independent client and server components. The client handles the user interface and experience, while the server processes requests, manages resources, and handles business logic. This separation allows for better scalability and flexibility.
  2. Stateless: The server doesn't retain any information about previous client requests; it treats each request as brand new. The server also requires specific information from the client to personalize the response.
  3. Uniform interface: The interface between the client and server should be consistent, with resources identified by URIs and represented in a standardized format, such as JSON or XML.
  4. Cacheable: Responses from the server should be explicitly marked as cacheable or non-cacheable, which allows for the efficient use of caching mechanisms.
  5. Layered System: The REST API has several layers with specific roles and responsibilities. The client interacts with the outermost layer (server) while remaining unaware of the inner layers, promoting modularity and scalability.

What is a Flask REST API?

Flask is a popular web framework for Python that allows you to create web applications and APIs. A Flask REST API is a web API built using the Flask framework that adheres to REST principles, an architectural style for building distributed systems.

Flask provides several extensions that make it easy to build a REST API, such as Flask-RESTful and Flask-RESTPlus. These extensions offer features such as request parsing, input validation, and documentation generation that simplify building a REST API.

Overall, a Flask REST API allows you to expose your application's functionality to other applications or services in a standardized and easily accessible way.

What is a Flask Stores REST API?

Flask Stores REST API is a web application programming interface (API) built using Flask. This popular Python web framework allows users to interact with a database of stores using RESTful principles.

With a Flask Stores REST API, the data store might be a database, file system, or any other type of persistent data storage. The API provides a set of endpoints (URLs) that map to specific resources in the data store, and clients can use HTTP methodssuch as GET, POST, PUT, and DELETE to interact with these endpoints and perform various CRUD (Create, Read,** Update**, Delete) operations.

For example, Kurro Flask Stores REST API is a store-related website that allows customers to add stores and items. The API provides endpoints for retrieving store and item information, adding items to a shopping cart, and checking out, among other operations. Other applications could also use the API to perform inventory management or order-tracking tasks.

To interact with the Kurro Flask Stores REST API, users can send HTTP requests to specific endpoints defined in the API, such as /stores to retrieve a list of all stores or /stores/string: name/items to retrieve a list of items associated with a specific store. The API then responds with data formatted in a specified way, such as JSON.

Conclusion

  1. There is no fundamental difference between a REST API designed for standard web services and one designed for cloud services. The principles of REST remain consistent, irrespective of the hosting location. It is crucial to consider specific factors and best practices when developing a REST API for cloud services.
  2. The diagrams in this article are solely for illustration purposes and do not aim to depict the REST API for cloud services.

That brings us to the end of this topic. To access an outline for the REST API, we will build in this tutorial; please follow this link:

REST API Tutorial Outline

Clone this wiki locally