Upgrade to Pro — share decks privately, control downloads, hide ads and more …

swaggerでかっこいい APIドキュメントを作ろう

swaggerでかっこいい APIドキュメントを作ろう

2016/5/21 PHPカンファレンス福岡2016 LT

Kaz Watanabe

May 22, 2016
Tweet

More Decks by Kaz Watanabe

Other Decks in Programming

Transcript

  1. SwaggerͰ͔͍͍ͬ͜APIυΩϡϝϯτΛ࡞Ζ͏ @kaz_29 What is swagger? The goal of Swagger™ is

    to define a standard, language- agnostic interface to REST APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. ਓʹ΋ίϯϐϡʔλͷ྆ํʹ༏͘͠ɺݴޠʹґଘ͠ͳ͍ ඪ४తͳREST APIͷΠϯλʔϑΣʔεΛఆٛ͢Δ͜ͱɻ(໨ඪ) http://swagger.io/getting-started/
  2. SwaggerͰ͔͍͍ͬ͜APIυΩϡϝϯτΛ࡞Ζ͏ @kaz_29 Community-Driven Language Integrations • Clojure • D •

    Go • Haskell • Java • Javascript • .NET • node.js • perl • php • python • ruby • scala
  3. SwaggerͰ͔͍͍ͬ͜APIυΩϡϝϯτΛ࡞Ζ͏ @kaz_29 PHP Language Integrations • cakephp-swagger • Swagger-PHP •

    SwaggerAssertions • php-swaggerize-fastroute-library • SwaggerGen • Jane OpenAPI • gossi/swagger
  4. SwaggerͰ͔͍͍ͬ͜APIυΩϡϝϯτΛ࡞Ζ͏ @kaz_29 PHP Language Integrations • cakephp-swagger • Swagger-PHP •

    SwaggerAssertions • php-swaggerize-fastroute-library • SwaggerGen • Jane OpenAPI • gossi/swagger
  5. SwaggerͰ͔͍͍ͬ͜APIυΩϡϝϯτΛ࡞Ζ͏ @kaz_29 <?php /** * @SWG\Swagger( * basePath="/api", * host="phpcon.decr.jp",

    * schemes={"http"}, * produces={"application/json"}, * consumes={"application/json"}, * @SWG\Info( * version="1.0.0", * title="Swaggerαϯϓϧ PHPΧϯϑΝϨϯε෱Ԭ2016 LT൛", * description="Swaggerαϯϓϧ PHPΧϯϑΝϨϯε෱Ԭ2016 LT൛ ", * @SWG\Contact(name="[email protected]"), * @SWG\License(name="ϥΠηϯεදه") * ), * ) */ APIͷఆٛΛॻ͘ app/Model/Entity/User.php
  6. SwaggerͰ͔͍͍ͬ͜APIυΩϡϝϯτΛ࡞Ζ͏ @kaz_29 <?php namespace App\Model\Entity; use Cake\ORM\Entity; /** * User

    Entity. * * @SWG\Definition( * definition=" User", * required={ * "id", "username", "password", "created", "modified" * }, * @SWG\Property( * property="id", * type="integer", * description="user id" * ), * @SWG\Property( * property="email", * type="string", * description="ϝʔϧΞυϨε", * maxLength=255, * ), … ModelͷఆٛΛॻ͘
  7. SwaggerͰ͔͍͍ͬ͜APIυΩϡϝϯτΛ࡞Ζ͏ @kaz_29 <?php namespace App\Controller\Api; use App\Controller\AppController; /** * Users

    Controller * * @property \App\Model\Table\UsersTable $Users */ class UsersController extends AppController { … /** * Index method * * @SWG\Get( * path="/users", * description="ϢʔβҰཡ", * produces={"application/json"}, * @SWG\Parameter( * description="ݕࡧจࣈྻ", * in="formData", * name="q", * required=false, * type="string" * ), * @SWG\Response( * response=200, * description="औಘ੒ޭ", APIͷఆٛΛॻ͘
  8. SwaggerͰ͔͍͍ͬ͜APIυΩϡϝϯτΛ࡞Ζ͏ @kaz_29 … * @SWG\Response( * response=200, * description="औಘ੒ޭ", *

    @SWG\Schema( * @SWG\Property( * property="status", * type="boolean", * description="ॲཧ݁Ռ", * ), * @SWG\Property( * property="data", * type="array", * @SWG\Items(ref="#/definitions/IndexUser"), * description="Ϣʔβ৘ใ", * ), * @SWG\Property( * property="pagination", * ref="#/definitions/PageInfo", * description="ϖʔδ৘ใ", * ), * ) * ), * @SWG\Response( * response="404", * description="Not Found" * ), * ) APIͷఆٛΛॻ͘
  9. SwaggerͰ͔͍͍ͬ͜APIυΩϡϝϯτΛ࡞Ζ͏ @kaz_29 … * @SWG\Definition( * definition="IndexUser", * allOf={ *

    @SWG\Schema(ref="#/definitions/User"), * }, * @SWG\Property( * property="bookmarks", * description="ϒοΫϚʔΫҰཡ", * type="array", * @SWG\Items(ref="#/definitions/Bookmark") * ), * ) */ APIͷఆٛΛॻ͘
  10. SwaggerͰ͔͍͍ͬ͜APIυΩϡϝϯτΛ࡞Ζ͏ @kaz_29 Swagger-PHPͰॲཧ $ ./app/vendor/bin/swagger ./app/src/ ./app/config/ -o ./api-documents/ Swagger-PHP

    2.0.6 ----------------- post /bookmarks get /users post /users get /users/{id} put /users/{id} delete /users/{id} post /php/conferences ----------------------- 7 operations documented ----------------------- Written to /srv/application/current/api-documents/swagger.json