5. 5/16
RESTfull API に関するアドオン選定 /addons
●
それでこの Pyramid の RESTfull API に関するアド
オンがあるだろう調べたところ Cornice( 読み : コー
ニス ) があったんですね。これは Pyramid 上の
REST framework となっています。
●
I wanna use Cornice that is A REST
framework for Pyramid.
6. 6/16
Cornice と Colander について /About
Cornice and Colander.
●
それでこの Cornice 単体でバリデーションの仕組みも
ありますが、 Colander というバリデーション / デシ
リアライゼーションの Pyramid アドオンを利用するこ
ともできると。
●
The Cornice can use Colander.
7. 7/16
それで /And
●
さらに、この Colander と Cornice を組み合わせる
と Colander のやり方で API のスキーマ定義を書けて
Sphinx といったドキュメント生成パッケージで API
定義も書き出せて便利そうだと。
●
When I use Cornice and Colander, I can define
API schema and use Sphinx.
9. 9/16
組み方
●
Cornice と Colander を組み合わせて組む場合は次の
ようにコントローラ処理と API スキーマ定義、そして
バリデーション定義を書きます。
●
When I use Cornice and Colander, I have to
code Control, API schema definition and
validation definition.
10. 10/16
コード例 /code sample
import cornice, colander
hello = cornice.Service(name='hello', path='/', description='desc')
class HelloSchema(colander.MappingSchema):
foo = colander.SchemaNode(colander.String(), location="body", type='str')
def validate_foo_is_bar(request):
if request.validated['foo'] != 'bar':
request.errors.add('body', 'foo', 'foo is has to be bar')
@hello.post(schema=HelloSchema, validators=[validate_foo_is_bar])
def hello_post(request):
return {'Hello':request.validated['foo']}
11. 11/16
複雑ではないかと /I feel messy.
●
しかし、これらを一つの views のファイルの中に書く
とごちゃごちゃしすぎるし、本来やりたいコントローラ
に類する処理をシンプルに書けないなと。困ったなと。
●
API のスキーマ定義、コントローラ処理、バリデー
ションの処理 3 つの処理をどうモジュール分けしよう
かと。
●
That’s very messy. What can I do?
14. 14/16
結果として /Conclusion
●
悩んだ末、僕は API のスキーマ定義とコントローラの処理は同じ
views モジュールに記載し、バリデーションの処理は別モジュール
とすることにしました。
●
Then decouple two modules, that the API schema and
Controller module and the validation module.
●
モジュール分割の例 /sample modules
– views.py
●
API スキーマ定義とコントローラ処理を書く。
– validations.py