From d46d3d29aa08262f2d5bb4990bcf87efb97c99d6 Mon Sep 17 00:00:00 2001 From: Mario Rodeghiero Date: Mon, 14 Sep 2020 15:24:58 -0300 Subject: [PATCH] [add][s] refactor action method and fix tests - #10 --- lib/index.js | 18 +++++--- lib/util/action-api.js | 24 ----------- test/metadata.test.js | 95 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 103 insertions(+), 34 deletions(-) delete mode 100644 lib/util/action-api.js diff --git a/lib/index.js b/lib/index.js index b07a0f8..56edf69 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,7 +3,6 @@ const axios = require('axios') const CkanAuthApi = require('./util/ckan-auth-api') const CkanUploadAPI = require('./util/ckan-upload-api') -const ActionApi = require('./util/action-api') const Open = require('./file') /** @@ -42,17 +41,24 @@ class Client { * https://docs.ckan.org/en/latest/api/index.html#action-api-reference * @param {string} actionName - The action name, e.g. site_read, package_show ... * @param {object} payload - The payload being sent to CKAN + * @param {string} requestType - The requestType is the request method, default is POST */ - async action(actionName, payload) { + async action(actionName, payload, requestType = "POST") { const path = `/api/3/action/${actionName}` - const config = { + const headers = { headers: { 'Content-Type': 'application/json;charset=utf-8', Authorization: this.authToken, }, } - const response = await axios.post(`${this.api}${path}`, payload, config) + const response = await axios({ + method: requestType, + url:`${this.api}${path}`, + headers: headers, + data: payload + }) + if (!response.data.success) { throw `Action response: ${response.statusText}` } @@ -60,8 +66,8 @@ class Client { return response.data } - put(actionType, data) { - return ActionApi.action(this.api, this.authToken, actionType, data) + put(actionType, payload) { + return this.action(actionType, payload) } /** diff --git a/lib/util/action-api.js b/lib/util/action-api.js deleted file mode 100644 index 26dd8bb..0000000 --- a/lib/util/action-api.js +++ /dev/null @@ -1,24 +0,0 @@ -const axios = require('axios') - -const ActionApi = { - // Call the ckan api actions ie. 'package_create' - action: async (baseUrl, authToken, actionType, data) => { - const path = `/api/3/action/${actionType}` - const config = { - headers: { - 'Content-Type': 'application/json;charset=utf-8', - Authorization: authToken, - }, - } - const body = JSON.stringify(data) - - const response = await axios.post(`${baseUrl}${path}`, data, config) - if (!response.data.success) { - throw `Action response: ${response.statusText}` - } - - return response.data - }, -} - -module.exports = ActionApi diff --git a/test/metadata.test.js b/test/metadata.test.js index 15f78a7..0ce8ba5 100644 --- a/test/metadata.test.js +++ b/test/metadata.test.js @@ -1,6 +1,6 @@ const test = require('ava') const nock = require('nock') -const f11s = require('data.js') +const f11s = require('data.js'); const { Client } = require('../lib/index') /** @@ -184,6 +184,83 @@ const resourceMock = nock(config.api) } }) +const getAction = nock(config.api) + .persist() + .get('/api/3/action/package_show', { + "name_or_id": "3a1aa4af-9f00-490b-956e-2b063ed04961" + }) + .reply(200, { + "help": "http://localhost:5000/api/3/action/help_show?name=package_show", + "success": true, + "result": { + "license_title": null, + "maintainer": null, + "relationships_as_object": [], + "private": false, + "maintainer_email": null, + "num_tags": 0, + "id": "3a1aa4af-9f00-490b-956e-2b063ed04961", + "metadata_created": "2020-09-10T23:15:09.975155", + "metadata_modified": "2020-09-14T11:52:03.155843", + "author": null, + "author_email": null, + "state": "active", + "version": null, + "creator_user_id": "ceb0d8c2-352b-4a6c-b2c8-2a1a4190192b", + "type": "dataset", + "resources": [ + { + "mimetype": null, + "cache_url": null, + "hash": "", + "description": "", + "name": "my_dataset_name", + "format": "", + "url": "", + "datastore_active": false, + "cache_last_updated": null, + "package_id": "3a1aa4af-9f00-490b-956e-2b063ed04961", + "created": "2020-09-11T02:40:54.757756", + "state": "active", + "mimetype_inner": null, + "last_modified": null, + "position": 0, + "revision_id": "a50f4241-7c92-48cd-a51a-6e83ec9be694", + "url_type": null, + "id": "bb4b2f10-d640-4305-a43d-4b369600ff82", + "resource_type": null, + "size": null + }, + ], + "num_resources": 20, + "tags": [], + "groups": [], + "license_id": null, + "relationships_as_subject": [], + "organization": { + "description": "my org", + "created": "2020-04-14T13:27:47.434967", + "title": "myorg", + "name": "myorg", + "is_organization": true, + "state": "active", + "image_url": "", + "revision_id": "bff67202-a21e-470b-9b67-14e65ad85fa2", + "type": "organization", + "id": "f3fce98a-4750-4d09-a5d1-d5e1b995a2e8", + "approval_status": "approved" + }, + "name": "my_dataset_name", + "isopen": false, + "url": "http://civicdatatest.cloudapp.net/storage/f/testdata/test.csv", + "notes": "A long description of my dataset", + "owner_org": "f3fce98a-4750-4d09-a5d1-d5e1b995a2e8", + "extras": [], + "title": "my_dataset_name", + "revision_id": "959e0ee9-b902-4568-bd71-ff0d8c77bb18" + } +}) + test('create a dataset', async (t) => { const client = new Client( config.authToken, @@ -192,7 +269,7 @@ test('create a dataset', async (t) => { config.api ) - const response = await client.put('package_create', { "name": "my_dataset", "owner_org": "myorg"}) + const response = await client.action('package_create', { "name": "my_dataset", "owner_org": "myorg"}) t.is(client.api, config.api) t.is(packageCreateMock.isDone(), true) @@ -209,7 +286,7 @@ test('put metadata', async (t) => { resources: [], }) - const response = await client.put('package_create', dataset.descriptor) + const response = await client.action('package_create', dataset.descriptor) t.is(packageCreateMock.isDone(), true) t.is(response.success, true) @@ -224,7 +301,7 @@ test('put_resource create or update a resource', async (t) => { // Dataset must exist file.descriptor.package_id = "my_dataset_name" - const response = await client.put('resource_create', file.descriptor) + const response = await client.action('resource_create', file.descriptor) t.is(resourceMock.isDone(), true) t.is(response.success, true) @@ -232,3 +309,13 @@ test('put_resource create or update a resource', async (t) => { t.is(response.result.format, "CSV") t.is(response.result.mediatype, "text/csv") }) + +test('action method GET', async (t) => { + + const response = await client.action('package_show', { + "name_or_id": "3a1aa4af-9f00-490b-956e-2b063ed04961" + }, "GET") + + t.is(resourceMock.isDone(), true) + t.is(response.success, true) +})