Kabuku Inc.
Software Engineer
@taxpon
taxpon
http://takuro.ws
- Python, TypeScript, Go, DevOps
- EuroPython 2017, 2016, PyConJP 2015, Blender Conf 2015
- Swagger Codegen technical committee (Python, TypeScript)
Takuro Wada
3
5
(https://www.openapis.org/about より一部改変引用)
‣
‣
‣
‣
‣
‣
‣
6
‣
‣
‣
‣
‣
‣
7
8
‣
‣
‣
‣
‣
‣
‣
‣
‣
‣
‣
9
10
11
‣
‣
‣
12
ちょっとややこしい…
13
OpenAPI
Specification
Swagger Core
Swagger Parser
Swagger Codegen
Swagger UI
Swagger Editor
3rd Party Tools
OpenAPI
Document
(YAML or JSON)
べんり!
‣
‣
‣
‣
15
‣
‣
‣
16
17
‣
‣
‣
‣
‣
‣
19
‣
‣
‣
20
‣
‣
21
COREツール
‣
‣
‣
22
COREツール
‣
23
OpenAPI
Doc
Generate
Swagger
Codegen
Input
Multiple languages
COREツール
‣
‣
‣
24
‣
‣
‣
‣
‣
‣
‣
25
‣
26
Book:
type: object
required: [id]
properties:
   id:
     type: integer
   title:
     type: string
   author:
     type: string
‣
27
import yaml
from bravado_core.spec import Spec
# 1
with open('openapi.yaml', 'r') as f:
raw_spec = yaml.load(f)
# 2
spec = Spec.from_dict(raw_spec)
# 3
book = raw_spec['definitions']['Book']
# 4
validate_schema_object(spec, book, target)
‣
28
validate_schema_object(spec, book, {})
Code
jsonschema.exceptions.ValidationError: 'id' is a required property
Failed validating 'required' in schema:
{'properties': {'author': {'type': 'string'},
'id': {'type': 'integer'},
'release_date': {'format': 'date', 'type': 'string'},
'title': {'type': 'string'}},
'required': ['id'],
'type': 'object',
'x-model': 'Book'}
On instance:
{}
Result
‣
29
validate_schema_object(spec, book, {"id": 1, "title": 1})
Code
jsonschema.exceptions.ValidationError: 1 is not of type 'string'
Failed validating 'type' in schema['properties']['title']:
{'type': 'string'}
On instance['title']:
1
Result
‣
‣
30
from bravado_core.unmarshal import unmarshal_schema_object
book_obj = unmarshal_schema_object(
  spec, book,
  {"id": 1,
"title": "Merchant of Venice",
“author": "William Shakespeare"})
print(book_obj)
Code
]変換対象のデータ
Book(author='William Shakespeare', id=1, title='Merchant of Venice')
Result
‣
31
Book:
type: object
required: [id]
properties:
   id:
     type: integer
   title:
     type: string
   author:
     type: string
   release_date:
     type: string
     format: date ]
‣
32
book_obj = unmarshal_schema_object(
  spec, book,
  {"id": 1,
"title": "Merchant of Venice",
"author": "William Shakespeare",
"release_date": "2017-07-11"})
print(book_obj)
Code
]変換対象のデータ
Book(author='William Shakespeare', id=1,
release_date=datetime.date(2017, 7, 11), title='Merchant of Venice')
Result
‣
‣
‣
‣
‣
33
‣
‣
34
Book = spec.definitions['Book']
book_obj = Book(id=1, title="Merchant of Venice",
s
release_date=date(2017, 7, 11))
book_dict = marshal_schema_object(spec, book, book_obj)
print(book_dict)
]“Book” object
Code
{'release_date': '2017-07-11', 'title': 'Merchant of Venice', 'id': 1,
'author': 'William Shakespeare'}
Result
‣
‣
‣
‣
‣
‣
35
‣
‣
‣
37
‣
‣
‣
38
Backend
ServerFrontend
API
Other
ServiceAPI
Kabuku Connect
Other
Service
OpenAPI OpenAPI
‣
‣
‣
39
Backend
ServerFrontend
API
Kabuku Connect
OpenAPI
Other
ServiceAPI
Other
Service
OpenAPI
Swagger codegen Swagger UI bravado-core
‣
40
BackendFrontend
API
Kabuku Connect
OpenAPI
Other
ServiceAPI
Other
Service
OpenAPI
Swagger codegen Swagger UI
‣
‣
‣
‣
‣
‣
‣
‣
41
‣
‣
‣
‣
‣
‣
42
‣
‣
‣
44
So Hot!!
‣
‣
‣
‣
‣
‣
45
‣
‣
‣
‣
46
‣
‣
‣
‣
‣
47
http://www.kabuku.co.jp/#jobs

OpenAPIを利用したPythonWebアプリケーション開発