{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/wp-calypso/issues/10270","repository_url":"https://api.github.com/repos/Automattic/wp-calypso","labels_url":"https://api.github.com/repos/Automattic/wp-calypso/issues/10270/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/wp-calypso/issues/10270/comments","events_url":"https://api.github.com/repos/Automattic/wp-calypso/issues/10270/events","html_url":"https://github.com/Automattic/wp-calypso/pull/10270","id":197574489,"number":10270,"title":"Framework: Provide a simple API to use the batch endpoint for all GET requests","user":{"login":"youknowriad","id":272444,"avatar_url":"https://avatars.githubusercontent.com/u/272444?v=3","gravatar_id":"","url":"https://api.github.com/users/youknowriad","html_url":"https://github.com/youknowriad","followers_url":"https://api.github.com/users/youknowriad/followers","following_url":"https://api.github.com/users/youknowriad/following{/other_user}","gists_url":"https://api.github.com/users/youknowriad/gists{/gist_id}","starred_url":"https://api.github.com/users/youknowriad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/youknowriad/subscriptions","organizations_url":"https://api.github.com/users/youknowriad/orgs","repos_url":"https://api.github.com/users/youknowriad/repos","events_url":"https://api.github.com/users/youknowriad/events{/privacy}","received_events_url":"https://api.github.com/users/youknowriad/received_events","type":"User","site_admin":false},"labels":[{"id":285009988,"url":"https://api.github.com/repos/Automattic/wp-calypso/labels/Framework","name":"Framework","color":"006b75","default":false},{"id":285010018,"url":"https://api.github.com/repos/Automattic/wp-calypso/labels/[Status]%20Needs%20Review","name":"[Status] Needs Review","color":"fbca04","default":false},{"id":285009976,"url":"https://api.github.com/repos/Automattic/wp-calypso/labels/[Type]%20Enhancement","name":"[Type] Enhancement","color":"c7def8","default":false}],"state":"open","locked":false,"assignee":{"login":"youknowriad","id":272444,"avatar_url":"https://avatars.githubusercontent.com/u/272444?v=3","gravatar_id":"","url":"https://api.github.com/users/youknowriad","html_url":"https://github.com/youknowriad","followers_url":"https://api.github.com/users/youknowriad/followers","following_url":"https://api.github.com/users/youknowriad/following{/other_user}","gists_url":"https://api.github.com/users/youknowriad/gists{/gist_id}","starred_url":"https://api.github.com/users/youknowriad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/youknowriad/subscriptions","organizations_url":"https://api.github.com/users/youknowriad/orgs","repos_url":"https://api.github.com/users/youknowriad/repos","events_url":"https://api.github.com/users/youknowriad/events{/privacy}","received_events_url":"https://api.github.com/users/youknowriad/received_events","type":"User","site_admin":false},"assignees":[{"login":"youknowriad","id":272444,"avatar_url":"https://avatars.githubusercontent.com/u/272444?v=3","gravatar_id":"","url":"https://api.github.com/users/youknowriad","html_url":"https://github.com/youknowriad","followers_url":"https://api.github.com/users/youknowriad/followers","following_url":"https://api.github.com/users/youknowriad/following{/other_user}","gists_url":"https://api.github.com/users/youknowriad/gists{/gist_id}","starred_url":"https://api.github.com/users/youknowriad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/youknowriad/subscriptions","organizations_url":"https://api.github.com/users/youknowriad/orgs","repos_url":"https://api.github.com/users/youknowriad/repos","events_url":"https://api.github.com/users/youknowriad/events{/privacy}","received_events_url":"https://api.github.com/users/youknowriad/received_events","type":"User","site_admin":false}],"milestone":null,"comments":4,"created_at":"2016-12-26T10:49:30Z","updated_at":"2016-12-27T12:26:13Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/Automattic/wp-calypso/pulls/10270","html_url":"https://github.com/Automattic/wp-calypso/pull/10270","diff_url":"https://github.com/Automattic/wp-calypso/pull/10270.diff","patch_url":"https://github.com/Automattic/wp-calypso/pull/10270.patch"},"body":"In this PR, I add a batch queue to the data layer. This queue batches multiple GET requests into one API call.\r\n\r\n**Usage**\r\n\r\nReplace your `wpcom`API call by a `batch.request`call, for example:\r\n\r\n```js \r\nimport wpcom from 'lib/wp';\r\nwpcom.site( siteId ).statsPostViews( postId, { fields: stat } ).then( data => console.log( data) );\r\n```\r\n\r\nbecomes\r\n\r\n```js \r\nimport { request } from 'state/data-layer/wpcom/batch';\r\nconst path = `/sites/${ siteId }/stats/post/${ postId }?fields=${ stat }`;\r\nrequest( path ).then( data => console.log( data ) );\r\n```\r\n\r\n**Questions**\r\n\r\n- On the `batch` API,  when an URL returns an error, the status_code is included in the response, but when it's successful, it's not. It might be better if it behaves consistently and always include an \"envelope\" to the response.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/Automattic/wp-calypso/issues/comments/269319516","html_url":"https://github.com/Automattic/wp-calypso/pull/10270#issuecomment-269319516","issue_url":"https://api.github.com/repos/Automattic/wp-calypso/issues/10270","id":269319516,"user":{"login":"dmsnell","id":5431237,"avatar_url":"https://avatars.githubusercontent.com/u/5431237?v=3","gravatar_id":"","url":"https://api.github.com/users/dmsnell","html_url":"https://github.com/dmsnell","followers_url":"https://api.github.com/users/dmsnell/followers","following_url":"https://api.github.com/users/dmsnell/following{/other_user}","gists_url":"https://api.github.com/users/dmsnell/gists{/gist_id}","starred_url":"https://api.github.com/users/dmsnell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dmsnell/subscriptions","organizations_url":"https://api.github.com/users/dmsnell/orgs","repos_url":"https://api.github.com/users/dmsnell/repos","events_url":"https://api.github.com/users/dmsnell/events{/privacy}","received_events_url":"https://api.github.com/users/dmsnell/received_events","type":"User","site_admin":false},"created_at":"2016-12-27T12:26:13Z","updated_at":"2016-12-27T12:26:13Z","body":"For reference, I have started working on a new generic batch endpoint in D3798.\r\n\r\n> The lib/wp already includes the raw batch endpoint, this is what prevents me from moving it there for now\r\n\r\nRight :wink:\r\n\r\nThis isn't a good place for a general utility function. The data layer should specifically be watching for actions which may imply data needs, figure out what is needed to fetch or update that data, perform the request, and then respond afterwards by dispatching new actions inserting the data into Calypso.\r\n\r\nThe changes in this PR might be better as improvements to the batch function in `wpcom.js`.\r\n\r\nThe way I think the batch can be effectively used however is to hit the abstraction one level deeper, something I am working on preparing and documenting. Batching isn't going to work too well at the API layer because we want to be able to automatically combine, for example, a site request and a site settings request, and a site icon request if they all three come together. To do effective batching here we will need to describe those requests in terms of the language they are using to communicate to the backend: HTTP."}}
{"actor":{"display_login":"dmsnell"}}