zoukankan      html  css  js  c++  java
  • 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!

    出处:http://www.cnblogs.com/lightsong/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    java设计模式-适配器模式
    java设计模式-外观模式
    java设计模式-享元模式
    java设计模式-装饰模式
    java设计模式-组合模式
    java设计模式-桥接模式
    12月Java原生商城APP源码-完全开源
    uniapp插件市场-涂图视频编辑-美妆-剪辑-微整形原生sdk插件发布-优雅草科技
    12月最新仿知音漫画网站源码+手机端,小说漫画生成静态文件,超强负载量安全可靠
    如何把网易云音乐ncm格式转换成mp3格式---记一下
  • 原文地址:https://www.cnblogs.com/lightsong/p/15096985.html
Copyright © 2011-2022 走看看