updating importer api endointis (refs #63)

This commit is contained in:
Leo Germani 2018-06-18 15:40:01 -03:00
parent af092b1605
commit f2fe699369
2 changed files with 35 additions and 7 deletions

View File

@ -28,7 +28,7 @@ class REST_Importers_Controller extends REST_Controller {
* Register the collections route and their endpoints
*/
public function register_routes(){
register_rest_route($this->namespace, '/session/' . $this->rest_base, array(
register_rest_route($this->namespace, '/' . $this->rest_base . '/session', array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'),
@ -75,11 +75,11 @@ class REST_Importers_Controller extends REST_Controller {
));
register_rest_route($this->namespace, '/' . $this->rest_base . '/session/(?P<session_id>[0-9a-f]+)/source_metadata', array(
register_rest_route($this->namespace, '/' . $this->rest_base . '/session/(?P<session_id>[0-9a-f]+)/source_info', array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'source_metadata'),
'callback' => array($this, 'source_info'),
'permission_callback' => array($this, 'import_permissions_check'),
),
@ -175,6 +175,19 @@ class REST_Importers_Controller extends REST_Controller {
if($importer) {
foreach ($body as $att => $value){
if ($att == 'collection') {
if (is_array($value) && isset($value['id'])) {
$importer->add_collection($value);
continue;
} else {
return new \WP_REST_Response([
'error_message' => __('Invalid collection', 'tainacan' ),
'session_id' => $session_id
], 400);
}
}
$method = 'set_' . $att;
if (method_exists($importer, $method)) {
$importer->$method($value);
@ -199,7 +212,7 @@ class REST_Importers_Controller extends REST_Controller {
}
public function source_metadata() {
public function source_info() {
$session_id = $request['session_id'];
$importer = $_SESSION['tainacan_importer'][$session_id];
@ -212,12 +225,17 @@ class REST_Importers_Controller extends REST_Controller {
$response = [
'source_metadata' => false,
'source_total_items' => false
];
if ( method_exists($importer, 'get_source_metadata') ) {
$response['source_metadata'] = $importer->get_source_metadata();
}
if ( method_exists($importer, 'get_source_number_of_items') ) {
$response['source_total_items'] = $importer->get_source_number_of_items();
}
return new \WP_REST_Response( $response, 200 );
}

View File

@ -14,7 +14,7 @@ class TAINACAN_REST_Importers_Controller extends TAINACAN_UnitApiTestCase {
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/importers');
$request = new \WP_REST_Request('POST', $this->namespace . '/importers/session');
$request->set_body($params);
$response = $this->server->dispatch($request);
@ -38,10 +38,18 @@ class TAINACAN_REST_Importers_Controller extends TAINACAN_UnitApiTestCase {
$params = json_encode([
'url' => 'http://test.com',
'options' => ['delimiter' => ';']
'options' => ['delimiter' => ';'],
'collection' => [
'id' => 231,
'map' => [
30 => 'column1',
31 => 'column2'
],
'total_items' => 1234
]
]);
$request = new \WP_REST_Request('PATCH', $this->namespace . '/importers/' . $session_id);
$request = new \WP_REST_Request('PATCH', $this->namespace . '/importers/session/' . $session_id);
$request->set_body($params);
$response = $this->server->dispatch($request);
@ -56,6 +64,8 @@ class TAINACAN_REST_Importers_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals($__importer->get_url(), $data['url']);
$this->assertEquals(1234, $__importer->get_collections()[0]['total_items']);
}