Skip to content

Commit

Permalink
feat: implement generic http methods
Browse files Browse the repository at this point in the history
Signed-off-by: Hunter Achieng <[email protected]>
  • Loading branch information
hunterachieng committed Jan 31, 2025
1 parent c0fe0cf commit 49b5fa4
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
138 changes: 138 additions & 0 deletions packages/kobotoolbox/src/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { expandReferences } from '@openfn/language-common/util';
import * as util from './Utils';

/**
* Options object
* @typedef {Object} RequestOptions
* @property {object} query - An object of query parameters to be encoded into the URL
* @property {object} headers - An object of all request headers
* @property {string} [parseAs='json'] - The response format to parse (e.g., 'json', 'text', or 'stream')
*/

/**
* Make a GET request to any Kobotoolbox endpoint.
* @public
* @function
* @example <caption>GET forms resource</caption>
* http.get(
* "/assets",
* {
* query: {
* format: json
* }
* }
* )
* @param {string} path - path to resource
* @param {RequestOptions} [options={}] - An object containing query params and headers for the request
* @returns {operation}
*/
export function get(path, options = {}) {
return async state => {
const [resolvedPath, resolvedOptions] = expandReferences(
state,
path,
options
);

const response = await util.request(
state,
'GET',
resolvedPath,
resolvedOptions
);

return util.prepareNextState(state, response);
};
}

/**
* Make a POST request to a Kobotoolbox endpoint
* @public
* @function
* @example <caption>Create an asset resource</caption>
* http.post(
* '/assets/',
* {
* name: 'Feedback Survey Test',
* asset_type: 'survey',
* },
* {
* query: {
* format: 'json',
* },
* }
* );
* @param {string} path - path to resource
* @param {any} data - the body data in JSON format
* @param {RequestOptions} [options={}] - An object containing query params and headers for the request
* @returns {operation}
*/
export function post(path, data, options = {}) {
return async state => {
const [resolvedPath, resolvedData, resolvedOptions] = expandReferences(
state,
path,
data,
options
);

const optionsObject = {
data: resolvedData,
...resolvedOptions,
};
const response = await util.request(
state,
'POST',
resolvedPath,
optionsObject
);

return util.prepareNextState(state, response);
};
}

/**
* Make a PUT request to a Kobotoolbox endpoint
* @public
* @function
* @example <caption>Update an asset resource</caption>
* http.put(
* 'assets/a4jAWzoa8SZWzZGhx84sB5/deployment/',
* {
* name: 'Feedback Survey Test',
* asset_type: 'survey',
* },
* {
* query: {
* format: 'json',
* },
* }
* );
* @param {string} path - path to resource
* @param {any} data - the body data in JSON format
* @param {RequestOptions} [options={}] - An object containing query params and headers for the request
* @returns {operation}
*/
export function put(path, data, options = {}) {
return async state => {
const [resolvedPath, resolvedData, resolvedOptions] = expandReferences(
state,
path,
data,
options
);

const optionsObject = {
data: resolvedData,
...resolvedOptions,
};
const response = await util.request(
state,
'PUT',
resolvedPath,
optionsObject
);

return util.prepareNextState(state, response);
};
}
3 changes: 2 additions & 1 deletion packages/kobotoolbox/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Adaptor from './Adaptor';
export default Adaptor;

export * from './Adaptor';
export * from './Adaptor';
export * as http from './http';

0 comments on commit 49b5fa4

Please sign in to comment.