All Projects → liushoukun → Dawn Api Demo

liushoukun / Dawn Api Demo

Licence: other
dawn-api-demo

Projects that are alternatives of or similar to Dawn Api Demo

dawn-api
A RESTful API package
Stars: ✭ 25 (-78.63%)
Mutual labels:  wiki, document, restful-api, thinkphp5
Smartwiki
因个人精力有限,不在维护此项目,推荐用MinDoc代替
Stars: ✭ 486 (+315.38%)
Mutual labels:  api, wiki, document
Apidoc
RESTful API 文档生成工具,支持 Go、Java、Swift、JavaScript、Rust、PHP、Python、Typescript、Kotlin 和 Ruby 等大部分语言。
Stars: ✭ 785 (+570.94%)
Mutual labels:  api, restful-api, document
Gulp Apidoc
📄 RESTful web API Documentation Generator
Stars: ✭ 60 (-48.72%)
Mutual labels:  api, document
Api Strategy
Equinor API Strategy
Stars: ✭ 56 (-52.14%)
Mutual labels:  api, restful-api
Standards.rest
A collection of standards, specifications, etc. for HTTP API development.
Stars: ✭ 58 (-50.43%)
Mutual labels:  api, restful-api
Thinkphp5 Restfulapi
restful-api风格接口 APP接口 APP接口权限 oauth2.0 接口版本管理 接口鉴权
Stars: ✭ 949 (+711.11%)
Mutual labels:  restful-api, thinkphp5
Parvula
An extremely simple & flexible CMS generated from flat files with a complete RESTful API —
Stars: ✭ 76 (-35.04%)
Mutual labels:  api, restful-api
Spacex Api
🚀 Open Source REST API for SpaceX launch, rocket, core, capsule, starlink, launchpad, and landing pad data.
Stars: ✭ 8,973 (+7569.23%)
Mutual labels:  api, restful-api
Aztro
The Astrology API 💫 Get daily horoscope!
Stars: ✭ 78 (-33.33%)
Mutual labels:  api, restful-api
Ppgo api demo gin
API接口应用Demo 基于Gin
Stars: ✭ 90 (-23.08%)
Mutual labels:  api, restful-api
Rest Api Example
RESTful Blog API in Lumen 5.2
Stars: ✭ 53 (-54.7%)
Mutual labels:  api, restful-api
Ebook Building An Api Backend With Microprofile
Building an API Backend with MicroProfile
Stars: ✭ 53 (-54.7%)
Mutual labels:  api, restful-api
Openapi Generator
OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
Stars: ✭ 10,634 (+8988.89%)
Mutual labels:  api, restful-api
Generator Http Fake Backend
Yeoman generator for building a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 49 (-58.12%)
Mutual labels:  api, restful-api
Jokeapi
A REST API that serves uniformly and well formatted jokes in JSON, XML, YAML or plain text format that also offers a great variety of filtering methods
Stars: ✭ 71 (-39.32%)
Mutual labels:  api, restful-api
Taco Api
🍉 Brazilian Table of Food Composition (TACO) - JSON API
Stars: ✭ 87 (-25.64%)
Mutual labels:  api, restful-api
Api Restful Con Laravel Guia Definitiva
Repositorio para el código base del curso "API RESTful con Laravel - Guía Definitiva"
Stars: ✭ 95 (-18.8%)
Mutual labels:  api, restful-api
Restful Api Guidelines
A model set of guidelines for RESTful APIs and Events, created by Zalando
Stars: ✭ 1,397 (+1094.02%)
Mutual labels:  api, restful-api
E Commerce 2 django
Guest register, user register, user login, user logout, account home page, product view history, change password, reset password, change name, send activation email when register, resend activation email, add shipping address, add billing address, add nickname to the addresses, edit shipping address, edit billing address, view list of your addresses, reuse shipping addresses when order products, reuse billing addresses when ordeer products, show sales analytics if staff or admin only using -chart.js-, get analytics data with Ajax, receive marketing email, change if user will receive marketing email or not by admin, send contact message with Ajax, products list, product detail, download product detail as a PDF file, download digital product files -if the user purchased that digital product only-, orders list, list of digital products files, order detail, download order detail as a PDF file, verify order ownership with Ajax -to secure order detail page-, show cart products, add or remove product from cart, checkout page, thanks page when order placed successfully, add or reuse payment method, add or reuse payment method with Ajax, search products by title, search products by description, search products by price, search products by tag title, write tags for products -by admin only-, auto fill contact email, full name if user logged in.
Stars: ✭ 20 (-82.91%)
Mutual labels:  api, restful-api

Dawn-Api

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads [Toc]

说明

thinkphp5编写的restful风格的API,集API请求处理,权限认证,自动生成文档等功能;

  • restful风格处理请求

每个接口对于一个控制器,method对应[method]方法响应

  • 权限认证

Basic,Oauth Client Credentials Grant

  • 文档生成

简洁,优雅,不需要额外的文档工具;

安装

  • 如果想在你的TP5项目中使用,那么可以直接使用
composer require liushoukun/dawn-api
  • 如果是新项目先要创建tp5项目,然后再require
composer create-project topthink/think api  --prefer-dist
composer require liushoukun/dawn-api
  • 如果要使用生成文档 需要在public/static/ 下 安装hadmin
cd /public/static/
git clone  https://git.oschina.net/liushoukun/hadmin.git

使用

  1. 新建demo 模块
  2. 创建业务基础类 Base 继承 ApiController
<?php
   namespace app\demo\controller;


   use DawnApi\facade\ApiController;
   class Base extends ApiController
   {

    //是否开启授权认证
    public    $apiAuth = true;

   }
?>

  1. 创建一个用户接口 User 继承 Base;
class User extends Base
  1. 添加资源路由

/v1/user

Route::group('v1',function (){
    Route::resource('user','demo/User');
});

标识 请求类型 生成路由规则 对应操作方法(默认)
index GET v1/user index
save POST v1/user save
read GET v1/user/:id read
update PUT v1/user/:id update
delete DELETE v1/user/:id delete
create GET v1/user/create create
edit GET v1/user/:id/edit edit

附加方法

如需要在添加一些额外的操作 如:发送验证码 在该类$extraActionList 属性添加方法 protected $extraActionList = ['sendCode']; //附加方法 之后注册路由

Route::group('v1',function (){
    Route::any('user/sendCode','demo/User/sendCode');//需要在资源路由之前注册
    Route::resource('user','demo/User');
});

跳过鉴权 如有有需要单操作跳过鉴权 在该类$skipAuthActionList 属性添加方法,即可跳过鉴权 protected $skipAuthActionList = ['sendCode']; //跳过鉴权的方法

资源路由文档->

返回处理

   // 成功
  return $this->sendSuccess($data = [], $message = 'success', $code = 200, $headers = [], $options = [])
   // 错误
  return $this->sendError($error = 400, $message = 'error', $code = 400, $data = [], $headers = [], $options = [])
    // 重定向
  return $this-> sendRedirect($url, $params = [], $code = 302, $with = [])
  1. 请求接口

开启授权认证

1.添加配置 认证总开关

 'api_auth' => true,  //是否开启授权认证

2.权限类 实现AuthContract 接口

  • authenticate 接口认证返回true则通过,否则不通过

  • getUser 获取用户信息,Api控制可以调用

self::$app['auth']->getUser();
 /**
     * 认证授权通过客户端信息和路由等
     * 如果通过返回true
     * @param Request $request
     * @return bool
     */
    public function authenticate(Request $request)
    {
        // TODO: Implement authenticate() method.
        return true;
    }

    /**
     * 获取用户信息 接口里可以直接获取用户信息
     * @return mixed
     */
    public function getUser()
    {
       return ['app_id'=>'111','name'=>'dawn-api'];
    }

  1. 接口类开启授权认证
//是否开启授权认证
public    $apiAuth = true;
配置(api_auth) 类($apiAuth) 效用
true true 认证开启
true false 认证关闭
false false 认证关闭
false true 认证关闭
  1. 配置验证类

简单实现了Basic & Oauth Client Credentials Grant认证

  • Basic
   'auth_class' => \app\demo\auth\BasicAuth::class, //授权认证类

/v1/user?client_id=test&secret=test OR v1/user headers Basic

  • Oauth Client Credentials Grant
  1. 配置验证类
   'auth_class' => \app\demo\auth\OauthAuth::class, //授权认证类
  1. 获取access_token
  • 编写可访问的方法获取access_token
namespace app\demo\controller;

use app\demo\auth\OauthAuth;
use think\Request;

class Auth
{
    public function accessToken()
    {
        $request = Request::instance();
        $OauthAuth = new OauthAuth();
        return $OauthAuth->accessToken($request);
    }

}
  • 配置路由
Route::any('accessToken','demo/auth/accessToken');//Oauth

按需改写获取客户端信息

  • 请求获取 /accessToken?client_id=20882088&secret=nGk5R2wrnZqQ02bed29rjzax1QWRIu1O 或者 /accessToken headers Basic MjA4ODIwODg6bkdrNVIyd3JuWnFRMDJiZWQyOXJqemF4MVFXUkl1MU8=

accessToken

3.请求接口 /v1/user?access_token=cxXpnNIdf4aSIQFjR8NYH91Uwsg8jzMZ

accessToken

快捷方式利用postman accessToken accessToken accessToken

自动生成文档

  1. 创建Doc文档显示控制器 wiki 继承 DawnApi\facade\Doc;

  2. 配置路由

// 文档
\DawnApi\route\DawnRoute::wiki();

可以改写该方法以存储数据获取,为了方便采用的是配置方式

tp5 增加额外配置 创建application/extra/api_doc.php 文件

return [
    '1' => ['name' => '测试文档', 'id' => '1', 'parent' => '0', 'class'=>'','readme' =>''],//下面有子列表为一级目录
    '2' => ['name' => '获取权限', 'id' => '2', 'parent' => '1', 'class'=>'','readme' => '/doc/md/auth.md'],//没有接口的文档,加载markdown文档
    '3' => ['name' => '用户接口', 'id' => '3', 'parent' => '1', 'readme' => '','class'=>\app\test\controller\User::class],//User接口文档
];

wiki

参数 必须 备注 作用
name true 接口列表名称 显示列表名称
id true 主键 生成列表所用
parent true 生成列表所用
class true 接口位置 用于生成具体接口文档
readme true markdown 可以生成没有接口的文档,比如一些说明 module和controller为空,readme填文档路径

3.具体接口文档配置

  • 接口描述部分(类文件的注释)
/**
 * Class User
 * @title 用户接口
 * @url /v1/user
 * @desc  有关于用户的接口
 * @version 1.0
 * @readme /doc/md/user.md
 */
class User extends Base{}
参数 必须 备注 作用
title true 接口标题 显示列表名称
url true 请求地址 用户显示
desc true 接口描述 显示描述
version false 版本号 版本号
readme false markdown文档 可以编写信息文档

ClassDoc

  • 具体接口文档
  1. 接口描述信息(注释填写)
       /**
        * @title 获取用户信息
        * @desc 获取用户信息
        * @readme /doc/md/method.md
        */
      public function getResponse(\think\Request $request){}

参数 必须 备注 作用
title true 接口标题 显示列表名称
desc true 接口描述 显示描述
readme false markdown文档 可以编写信息文档

2.请求参数

    /**
     * 参数规则
     * @name 字段名称
     * @type 类型
     * @require 是否必须
     * @default 默认值
     * @desc 说明
     * @range 范围
     * @return array
     */
    public static function getRules()
    {
        $rules = [
            'index' => [
            ],
            'create' => [
                'name' => ['name' => 'name', 'type' => 'string', 'require' => 'true', 'default' => '', 'desc' => '名称', 'range' => '',],
                'age' => ['name' => 'age', 'type' => 'string', 'require' => 'true', 'default' => '', 'desc' => '年龄', 'range' => '',],
            ],
            'sendCode' => [
                'name' => ['name' => 'name', 'type' => 'string', 'require' => 'true', 'default' => '', 'desc' => '名称', 'range' => '',],
                'age' => ['name' => 'age', 'type' => 'string', 'require' => 'true', 'default' => '', 'desc' => '年龄', 'range' => '',],
            ]

        ];
        //可以合并公共参数
        return $rules;

    }

data

  1. 返回参数(注释填写)
     * @return int id ID
     * @return string username 错误信息
     * @return int age 年龄
参数 必须 备注
第一个参数 true 类型
第二个参数 true 参数名
第三个参数 true 参数说明

类型填写规则

'string'    => '字符串',
'int'       => '整型',
'float'     => '浮点型',
'boolean'   => '布尔型',
'date'      => '日期',
'array'     => '数组',
'fixed'     => '固定值',
'enum'      => '枚举类型',
'object'    => '对象',

return

整体效果 all all all

开发文档参考

开发工具推荐

  • IDE PHPSTORM
  • 模拟请求 Postman
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].