From 49b5fa4e3626da32b386a401efd831133376b647 Mon Sep 17 00:00:00 2001 From: Hunter Achieng Date: Fri, 31 Jan 2025 21:02:34 +0300 Subject: [PATCH] feat: implement generic http methods Signed-off-by: Hunter Achieng --- packages/kobotoolbox/src/http.js | 138 ++++++++++++++++++++++++++++++ packages/kobotoolbox/src/index.js | 3 +- 2 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 packages/kobotoolbox/src/http.js diff --git a/packages/kobotoolbox/src/http.js b/packages/kobotoolbox/src/http.js new file mode 100644 index 000000000..af7bb77cc --- /dev/null +++ b/packages/kobotoolbox/src/http.js @@ -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 GET forms resource + * 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 Create an asset resource + * 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 Update an asset resource + * 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); + }; +} diff --git a/packages/kobotoolbox/src/index.js b/packages/kobotoolbox/src/index.js index a013b3648..fa943e565 100644 --- a/packages/kobotoolbox/src/index.js +++ b/packages/kobotoolbox/src/index.js @@ -1,4 +1,5 @@ import * as Adaptor from './Adaptor'; export default Adaptor; -export * from './Adaptor'; \ No newline at end of file +export * from './Adaptor'; +export * as http from './http'; \ No newline at end of file