Build a web API server at explosive speed using hug

Introduction

hug is a web application framework dedicated to building web API servers. While other frameworks are packed with rich features such as template engine and OR mapper, hug is a framework that specializes only in the features needed for Web API servers.

hug Official Site

Of particular note is its simplicity and speed. The following is the benchmark posted on the official page (benchmark by Pycnic). スクリーンショット 2016-08-02 11.33.56.png It's slightly less fast than Falcon, but it's still very fast and much easier to implement than Falcon.

Let's install it and touch it.

Installation

hug is only compatible with Python 3 series. Installation is done below.

pip install hug

Hug for the first time

Now let's create an API. It's very easy to implement. The following is an example implementation of a server that returns a simple response.

hello_hug.py


import hug


@hug.get('/hello')
def hello(name):
    """Says Hello to a user"""
    return "Hello {}!".format(name)

The part @ hug.get () is the HTTP method decorator. Besides get, there are post and update.

Let's run it. You can do it below. When executed, the server will start up on port 8000, and when accessed, a response will be returned.

hug -f hello_hug.py

Try accessing [http: // localhost: 8000 / hello? Name = hug](http: // localhost: 8000 / hello? Name = hug) in your browser. You should see the response.

You can also view a simple document by accessing [http: // localhost: 8000 / documentation](http: // localhost: 8000 / documentation).

{
    "404": "The API call you tried to make was not defined. Here's a definition of the API to help you get going :)",
    "documentation": {
        "handlers": {
            "/hello": {
                "GET": {
                    "usage": "Says Hello to a user",
                    "outputs": {
                        "format": "JSON (Javascript Serialized Object Notation)",
                        "content_type": "application/json"
                    },
                    "inputs": {
                        "name": {
                            "type": "Basic text / string value"
                        }
                    }
                }
            }
        }
    }
}

Versioning

API versioning is also easy with hug. Implementing versioning is also very easy. Take a look at the code below.

versioning_example.py


# filename: versioning_example.py
"""A simple example of a hug API call with versioning"""
import hug

@hug.get('/echo', versions=1)
def echo(text):
    return text


@hug.get('/echo', versions=range(2, 5))
def echo(text):
    return "Echo: {text}".format(**locals())

To do versioning, just specify a number or range in versions.

Run below.

hug -f versioning_example.py

[http: // localhost: 8000 / v1 / echo? Text = Hi](http: // localhost: 8000 / v1 / echo? Text = Hi) and [http: // localhost: 8000 / v2 / echo? Text = Go to Hi](http: // localhost: 8000 / v2 / echo? Text = Hi) and compare the differences. You can see that it is easy to version.

Summary

In addition to the versioning mentioned above, there are other useful functions for creating APIs. I'm especially happy that asynchronous processing can be done easily.

I think that it is suitable for the following situations.

We hope that you will try it out and experience its speed and ease of use.

Recommended Posts

Build a web API server at explosive speed using hug
Build a speed of light web API server with Falcon
Learning neural networks using Chainer-Creating a Web API server
Easily build a DNS server using Twisted
Build a web server on your Chromebook
[Mac] Build a Python 3.x environment at the fastest speed using Docker
Explosive speed with Python (Bottle)! Web API development
Prepare a pseudo API server using GitHub Actions
[Part 2] Let's build a web server on EC2 Linux
Implement APIs at explosive speed using Django REST Framework
Create a pseudo REST API server using GitHub Pages
CTF beginner tried to build a problem server (web) [Problem]
Try to solve Sudoku at explosive speed using numpy
Build a lightweight Fast API development environment using Docker
Build a seq2seq model using keras's Functional API Model building & learning
Easily build a GCP environment for Kaggle at high speed
Explosive speed! Using Python Simple HTTP Server for kintone development
Try multivariable correlation analysis using Graphical lasso at explosive speed
Image Optimize on the server side using TinyPNG's Web API
Build a Flask development environment at low cost using Docker
I tried to build a service that sells machine-learned data at explosive speed with Docker
Turn your Android Smart Phone into a Web Server using python.
Make a rain notification bot for Hangouts Chat at explosive speed
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
Proxy measures when using WEB API
Creating a web application using Flask ②
Build a go environment using Docker
Build a web application with Django
Creating a web application using Flask ①
Create API using hug with mod_wsgi
Creating a web application using Flask ③
Create a CRUD API using FastAPI
Creating a web application using Flask ④
Set up a local web server in 30 seconds using python 3's http.server
Start a web server using Bottle and Flask (I also tried using Apache)
Build a proxy server with nginx on multiple remote servers using Ansible
Build a Pypi cache server on QNAP
Build a simple WebDAV server on Linux
Build a Fast API environment with docker-compose
Set up a mail server using Twisted
[TPU] [Transformers] Make BERT at explosive speed
Build a Samba server on Arch Linux
I tried to make a Web API
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server --2 PHP introduction
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server ―― 1. Apache introduction
The epidemic forecast of the new coronavirus was released on the Web at explosive speed
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server ―― 1. Apache introduction
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server --2 PHP introduction