Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method to call CKAN action API #10

Closed
6 tasks done
rufuspollock opened this issue Sep 14, 2020 · 3 comments
Closed
6 tasks done

Method to call CKAN action API #10

rufuspollock opened this issue Sep 14, 2020 · 3 comments
Assignees

Comments

@rufuspollock
Copy link
Member

rufuspollock commented Sep 14, 2020

We can copy this from old okfn/ckan.js

Acceptance

  • async client.action(actionName, payload, useHttpGet=false) calls CKAN action API and returns the result
  • (?) Support for forcing http get

[ ] the method is documented in code and README
Created a separate issue for this #22

Tasks

  • Tests (copy from okfn/ckan.js)
    • Node

* [ ] Browser (?)
I don't think we need this for now, because the implementation of action() is platform agnostic, i.e. there is no any platform related implementation for now.

  • Implement the method
  • Support for postType=GET (like ckan.js has)
@mariorodeghiero
Copy link
Collaborator

mariorodeghiero commented Sep 14, 2020

Use in Datapub:

await client.put('resource_create', resource.descriptor)

The SDK implemented:

index.js

const ActionApi = require('./util/action-api')

class Client {
  constructor(authToken, organizationId, datasetId, api) {
    this.authToken = authToken
    this.organizationId = organizationId
    this.datasetId = datasetId
    this.api = api
  }

  put(actionType, data) {
    return ActionApi.action(this.api, this.authToken, actionType, data)
  }
 .
 .
 .
}

module.exports = {
  Client,
}

Action function:

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
  },
}

@rufuspollock
Copy link
Member Author

rufuspollock commented Sep 14, 2020

The action method should be part of Client class

class Client {

  // Call the ckan api actions ie. 'package_create'
  action: async(actionType, payload, forceHttpGet=false) => {
    const path = `/api/3/action/${actionType}`
    const config = {
      headers: {
        'Content-Type': 'application/json;charset=utf-8',
        Authorization: this.authToken,
      }
    }

    const body = JSON.stringify(data)
    
    // TODO: if forceHttpGet then ...
    
    const response = await axios.post(`${baseUrl}${path}`, data, config)
    if (!response.data.success) {
      throw `Action response: ${response.statusText}`
    }
    return response.data
  },
}

@kmanaseryan
Copy link
Contributor

Covered in #14

DataPub Overview automation moved this from In Progress to Done Sep 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

3 participants