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#
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#
root@XXXXXXXXXX:~/win10/mine/code-snippet/python/hug#
root@XXXXXXXXXX:~/win10/mine/code-snippet/python/hug# hug -f happy_birthday.py -c greet LightSong
Happy LightSong!