Skip to content

Commit

Permalink
First pass at communicating with API
Browse files Browse the repository at this point in the history
  • Loading branch information
obenland committed Jul 24, 2019
1 parent dd1a6c3 commit 9ae3312
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public function handle_batch_requests( $request ) {

// Foreach request specified in the requests param, run the endpoint.
foreach ( $request['requests'] as $request_params ) {
$response = $this->handle_request( $request_params );
$key = $request_params['method'] . ' ' . $request_params['route'];
$data[ $key ] = $this->prepare_for_collection( $response );
$response = $this->handle_request( $request_params );
$key = $request_params['method'] . ' ' . $request_params['route'];
$data[ $key ][] = $this->prepare_for_collection( $response );
}

return rest_ensure_response( $data );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function register_routes() {
* @return WP_Error|WP_REST_Response Response object on success, WP_Error object on failure.
*/
public function create_item( $request ) {
if ( in_array( get_post_type( $request->get_param( 'post_id' ) ), [ 'revision', 'attachment' ], true ) ) {
if ( ! empty( $request['post_id'] ) && in_array( get_post_type( $request['post_id'] ), [ 'revision', 'attachment' ], true ) ) {
return new WP_Error( 'rest_invalid_param', __( 'Invalid parent type.' ), [ 'status' => 400 ] );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
*/
import { reduce, isEmpty, forEach, set } from 'lodash';

/**
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';

/**
* A full asset URL.
* @typedef {String} URL
Expand Down Expand Up @@ -133,28 +139,33 @@ const findAssetsInBlock = ( session, block ) => {
* @returns {Promise} Promise that resoves into an object with URLs as keys and fetch results as values.
*/
const fetchAssets = async assets => {
return new Promise( resolve => {
// Simulate API call delay.
setTimeout( () => {
// TODO: Get this from API response.
const mockImage = {
id: 66,
url:
'http://test.local/wp-content/uploads/2019/07/Screenshot-2019-07-16-at-17.30.52-1021x1024.png',
};

resolve(
reduce(
assets,
( fetched, asset ) => ( {
...fetched,
[ asset.url ]: mockImage,
} ),
{}
)
);
}, 1000 );
const requests = [];

forEach( assets, ( { url } ) => {
requests.push( {
method: 'POST',
route: '/fse/v1/sideload/image',
params: { url },
} );
} );

return await apiFetch( {
method: 'POST',
path: addQueryArgs( '/fse/v1/batch', { requests } ),
} ).then( response =>
reduce(
assets,
( fetched, asset ) => {
const { id, source_url } = response[ 'POST /fse/v1/sideload/image' ].shift();

return {
...fetched,
[ asset.url ]: { id, url: source_url },
};
},
{}
)
);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion apps/full-site-editing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"build": "rimraf dist && npm-run-all --parallel build:*"
},
"dependencies": {
"@wordpress/api-fetch": "^3.1.2",
"@wordpress/api-fetch": "^3.3.0",
"@wordpress/blocks": "^6.2.3",
"@wordpress/components": "^7.3.0",
"@wordpress/compose": "^3.2.0",
Expand Down

0 comments on commit 9ae3312

Please sign in to comment.