“This is the 14th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

preface

Recently, I found what I claim is the next generation of high performance Api development tool, which is Hug.

How does Hug compare to Django, Flask, FastApi and other Api development tools?

I’ll take a look together today.

About Hug

The heart of Hug is that it greatly simplifies Api development compared to Flask.

The purpose of the Hug

  • letApiDevelopment is extremely simple
  • Encourage programmers to write self-documenting code
  • Fast, so that developers don’t choose another framework because of performance
  • According to theHug ApiWriting tests will also be easier
  • Solve problems in the framework rather than letting users solve them
  • Called the next generationApiThe foundation of technology

As a result, Hug is based on Falcon’s high-performance HTTP framework and supports only Python@3

Practice (see how easy it is)

Install the Hug

pip install hug --upgrade
Copy the code

demo

code

Start the service

Hug started the development server for us on local port 8000 and we went to http://localhost:8000 to see the results

Sigh, So Easy!

Small eggs

Above, we defined the happyNewYear method to be bound to the root path, but Hug allowed us to leave the path unbound.

By default, Hug will automatically Reload after we change the code.

Code (just remove the decorator path argument)

The effect of visiting http://localhost:8000

Although no paths are defined, Hug provides us with a friendly 404 and tells you the URIs, requests, examples, and output of the paths you can access.

We try to Hug the see the effect after the modification request path for http://localhost:8000/happyNewYear

Of course, defining a path and not defining a path cannot be mixed in the same method.

The path parameter

As with most Api frameworks, these basic features are supported, and this article will show you just how to use the Hug path parameter.

code

The effect

Api Version Management

Managing and defining interface versions in Hug is particularly easy and convenient by adding the Version parameter to the Hug decorator.

code

The effect

As mentioned above, it is very easy to manage and refactor Api version numbers with Hug.

Test Hug Api

In addition to making development and testing simple, we will now use Hug to test Hug.

The test code

import hug
import demo
from hug import HTTP_200

def testHpny() :
    @api_or_module: Api module, i.e. Api file @url: i.e. Url, removing part of host and port.
    resp = hug.test.get(api_or_module=demo,url='/v1/ Friends of Python Institute ')
    Print impact status and response data
    print(resp.status,resp.data)
    # assertion
    assert resp.status == HTTP_200
    assertresp.data ! =None

if __name__ == '__main__':
    testHpny()
Copy the code

Perform the test

Not only does Hug make Api development easy, but it also integrates testing of the Api, which really makes it possible for developers to develop high-quality apis quickly.

Reference:hugapi.github.io/hug/