15.
Swagger
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. When properly defined
via Swagger, a consumer can understand and interact with the remote service
with a minimal amount of implementation logic. Similar to what interfaces have
done for lower-level programming, Swagger removes the guesswork in calling
the service.
人とコンピュータに優しい
REST APIを記述する標準仕様
(を目指してます)
http://swagger.io/getting-started/
16.
Open API Initiative
RESTful APIの記述標準化を目指す「Open API Initiative」を
マイクロソフト、Google、IBMらが立ち上げ。
Swaggerをベースに - Publickey
http://www.publickey1.jp/blog/15/open_api_initiative.html
18.
REST API
Representational State Transfer
狭義のREST(RESTful)
RESTの定義を満たしたREST(実は余りない)
ステートレスなクライアント/サーバプロトコル
リソース指向
URIによるリソースの識別
HTTPメソッドを利用した操作
広義のREST(Web API)
XMLやJSONで応答(XML/JSON over HTTP)
RESTと称しているものの多くがこっち
19.
RESTのベストプラクティス
幾つもあるので、そのごく一部を紹介
動詞ではなく名詞を使う
悪い例
/getProducts
/listOrders
良い例
GET /products : すべての製品のリストを返す
POST /products : コレクションに製品を加える
GET /products/4 : 4番の製品を取り出す
PATCH/PUT /products/4 : 4番の製品を更新する
26.
Swagger Spec
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"description": "A sample API that uses a petstore as an example
to demonstrate features in the swagger-2.0 specification",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "Swagger API Team"
},
"license": {
"name": "MIT"
}
},
"host": "petstore.swagger.io",
"basePath": "/api",
"schemes": [
"http"
],
"consumes": [
JSONで記述
46.
インストール後の稼働テスト
$ ./aws-api-import.sh
$ ./aws-api-import.sh
Usage: aws-api-import [options] Path to API definition file to
import
Options:
--create, -c
Create a new API
Default: false
--deploy, -d
Stage used to deploy the API
--help
Default: false
--profile, -p
AWS CLI profile to use
Default: default
--raml-config
Provide a configuration file to load AWS information from
--test, -t
Delete the API after import (create only)
Default: false
--update, -u
47.
API Gatewayの作成
$ ls tst/resources/swagger/
apigateway.json petstore-with-external-docs.json
basic.json petstore.json
large.json test.json
petstore-expanded.json uber.json
petstore-minimal.json uber.yaml
petstore-simple.json
用意されているテンプレートを利用
実行
$ ./aws-api-import.sh -c tst/resources/swagger/petstore-simple.json
2015-11-21 07:42:29,295 INFO - Skip unsupported property name
region in profile [default].
2015-11-21 07:42:29,297 INFO - Skip unsupported property name
region in profile [dev].
2015-11-21 07:42:32,823 INFO - Attempting to create API from
Swagger definition. Swagger file: tst/resources/swagger/petstore-
simple.json
51.
参考情報
API GatewayのCLIが利用可能
o create-api-key
o create-base-path-mapping
o create-deployment
o create-domain-name
o create-model
o create-resource
o create-rest-api
o create-stage
o delete-api-key
o delete-base-path-mapping
o delete-client-certificate
o delete-deployment
o delete-domain-name
o delete-integration
o delete-integration-response
o delete-method
o delete-method-response
o delete-model
o delete-resource
o delete-rest-api
o delete-stage
o flush-stage-cache
o generate-client-certificate
o get-account
o get-api-key
o get-api-keys
o get-base-path-mapping
o get-base-path-mappings
o get-client-certificate
o get-client-certificates
o get-deployment
o get-deployments
o get-domain-name
o get-domain-names
o get-integration
o get-integration-response
o get-method
o get-method-response
o get-model
o get-model-template
o get-models
o get-resource
o get-resources
o get-rest-api
o get-rest-apis
o get-sdk
o get-stage
o get-stages
o help
o put-integration
o put-integration-response
o put-method
o put-method-response
o test-invoke-method
o update-account
o update-api-key
o update-base-path-mapping
o update-client-certificate
o update-deployment
o update-domain-name
o update-integration
o update-integration-response
o update-method
o update-method-response
o update-model
o update-resource
o update-rest-api
o update-stage
52.
API作成の流れ with Swagger
1. Swagger EditorでSpecを書く
2. AWS API Gateway Importerでインポート
3. SpecのHost名を書き換え
(DNSのCName設定の方がスマート?)
4. Swagger UIでテストしてみる
あまりスマートではないので、CLIと組み合わせ?
Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.
Be the first to comment