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.
Of particular note is its simplicity and speed. The following is the benchmark posted on the official page (benchmark by Pycnic). 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.
hug is only compatible with Python 3 series. Installation is done below.
pip install hug
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"
}
}
}
}
}
}
}
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.
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.