{"id":1818,"date":"2015-03-17T19:23:04","date_gmt":"2015-03-17T19:23:04","guid":{"rendered":"http:\/\/www.doanduyhai.com\/blog\/?p=1818"},"modified":"2017-08-10T16:46:07","modified_gmt":"2017-08-10T16:46:07","slug":"killrchat-rest-api-design","status":"publish","type":"post","link":"https:\/\/www.doanduyhai.com\/blog\/?p=1818","title":{"rendered":"KillrChat REST API design"},"content":{"rendered":"<p>In this post, I&#8217;ll discuss about how <strong>KillrChat<\/strong> REST API has been designed to decouple the front and back ends.<\/p>\n<p> If you have missed the previous posts on <strong>KillrChat<\/strong>, please go <a href=\"https:\/\/www.doanduyhai.com\/blog\/?cat=55\" title=\"KillrChat blog posts\" target=\"_blank\">there<\/a><\/p>\n<p> The application is completely open-sourced, you can get it <a href=\"https:\/\/github.com\/doanduyhai\/killrchat\" title=\"KillrChat Github Repo\" target=\"_blank\">here<\/a><br \/>\n<!--more--><\/p>\n<h1>I User API<\/h1>\n<p>First we need to deal with the user management API. By user management, we mean:<\/p>\n<ol>\n<li>user account creation<\/li>\n<li>user login (security end-point)<\/li>\n<li>user logout (security end-point)<\/li>\n<li>get user details<\/li>\n<\/ol>\n<p>The corresponding REST end points are:<\/p>\n<table>\n<thead>\n<tr>\n<th>Description<\/th>\n<th>HTTP Method<\/th>\n<th>End Point<\/th>\n<th>Body\/Returned data<\/th>\n<th>HTTP code<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>User account creation<\/td>\n<td>POST<\/td>\n<td>\/users<\/td>\n<td>Body<\/p>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\r\n{\r\n    &quot;login&quot;: &quot;jdoe&quot;,\r\n    &quot;password&quot;: &quot;xxx&quot;,\r\n    &quot;firstname&quot;: &quot;John&quot;,\r\n    &quot;lastname&quot;: &quot;DOE&quot;,\r\n    &quot;email&quot;: &quot;john.doe@dallas.com&quot;,\r\n    &quot;bio&quot;: &quot;I am John DOE&quot;\r\n}\r\n<\/pre>\n<\/td>\n<td>HTTP 204 or error<\/td>\n<\/tr>\n<tr>\n<td>User login (requires <strong>HTTPS<\/strong>)<\/td>\n<td>POST<\/td>\n<td>\/authenticate?<em>j_password:xxx&#038;j_username:jdoe<\/em><\/td>\n<td>N\/A<\/td>\n<td>HTTP 204 or error<\/td>\n<\/tr>\n<tr>\n<td>User logout<\/td>\n<td>GET<\/td>\n<td>\/logout<\/td>\n<td>N\/A<\/td>\n<td>HTTP 200<\/td>\n<\/tr>\n<tr>\n<td>Get user details<\/td>\n<td>GET<\/td>\n<td>\/users\/<em>:login<\/em><\/td>\n<td>Returned data<\/p>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\r\n{\r\n    &quot;login&quot;:&quot;jdoe&quot;,\r\n    &quot;firstname&quot;:&quot;John&quot;,\r\n    &quot;lastname&quot;:&quot;DOE&quot;,\r\n    &quot;email&quot;:&quot;john.doe@dallas.com&quot;,\r\n    &quot;bio&quot;:&quot;I am John DOE&quot;,\r\n    &quot;chatRooms&quot;:[&quot;Twin_Peaks&quot;,&quot;Dallas&quot;]\r\n}\r\n<\/pre>\n<\/td>\n<td>\nHTTP 200 or error\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1>II Chat Room API<\/h1>\n<p> The chat room lifecycle involves:<\/p>\n<ol>\n<li>chat room creation<\/li>\n<li>get chat room details<\/li>\n<li>add participant to chat room<\/li>\n<li>remove participant from chat room<\/li>\n<li>list n random chat rooms<\/li>\n<li>remove chat room and kick out all its participants<\/li>\n<\/ol>\n<p>The corresponding REST end points are:<\/p>\n<table>\n<thead>\n<tr>\n<th>Description<\/th>\n<th>HTTP Method<\/th>\n<th>End Point<\/th>\n<th>Body\/Returned data<\/th>\n<th>HTTP code<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Chat room creation<\/td>\n<td>POST<\/td>\n<td>\/rooms\/<em>:roomName<\/em><\/td>\n<td>Body<\/p>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\r\n{\r\n    creator: {\r\n                 login: &quot;jdoe&quot;, \r\n                 firstname: &quot;John&quot;, \r\n                 lastname: &quot;DOE&quot;\r\n             }, \r\n    banner: &quot;Chat room for Dallas serie&quot;\r\n}\r\n<\/pre>\n<\/td>\n<td>HTTP 204 or error<\/td>\n<\/tr>\n<tr>\n<td>Get chat room details<\/td>\n<td>GET<\/td>\n<td>\/rooms\/<em>:roomName<\/em><\/td>\n<td>Returned data<\/p>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\r\n{\r\n    &quot;roomName&quot;:&quot;Dallas&quot;,\r\n    &quot;creator&quot;:{\r\n                 &quot;login&quot;:&quot;jdoe&quot;,\r\n                 &quot;firstname&quot;:&quot;John&quot;,\r\n                 &quot;lastname&quot;:&quot;DOE&quot;\r\n              },\r\n    &quot;banner&quot;:&quot;Anything related to this serie&quot;,\r\n    &quot;participants&quot;:[\r\n                    {\r\n                        &quot;login&quot;:&quot;jdoe&quot;,\r\n                        &quot;firstname&quot;:&quot;John&quot;,\r\n                        &quot;lastname&quot;:&quot;DOE&quot;\r\n                    },\r\n                    ...\r\n                   ],\r\n    &quot;creationDate&quot;:&quot;2015-03-14 20:31:48&quot;\r\n}\r\n<\/pre>\n<\/td>\n<td>HTTP 200 or error<\/td>\n<\/tr>\n<tr>\n<td>Add participant to chat room<\/td>\n<td>PUT<\/td>\n<td>\/rooms\/participant<\/td>\n<td>Body<\/p>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\r\n{\r\n\r\n    &quot;login&quot;:&quot;jdoe&quot;,\r\n    &quot;firstname&quot;:&quot;John&quot;,\r\n    &quot;lastname&quot;:&quot;DOE&quot;\r\n}\r\n<\/pre>\n<\/td>\n<td>HTTP 204 or error<\/td>\n<\/tr>\n<tr>\n<td>Remove participant from chat room<\/td>\n<td>PATCH<\/td>\n<td>\/rooms\/participant<\/td>\n<td>Body<\/p>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\r\n{\r\n\r\n    &quot;login&quot;:&quot;jdoe&quot;,\r\n    &quot;firstname&quot;:&quot;John&quot;,\r\n    &quot;lastname&quot;:&quot;DOE&quot;\r\n}\r\n<\/pre>\n<\/td>\n<td>HTTP 204 or error<\/td>\n<\/tr>\n<tr>\n<td>List 100 random chat rooms<\/td>\n<td>GET<\/td>\n<td>\/rooms?<em>fetchSize=100<\/em><\/td>\n<td>Returned data<\/p>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\r\n[\r\n    {\r\n        &quot;roomName&quot;:&quot;Dallas&quot;,\r\n        &quot;creator&quot;:{\r\n            &quot;login&quot;:&quot;jdoe&quot;,\r\n            &quot;firstname&quot;:&quot;John&quot;,\r\n            &quot;lastname&quot;:&quot;DOE&quot;\r\n        },\r\n        &quot;banner&quot;:&quot;Anything related to this serie&quot;,\r\n        &quot;participants&quot;:[\r\n            {\r\n                &quot;login&quot;:&quot;jdoe&quot;,\r\n                &quot;firstname&quot;:&quot;John&quot;,\r\n                &quot;lastname&quot;:&quot;DOE&quot;\r\n            },\r\n            ...\r\n        ],\r\n        &quot;creationDate&quot;:&quot;2015-03-14 20:21:39&quot;\r\n    },\r\n    ...\r\n]\r\n<\/pre>\n<\/td>\n<td>HTTP 200 or error<\/td>\n<\/tr>\n<tr>\n<td>Remove a chat room<\/td>\n<td>PATCH<\/td>\n<td>\/rooms\/<em>:roomName<\/em><\/td>\n<td>Body<\/p>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\r\n{\r\n    participants: [&quot;jdoe&quot;, &quot;hsue&quot;, ...]\r\n}\r\n<\/pre>\n<\/td>\n<td>HTTP 204 or error<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote><p>We use the HTTP verb <strong>PATCH<\/strong> instead of <strong>DELETE<\/strong> to be able to pass a body. Indeed, the HTTP specification is quite vague about supporting a body content for <strong>DELETE<\/strong> messages. Some REST producers\/consumers do support it, others don&#8217;t. To circumvent this limitation, <strong>PATCH<\/strong> is a possible work-around, although not ideal because we loose the HTTP verb semantics.<\/p><\/blockquote>\n<h1>III Chat Messages API<\/h1>\n<p> Chat messages lifecycle is quite simple:<\/p>\n<ol>\n<li>create a new chat message<\/li>\n<li>fetch a batch of messages starting from a message id<\/li>\n<\/ol>\n<p>The corresponding REST end points are:<\/p>\n<table>\n<thead>\n<tr>\n<th>Description<\/th>\n<th>HTTP Method<\/th>\n<th>End Point<\/th>\n<th>Body\/Returned data<\/th>\n<th>HTTP code<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Create a new chat message<\/td>\n<td>POST<\/td>\n<td>\/messages<\/td>\n<td>Body<\/p>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\r\n{\r\n    author: {\r\n        login: &quot;jdoe&quot;, \r\n        firstname: &quot;John&quot;, \r\n        lastname: &quot;DOE&quot;\r\n    }, \r\n    content: &quot;I'm fine, thanks!&quot;\r\n}\r\n<\/pre>\n<\/td>\n<td>HTTP 204 or error<\/td>\n<\/tr>\n<tr>\n<td>Fetch previous messages starting from<\/td>\n<td>GET<\/td>\n<td>\/messages<em>?fetchSize=20<br \/>\n&#038;fromMessageId=52278d00-cb0b-&#8230;<\/em><\/td>\n<td>Returned data<\/p>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\r\n[\r\n    {\r\n        &quot;messageId&quot;:&quot;4e831610-cb0b-...&quot;,\r\n        &quot;author&quot;:{\r\n            &quot;login&quot;:&quot;jdoe&quot;,\r\n            &quot;firstname&quot;:&quot;John&quot;,\r\n            &quot;lastname&quot;:&quot;DOE&quot;\r\n        },\r\n        &quot;content&quot;:&quot;1&quot;,\r\n        &quot;systemMessage&quot;:false,\r\n        &quot;creationDate&quot;:&quot;2015-03-15 13:03:35&quot;\r\n    },\r\n    ...\r\n]\r\n<\/pre>\n<\/td>\n<td>HTTP 200 or error<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>To be continued &#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, I&#8217;ll discuss about how KillrChat REST API has been designed to decouple the front and back ends. If you have missed the previous posts on KillrChat, please go there The application is completely open-sourced, you can get&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/www.doanduyhai.com\/blog\/?p=1818\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[57,55,10],"tags":[],"_links":{"self":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1818"}],"collection":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1818"}],"version-history":[{"count":35,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1818\/revisions"}],"predecessor-version":[{"id":1855,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1818\/revisions\/1855"}],"wp:attachment":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}