API結(jié)構(gòu)

2018-05-26 11:53 更新

為了提供一個(gè)統(tǒng)一API的實(shí)現(xiàn)方式和使用方式,將會(huì)做出一系列實(shí)現(xiàn)和使用的規(guī)范

身份驗(yàn)證

API 使用基于令牌的身份驗(yàn)證機(jī)制。某些終端不需要身份驗(yàn)證。您可以通過(guò) /api/token 取得一個(gè)令牌,主要有兩種令牌:

  • 密碼授權(quán)令牌
  • 私人訪問(wèn)令牌

一般情況,后臺(tái)管理程序推薦使用密碼授權(quán)令牌。

基本結(jié)構(gòu)

  • Notadd 中實(shí)現(xiàn) API 路由的方式,傾向于傳統(tǒng) Laravel 的實(shí)現(xiàn)方式,基于 Controller 調(diào)用 API Handler 的方式來(lái)實(shí)現(xiàn)。
  • Handler 中使用 toResponse 方法返回 ApiResponse 的實(shí)例。
  • Handler 主要實(shí)現(xiàn) DataHandler、SetHandler 兩種類型的 Handler。
  • ApiResponse 為 \Psr\Http\Message\ResponseInterface 契約的一個(gè)實(shí)現(xiàn)。
  • ApiResponse 實(shí)例所提供的并返回至前端調(diào)用的數(shù)據(jù)主要包含:code、data、message。

所支持的相關(guān)API操作

  • /oauth/access 驗(yàn)證是否擁有 API 訪問(wèn) Token
  • /oauth/access/authorize
  • /oauth/access/token
  • /oauth/authorize
  • /oauth/clients
  • /oauth/refresh

API Handler 示例

namespace Notadd\Foundation\Setting\Controllers;


use Notadd\Foundation\Routing\Abstracts\Controller;
use Notadd\Foundation\Setting\Contracts\SettingsRepository;
use Notadd\Foundation\Setting\Handlers\AllHandler;
use Notadd\Foundation\Setting\Handlers\SetHandler;


/**
 * Class ApiController.
 */
class SettingController extends Controller
{
    /**
     * @var \Notadd\Foundation\Setting\Contracts\SettingsRepository
     */
    protected $settings;


    /**
     * SettingController constructor.
     *
     * @param \Notadd\Foundation\Setting\Contracts\SettingsRepository $settings
     *
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function __construct(SettingsRepository $settings)
    {
        parent::__construct();
        $this->settings = $settings;
    }


    /**
     * TODO: Method  Description
     *
     * @param \Notadd\Foundation\Setting\Handlers\AllHandler $handler
     *
     * @return \Notadd\Foundation\Passport\Responses\ApiResponse
     * @throws \Exception
     */
    public function all(AllHandler $handler)
    {
        $response = $handler->toResponse();


        return $response->generateHttpResponse();
    }


    /**
     * @param \Notadd\Foundation\Setting\Handlers\SetHandler $handler
     *
     * @return \Notadd\Foundation\Passport\Responses\ApiResponse
     * @throws \Exception
     */
    public function set(SetHandler $handler)
    {
        $response = $handler->toResponse();


        return $response->generateHttpResponse();
    }
}

namespace Notadd\Foundation\Setting\Handlers;


use Illuminate\Container\Container;
use Illuminate\Http\Request;
use Illuminate\Translation\Translator;
use Notadd\Foundation\Passport\Abstracts\DataHandler;
use Notadd\Foundation\Setting\Contracts\SettingsRepository;


/**
 * Class SettingHandler.
 */
class AllHandler extends DataHandler
{
    /**
     * @var \Notadd\Foundation\Setting\Contracts\SettingsRepository
     */
    protected $settings;


    /**
     * AllHandler constructor.
     *
     * @param \Illuminate\Container\Container                         $container
     * @param \Illuminate\Http\Request                                $request
     * @param \Notadd\Foundation\Setting\Contracts\SettingsRepository $settings
     * @param \Illuminate\Translation\Translator                      $translator
     */
    public function __construct(
        Container $container,
        Request $request,
        SettingsRepository $settings,
        Translator $translator
    ) {
        parent::__construct($container, $request, $translator);
        $this->settings = $settings;
    }


    /**
     * Http code.
     *
     * @return int
     */
    public function code()
    {
        return 200;
    }


    /**
     * Data for handler.
     *
     * @return array
     */
    public function data()
    {
        return $this->settings->all()->toArray();
    }


    /**
     * Errors for handler.
     *
     * @return array
     */
    public function errors()
    {
        return [
            '獲取全局設(shè)置失??!',
        ];
    }


    /**
     * Messages for handler.
     *
     * @return array
     */
    public function messages()
    {
        return [
            '獲取全局設(shè)置成功!',
        ];
    }
}

namespace Notadd\Foundation\Setting\Handlers;


use Illuminate\Container\Container;
use Illuminate\Http\Request;
use Illuminate\Translation\Translator;
use Notadd\Foundation\Passport\Abstracts\SetHandler as AbstractSetHandler;
use Notadd\Foundation\Setting\Contracts\SettingsRepository;


/**
 * Class SetHandler.
 */
class SetHandler extends AbstractSetHandler
{
    /**
     * @var \Notadd\Foundation\Setting\Contracts\SettingsRepository
     */
    protected $settings;


    /**
     * SetHandler constructor.
     *
     * @param \Illuminate\Container\Container $container
     * @param \Illuminate\Http\Request $request
     * @param \Notadd\Foundation\Setting\Contracts\SettingsRepository $settings
     * @param \Illuminate\Translation\Translator $translator
     */
    public function __construct(
        Container $container,
        Request $request,
        SettingsRepository $settings,
        Translator $translator
    ) {
        parent::__construct($container, $request, $translator);
        $this->settings = $settings;
    }


    /**
     * Data for handler.
     *
     * @return array
     */
    public function data()
    {
        return $this->settings->all()->toArray();
    }


    /**
     * Errors for handler.
     *
     * @return array
     */
    public function errors()
    {
        return [
            '修改設(shè)置失敗!',
        ];
    }


    /**
     * Execute Handler.
     *
     * @return bool
     */
    public function execute()
    {
        $this->settings->set('site.enabled', $this->request->input('enabled'));
        $this->settings->set('site.name', $this->request->input('name'));
        $this->settings->set('site.domain', $this->request->input('domain'));
        $this->settings->set('site.beian', $this->request->input('beian'));
        $this->settings->set('site.company', $this->request->input('company'));
        $this->settings->set('site.copyright', $this->request->input('copyright'));
        $this->settings->set('site.statistics', $this->request->input('statistics'));


        return true;
    }


    /**
     * Messages for handler.
     *
     * @return array
     */
    public function messages()
    {
        return [
            '修改設(shè)置成功!',
        ];
    }
}
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)