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

Support PAT for dhis2 #897

Merged
merged 5 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/dhis2/configuration-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@
"examples": [
"v2"
]
},
"personalAccessToken": {
PiusKariuki marked this conversation as resolved.
Show resolved Hide resolved
"title": "Personal Access Token",
"type": "string",
"description": "Personal access token",
"minLength": 1,
"examples": [
"d2pat_vreYExmi0yh92Soy8CCF2zHG0wRjID494035128247"
]
}
},
"type": "object",
Expand Down
35 changes: 24 additions & 11 deletions packages/dhis2/src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import Qs from 'qs';
* before spreading the rest of the axios configuration, and (3) executes an
* axios request.
* @function
* @param {object} configuration - configuration must have a username and password
* @param {object} configuration - configuration can either be a username and password OR a personal access token
* @param {object} axiosRequest - the axiosRequest contains valid axios params: https://axios-http.com/docs/req_config
* @returns {Promise} a promise that will resolve to either a response object or an error object.
*/
export function request({ username, password }, axiosRequest) {
export function request({ username, password, personalAccessToken }, axiosRequest) {
const { method, url, params } = axiosRequest;

console.log(`Sending ${method} request to ${url}`);
Expand All @@ -22,13 +22,26 @@ export function request({ username, password }, axiosRequest) {
method.toLowerCase()
);

return axios({
headers: { 'Content-Type': 'application/json' },
responseType: 'json',
maxRedirects: safeRedirect ? 5 : 0,
auth: { username, password },
paramsSerializer: params => Qs.stringify(params, { arrayFormat: 'repeat' }),
// Note that providing headers or auth in the request object will overwrite.
...axiosRequest,
});
// Declare auth property and headers dynamically
const headers = { 'Content-Type': 'application/json'}
let auth;

if(personalAccessToken){
headers.Authorization = `ApiToken ${personalAccessToken}`
} else {
auth = { username, password }
}


return axios(
{
headers,
responseType: 'json',
auth,
maxRedirects: safeRedirect ? 5 : 0,
paramsSerializer: params => Qs.stringify(params, { arrayFormat: 'repeat' }),
// Note that providing headers or auth in the request object will overwrite.
...axiosRequest,
}
);
}