Posts Related To Development (21)
How To Install Docker in Ubuntu 20.04
Docker is an application that allows you to manage application processes using containers. These containers work in a similar manner as virtual machines but are rather more portable, resource-friendly and OS-dependant. In this tutorial, you’ll install and use Docker Community Edition (CE) on Ubuntu 20.04.
Test Your Locally Running App On Another Device
Have you ever wondered how you can view your locally running application on another device such as your mobile phone? You probably would love to get feedback on your application before deploying, but you are faced with the fact that your computer has a firewall that does not allow for external access. The good news is that it is possible to bypass this limitation and still share a link to your application with a friend who lives on a different part of the world for testing purposes.
Start A Flask Server
Flask is a lightweight Python framework for web applications that provides the basics for URL routing and page rendering. Its philosophy aims to keep the core simple but extensible. Over the years, there has been considerable design changes that have been made to Flask, but the core has largely remained the same. This article is going to explore how you can start your Flask development server to test an application that is running locally in your machine.
Install and Configure Virtualenvwrapper In Ubuntu 20.04
Virtual environments are key to safely tinkering with different versions of Python and combinations of packages. They also allow you to install different versions of the same library for different projects, which resolves what would be impossible if all of your projects' requirements were installed in the same environment.
Virtualenvwrapper is a set of extensions for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.
Connect to GitHub Using SSH
You can connect to GitHub using the Secure Shell Protocol (SSH), which provides a secure channel over an unsecured network. This protocol can connect and authenticate remote servers and services. With SSH keys, you can connect to GitHub without supplying your username and personal access token at each visit.
Install and Configure Git In Ubuntu
Git
is software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Its goals include speed, data integrity, and support for distributed, non-linear workflows.
Git allows and encourages you to have multiple local branches that can be entirely independent of each other. The creation, merging, and deletion of those lines of development takes seconds.
Install Elasticsearch In Ubuntu 20.04 Localhost
If you are looking at utilizing Elasticsearch, the free and open-source search and analytics tool, you have to familiarize yourself with the technical setup details. In this article, you will learn how to download, install and start the Elasticsearch service. You will also briefly learn how to use sample JSON data to ingest and index search queries in your Flask app.
Application Programming Interfaces (APIs) In Flask
An Application Programming Interface is a low-level entry point into an application that a client can use to work with the application in a more direct way than through the traditional web browser workflow. Think of it as an intermediary layer that processes data between systems, through which a company can share its application's data and functionality with external third-party developers, partners, or even internal departments.
In this article, you will learn the basic principles that help guide the development of an API. There are several types of APIs. We will examine the most popular API among them all, the REST API.
Implement An API Blueprint In Flask
A blueprint is a logical structure that represents a subset of the application. We can write a blueprint in a separate Python package that encapsulates the elements related to that specific feature of the application, in this case, an API. The essence of using blueprints in your Flask app is to allow for the adaptation of the application as it becomes larger or as the application's needs change.
Representation Of Resources In Flask API
Before building any API, it is important to decide what the representation of its resources is going to be. Both the server and the client must agree on a format to represent a resource. Most modern APIs use the JavaScript Object Notation (JSON) format to build resource representations, though an API can support multiple representation formats when properly defined in their content negotiation options.
Handling Errors In Flask API
What happens when an error occurs in a Flask application? We would create custom HTML error pages such as 404.html
or 500.html
that display more user-friendly content. Flask would return these pages instead of a rather scary and oftentimes too revealing stack trace.
In the context of APIs, we would need a "machine-friendly" type of error, something that the client application would easily interpret.
Unique Resource Identifiers In Flask API
A URI (Uniform Resource Identifier) is a string of characters that identifies a resource on the Internet. They are achieved by assigning a unique URL to each resource. For example, if a database has a user identified by the username "muthoni", her unique URL would be something such as /users/muthoni
where muthoni
is the identifier assigned to the user in the database. Such an endpoint can be reasonably implemented in an API.
API Authentication In Flask
In a typical web application, there are certain web pages that require a user to login in order to access their content. Such pages require a client to identify, authenticate, and authorize a user in an effort to preserve their integrity. Traditionally, an anonymous user will be redirected to an HTML login page from where they will be verified.
It is also possible to achieve the same level of security when it comes to accessing an application's endpoints. Because there is no concept of HTML or login pages in an API, the server needs to return an appropriate response to the client to inform a user that they need to supply valid credentials.
API-friendly Error Messages In Flask
We can go to extra lengths to provide custom error-handling mechanisms in an API, but there are some errors that would bypass these efforts and instead return HTML responses.
The HTTP protocol supports a mechanism by which the client and the server can agree on the best format for a response, called content negotiation. The client needs to send an Accept
header with the request, indicating the format preferences. The server then looks at the list and responds using the best format it supports from the list offered by the client.
API Testing Using Postman
The HTTPie package is a fantastic command-line tool that can be used to test endpoints designed for testing, debugging, and generally interacting with APIs & HTTP servers. Another popular API-testing tool is Postman. It offers both a web and desktop client for creating, testing, sharing, and documenting APIs. While using Postman, for testing purposes, one doesn't need to write any HTTP client network code. Instead, we build test suites called collections and let Postman interact with the API.