1 / 30

はてなと REST API

はてなと REST API. 株式会社はてな 伊藤 直也 naoya@hatena.ne.jp. アジェンダ. はてなの取り組み はてなブックマークと AtomPP フレームワークと Web API AtomPP は銀の弾丸 ?. はてなの取り組み. はてな. 知的生産ツール群を提供する代官山にあるベンチャー企業. はてなウェブサービス. フィードによるデータ再利用を促進 API の公開 はてなキーワードリンク API はてなブックマーク AtomAPI はてなフォトライフ AtomAPI. なぜ API を公開するの ?.

koen
Download Presentation

はてなと REST API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. はてなとREST API 株式会社はてな 伊藤 直也 naoya@hatena.ne.jp

  2. アジェンダ • はてなの取り組み • はてなブックマークとAtomPP • フレームワークとWeb API • AtomPPは銀の弾丸?

  3. はてなの取り組み

  4. はてな • 知的生産ツール群を提供する代官山にあるベンチャー企業

  5. はてなウェブサービス • フィードによるデータ再利用を促進 • APIの公開 • はてなキーワードリンクAPI • はてなブックマークAtomAPI • はてなフォトライフAtomAPI

  6. なぜAPIを公開するの? It's developer friendly, user friendly, and is a web site as well as a "service" or "platform" (in the Web 2.0 sense) all at the same time. (Jeremy Zawodny http://jeremy.zawodny.com/blog/archives/002526.html) ブックマーク、ダイアリー etc. エンドユーザー サービス XMLウェブサービス API デベロッパー、 Geeks RSSフィード、キーワードリンクAPI、AtomAPI hatena.ne.jp

  7. なぜGeekにイケてるのが重要なの? テクノロジー・ライフサイクル (http://www.goodpic.com/mt/archives/000227.html) • はてなアンテナ、はてなダイアリー、はてなブックマーク • Blog / RSS • iPod / Google / Movable Type / Bloglines / Flickr / del.icio.us … 技術革新を伴うサービスはGeek & パワーユーザーが広める

  8. データ重要 • Web 2.0 (Tim O'Reilly) • Data is the Next Intel Inside • You control your own data • で、どうやって? • インポート / エクスポート • フィード • フィードで過去の物も遡れる • API で保存、編集、削除、取得

  9. はてなブックマークとAtomPP

  10. はてなブックマーク • ソーシャルブックマークサービス

  11. はてなブックマークAtomAPI • AtomPP 0.3 • REST • ブックマークの新規投稿 (POST) • 投稿したブックマークの取得 (GET) • ブックマークの編集 (PUT) • ブックマークの削除(DELETE) • WSSE認証

  12. 新規ブックマークの投稿 • PostURIへのXML文書(entry)のPOST POST /atom/post <?xml version="1.0" encoding="utf-8"?> <entry xmlns="http://purl.org/atom/ns#"> <title>Starwars Episode III</title> <link rel="related" href="http://www.starwars.com/" /> <summary type="text/plain">Darth Vader, Rise</summary> </entry>

  13. ブックマークの取得 • EditURI への GET or FeedURI へのGET # request GET /atom/edit/1234 # in response <?xml version="1.0" encoding="utf-8"> <entry xmlns="http://purl.org/atom/ns#"> <title>Lorem Ipsum</title> <link rel="related" href="http://www.lipsum.com/" /> <link rel="alternate" href="http://b.hatena.ne.jp/sample/20050407#1234" /> <link rel="service.edit" href="http://b.hatena.ne.jp/atom/edit/1234" /> <author> <name>naoya</name> </author> <issued>2005-04-07T:18:36:00+9:00</issued> <id>tag:hatena.ne.jp,2005:bookmark-naoya-1234</id> <summary>Lorem Ipsum is simply dummy text of the printing</summary> </entry>

  14. ブックマークの編集 • EditURI への PUT PUT /atom/edit/1234 <entry xmlns="http://purl.org/atom/ns#"> <title>Parrot</title> <summary>All the language belong to us.</summary> </entry>

  15. ブックマークの削除 • EditURI へのDELETE DELETE /atom/edit/1234 200 OK

  16. 一覧取得 • FeedURI + クエリパラメーター = Atomフィード • GET /atom/feed • GET /atom/feed?tag=hatena • GET /atom/feed?word=perl • GET /atom/feed?date=20050701 • GET /atom/feed?date=latest • GET /atom/feed?of=50 • それぞれの entry に EditURI • link要素でページング <link rel="prev" href="http://b.hatena.ne.jp/naoya/atomfeed?of=0" /> <link rel="next" href="http://b.hatena.ne.jp/naoya/atomfeed?of=40" />

  17. XML::Atom で AtomPP • XMLは一行も出てこない #/usr/local/bin/perl use strict; use XML::Atom::Entry; use XML::Atom::Client; my $PostURI = 'http://b.hatena.ne.jp/atom/post'; my $entry = XML::Atom::Entry->new; $entry->title('Alpha Neet'); $entry->content('Honestly, I think I'll be loser if I start to work.'); ... my $api = XML::Atom::Client->new; $api->username('naoya'); $api->password('foobarbaz); $api->createEntry($PostURI, $entry);

  18. なんでAtomPPにしたの? • モテそうだったから • REST とかかっこいい系? • 他のAPIとの相互交換性 • ユーザーがはてなのためだけに開発しなければいけない物、覚えなければいけないことが減る • ユーザーが既存のライブラリを使える • XML::Atom etc • はてなのアプリケーションとRESTの相性が良さそうだったから

  19. フレームワークとWeb API

  20. はてなと Cool URI • Cool URI重要 • モテそう • はてなと Cool URI • /naoya/ • /naoya/rss • /naoya/add?url=http://www.motetakute.net/ • /naoya/config • /keyword/perl • RESTを意識していたわけではないが、なんとなくいい感じのURIを意識してたらRESTと相性がよさそうな感じに。

  21. MVCフレームワークとURI • MVCフレームワークの Best Practice • M … O/Rマッパ • V …テンプレートエンジン • C …それぞれに特徴的なアーキテクチャ • Controller • Cool URI を意識するなら URI to Class / method の dispatch に規約を! • Convention over Configration

  22. 例: Ruby on Rails • /items • ItemsController.index • /items/list • ItemsController.list • /items/create • ItemsController.create

  23. 例: はてなフレームワーク • / • Hatena2::Bookmark::EngineIndex->do + Hatena2::Bookmark::PageMakerIndex->make • /atom • EngineAtom + PageMakerAtom • /atom/post • EngineAtomPost + PageMakerAtomPost • /add?mode=confirm • EngineAddConfirm + PageMakerAddConfirm

  24. URIに対応する規約があると • プログラマがURIに無知でも割と綺麗なURIのアプリケーションになる • 「Modelをリソースと見立て、その Controller を起動する URIを決める」という自然な考え方ができる。 • URIを見ただけで変更箇所が分かる • /atom/post は EngineAtomPost だね • 結果、REST に近づくため REST API との相性が良くなる

  25. フレームワークとWeb APIs • Catalyst • Catalyst::Plugin::XMLRPC • Catalyst::Plugin::AtomPP • Ruby on Rails • ActionWebService • Web APIs を容易に扱えるように、そのアーキテクチャを抽象化することが重要になってくる • AtomPP や XML-RPC など標準化(?)されてるものはフレームワークに搭載しやすい

  26. AtomPP は銀の弾丸?

  27. AtomPP は銀の弾丸ではない • REST / AtomPPの制約が邪魔なことも • 小さな API を作るとき • URIに対して被コメント数を返すとか。 • 大げさ、無理矢理、どうしたらいいかわからない • エレメントに対応するデータが直感的でない • summary がコメント? title がタグ?? • 結局仕様書が必要 • ほんとにこれでいいの? という疑問 • サーバーサイドの実装がややめんどくさい • やっぱりフレームワーク重要

  28. del.icio.us / api • XML over HTTP • Basic 認証 + 野良XML GET /api/posts/recent HTTP/1.0 <posts tag="" user="naomax"> <post href="http://...html" description="XML-Atom 0.12" tag="xml atom perl" time="2005-06-08T05:50:02Z"/> <post href="http://...html" description="Japan.internet.com" tag="podcasting" time="2005-06-08T05:49:07Z"/> <post href="http://...html" description="blah blah" tag="system:unfiled" time="2005-02-11T19:33:21Z"/> ... </posts> • シンプルで分かりやすい • データの項目が明快 • 小さなAPIも気にせず実装できる • Net::Delicious

  29. まとめ • Web APIs を公開するのは意味がある • REST的なアプリケーションならAtomPP • ただし、銀の弾丸ではないよ • URIとフレームワークの規約重要 • Web API を実装しやすくするフレームワークの進化に期待 • モテたくて 2.0

  30. ご清聴ありがとうございました。

More Related