GithubHelp home page GithubHelp logo

dqsdhr / panda-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from arlicle/panda-api

0.0 0.0 0.0 21.66 MB

Panda api is a simple and powerful api docs design tool.

License: MIT License

Rust 98.99% HTML 0.98% CSS 0.03%

panda-api's Introduction

panda-api | 中文文档

大量使用说明,教程在中文文档中,请大家先看看中文文档和相关例子,忙于开发,等时间充足再写英文文档。

Panda api makes it easier to build better api docs more quickly and easy for front end and back end.

Panda api encourages test driven development. it takes care of much of the hassle of web development between front end and back end, when you write done your api docs, you can focus on writing front end without needing to develop the backend. It’s free and open source.

Why Panda Api:

  1. A better online read api docs.
  2. Use json5 to write the api docs,eazy to lean and write.
  3. Manage you api docs change as your code with git.
  4. You can use Panda api as a back end api service with out backend develop.
  5. Panda api takes test data helps developers auto test back end and front end
  6. Suport define test case data
  7. Mork data auto created
  8. Environment route support, you can change the back end on panda api to development, test, production
  9. Websocket support

Install

use installer (Recommended)

It looks like you’re running macOS, Linux, or another Unix-like OS. To download installer and install Panda api.

Install by Source code

Get the latest development version

git clone https://github.com/arlicle/panda-api.git

build and run panda api use cargo

cargo run

Once Panda Api is installed (see Install above) do this in a terminal:

panda --help

You should see the Panda Api command manual page printed to the terminal. This information includes command line options recognized by panda.

Getting started

Let's build a simple project to get our feet wet. We'll create a new directory, say my-project, and a file in it, auth.json5:

mkdir my-project
cd my-project
touch auth.json5

write a panda api doc

Edit the file auth.json5 with the following contents:

{
    name:"Auth",
    desc:"user login and logout",
    order:1,
    apis:
    [{
        name:"user login",
        desc:"if user login success, will get a token",
        method: "POST",
        url:"/login/",
        body_mode:"json", // form-data, text, json, html, xml, javascript, binary
        body:{
            username:{name: "username"},
            password:{name: "password"}
        },
        response:{
            code:{name:"response result code", type:"int", desc:"success is 1", enum:[-1, 1]},
            msg:{name:"response result message", type:"string"},
            token:{name:"login success, get a user token; login failed, no token", type:"string", required:false}
        },
        test_data:[
            {
                body:{username:"edison", password:"123"},
                response:{code:-1, msg:"password incorrect"}
            },
            {
                body:{username:"lily", password:"123"},
                response:{code:-1, msg:"username not exist"}
            },
            {
                body:{username:"root", password:"123"},
                response:{code:1, msg:"login success", token:"fjdlkfjlafjdlaj3jk2l4j"}
            }
        ]
    },
    {
        name:"user logout",
        method:"GET",
        url:"/logout/",
        query:{
            id:{name:"user id"},
            username:{}
        },
        response:{
            code:{name:"response result code", type:"int", desc:"success is 1", enum:[-1, 1]},
            msg:{name:"response result message", type:"string"}
        },
        test_data:[
            {
                query:{id:1, username:"root"},
                response:{code:1, msg:"logout success"}
            },
            {
                response:{code:-1, msg:"error"}
            },
            {
                query:{id:3, username:"lily"},
                response:{code:-1, msg:"username and id not match"}
            }
        ]
    }
]}

run command panda in the my-project

panda

You should see run info:

 INFO  actix_server::builder > Starting 8 workers
 INFO  actix_server::builder > Starting "actix-web-service-127.0.0.1:9000" service on 127.0.0.1:9000

view online api docs

Now we can view the api docs online http:://127.0.0.1:9000 or http://localhost:9000

Notice if you get a error

Error: Os { code: 48, kind: AddrInUse, message: "Address already in use" }

It's mean the port 9000 is in use, you need to change another one.

panda -p 9001

request the api

When the panda is running, we can request api in the docs without write a code of backend.

we request the api /login/ with test_data in the docs auth.json5.

1th:

curl localhost:9000/login/ -X POST -H "Content-Type:application/json" -d '{"username":"edison","password":"123"}'
// you will get response
{"code":-1,"msg":"password incorrect"}

2th:

curl localhost:9000/login/ -X POST -H "Content-Type:application/json" -d '{"username":"lily","password":"123"}'
// you will get response
{"code":-1,"msg":"username not exist"}

3th:

curl localhost:9000/login/ -X POST -H "Content-Type:application/json" -d '{"username":"root","password":"123"}'
// you will get response
{"code":1,"msg":"login success"}

If you request data not defined in the test_data, You will get a mock response

curl localhost:9000/login/ -X POST -H "Content-Type:application/json" -d '{"username":"hello","password":"123"}'
// you will get response like this
{"code":1,"msg":"SqM!3Mky@)q1O","token":"OkkdvtKKl(htx#KU6"}

Pretty simple, right?

mock options can help the mock data more like the production environment, update api /login/ response define:

...
response:{
    code:{name:"response result code", type:"int", desc:"success is 1", enum:[-1, 1]},
    msg:{name:"response result message", type:"sentence"}, // update type string to sentence
    token:{name:"login success, get a user token; login failed, no token", type:"string", required:false, length:64} // set the token length:64
},
...

request data not defined in the test_data again:

curl localhost:9000/login/ -X POST -H "Content-Type:application/json" -d '{"username":"hello","password":"123"}'
// you will get response like this
{"code":1,"msg":"Qphxw ddfcvpy odpi ikdd, ","token":"PRL3%S%Uc&33X%HB*Yflc3qQt(LnC)cf6^0w357F07r3xUyafsvS#mr8BZw6UrMo"}

more field options in here: https://www.debugmyself.com/p/2020/1/29/Panda-api%E5%AD%97%E6%AE%B5%E8%AF%B4%E6%98%8E/

array and object field

response:{
    total_page: {name:"total page", type:"number"},
    current_page: {name:"current page num", type:"number"},
    result:
        [{
            id:{name:"Article ID", type:"PosInt"},
            title:{name:"Article title"},
            category:{
                id:{name:"category id"},
                name:{name:"category name"}
            },
            author_name:{name:"Author name"},
            tags:[{
                id:{name:"Tag id", type:"PosInt"},
                name:{name:"tag name"}
            }],
            created:{name:"article created time", type:"timestamp"}
        }]
}

inherit model

mkdir _data
cd _data
touch models.json5
// _datat/models.json5
{
    Article:{
        id:{name:"Article ID", type:"PosInt"},
        title:{name:"Article Title"},
        category:{
            id:{name:"Category ID",},
            name:{name:"Category Name"}
        },
        author_name:{name:"Author name"},
        tags:[{
            id:{name:"Tag id", type:"PosInt"},
            name:{name:"Tag name"}
        }],
        created:{name:"article created time", type:"timestamp"}
    }
}
body: {
    $ref:"./_data/models.json5:Article",
    $exclude:["created", "category/name", "tags/0/name"],
    id:{name:"Article ID", type:"PosInt", required:false},
}
response: {
    $ref:"./_data/models.json5:Article",
    $exclude:["created", "category/name", "tags/0/name"],
    id:{name:"Article ID", type:"PosInt", required:false},
}

Examples

  1. Basics
  2. Inherit from models
  3. Global field settings

Panda Api 如何使用

panda-api's People

Contributors

arlicle avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.