天天看點

hug -- Embrace the APIs of the future

Background

大部分的架構,要麼隻支援 restful API, 要麼隻支援CLI。

例如:

fastapi 或者 django_rest_framework  支援rest API

click 支援将函數裝飾為 CLI

特殊情況,需要兩種都要支援。即一份代碼, 同時支援兩種接口。

hug就是為此而生。

hug

http://www.hug.rest/

https://github.com/hugapi/hug

号稱未來的API, 代碼一次書寫,不管何種用戶端使用, 都可以支援。支援 本地調用, HTTP調用 , CLI調用。

Embrace the APIs of the future

Drastically simplify API development over multiple interfaces. With hug, design and develop your API once, then expose it however your clients need to consume it. Be it locally, over HTTP, or through the command line - hug is the fastest and most modern way to create APIs on Python3.

設計目标:

根據代碼自動産生文檔

運作快 -- 基于falcon restapi架構

測試非常友好

提使用者包裝好各種接口類型

面向未來

hug's Design Objectives:
  • Make developing a Python driven API as succinct as a written definition.
  • The framework should encourage code that self-documents.
  • It should be fast. A developer should never feel the need to look somewhere else for performance reasons.
  • Writing tests for APIs written on-top of hug should be easy and intuitive.
  • Magic done once, in an API framework, is better than pushing the problem set to the user of the API framework.
  • Be the basis for next generation Python APIs, embracing the latest technology.

TRY

https://github.com/fanqingsong/code_snippet/tree/master/python/hug

"""A basic (single function) API written using Hug"""
import hug


@hug.get("/happy_birthday", examples="name=HUG&age=1")
@hug.cli()
@hug.local()
def happy_birthday(name, age: hug.types.number):
    """Says happy birthday to a user"""
    return "Happy {age} Birthday {name}!".format(**locals())


@hug.get("/greet/{event}")
@hug.cli()
@hug.local()
def greet(event: str):
    """Greets appropriately (from http://blog.ketchum.com/how-to-write-10-common-holiday-greetings/)  """
    greetings = "Happy"
    if event == "Christmas":
        greetings = "Merry"
    if event == "Kwanzaa":
        greetings = "Joyous"
    if event == "wishes":
        greetings = "Warm"

    return "{greetings} {event}!".format(**locals())      

LOCAL

https://github.com/fanqingsong/code_snippet/blob/master/python/hug/happy_birthday_call.py

from happy_birthday import greet

data = greet("LightSong")

print(data)      

root@XXXXX:~/win10/mine/code-snippet/python/hug# hug -f happy_birthday.py -c greet LightSong

Happy LightSong!

HTTP

root@XXXXXX:~/win10/mine/code-snippet/python/hug# hug -f happy_birthday.py

/#######################################################################\

          `.----``..-------..``.----.

         :/:::::--:---------:--::::://.

        .+::::----##/-/oo+:-##----:::://

        `//::-------/oosoo-------::://.       ##    ##  ##    ##    #####

          .-:------./++o/o-.------::-`   ```  ##    ##  ##    ##  ##

             `----.-./+o+:..----.     `.:///. ########  ##    ## ##

   ```        `----.-::::::------  `.-:::://. ##    ##  ##    ## ##   ####

  ://::--.``` -:``...-----...` `:--::::::-.`  ##    ##  ##   ##   ##    ##

  :/:::::::::-:-     `````      .:::::-.`     ##    ##    ####     ######

   ``.--:::::::.                .:::.`

         ``..::.                .::         EMBRACE THE APIs OF THE FUTURE

             ::-                .:-

             -::`               ::-                   VERSION 2.6.1

             `::-              -::`

              -::-`           -::-

\########################################################################/

 Copyright (C) 2016 Timothy Edmund Crosley

 Under the MIT License

Serving on :8000...

127.0.0.1 - - [03/Aug/2021 23:22:46] "GET / HTTP/1.1" 404 1628

127.0.0.1 - - [03/Aug/2021 23:22:47] "GET /favicon.ico HTTP/1.1" 404 1628

127.0.0.1 - - [03/Aug/2021 23:23:41] "GET /happy_birthday?name=HUG&age=1 HTTP/1.1" 200 23

127.0.0.1 - - [03/Aug/2021 23:24:09] "GET /greet/LightSong HTTP/1.1" 200 18

CLI

root@XXXXXXXXXX:~/win10/mine/code-snippet/python/hug# hug -f happy_birthday.py -h

usage: hug [-h] [-v] [-f FILE] [-m MODULE] [-ho HOST] [-p PORT] [-n] [-ma]

           [-i INTERVAL] [-c COMMAND] [-s]

Hug API Development Server

optional arguments:

  -h, --help            show this help message and exit

  -v, --version         show program's version number and exit

  -f FILE, --file FILE  file

  -m MODULE, --module MODULE

                        module

  -ho HOST, --host HOST

                        host

  -p PORT, --port PORT  A whole number

  -n, --no_404_documentation

                        Providing any value will set this to true

  -ma, --manual_reload  Providing any value will set this to true

  -i INTERVAL, --interval INTERVAL

                        A whole number

  -c COMMAND, --command COMMAND

                        command

  -s, --silent          Providing any value will set this to true

root@XXXXXXXXXX:~/win10/mine/code-snippet/python/hug#

root@XXXXXXXXXX:~/win10/mine/code-snippet/python/hug# hug -f happy_birthday.py -c help

A basic (single function) API written using Hug

Available Commands:

 - happy_birthday: Says happy birthday to a user

 - greet: Greets appropriately (from http://blog.ketchum.com/how-to-write-10-...

root@XXXXXXXXXX:~/win10/mine/code-snippet/python/hug# hug -f happy_birthday.py -c greet LightSong

出處:http://www.cnblogs.com/lightsong/

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接。