## GET DHIS2 information let username = 'Your dhis2 username'; let password = 'Your dhis2 password'; let urlApi=baseUrl."/api/dataValueSets?dataSet=2sae1wexar5&period=201806&orgUnit=khG2ss1Ax2Y"; let options = { method: 'GET', url: urlApi, headers: { 'Authorization': fn.base_64_auth(username,password), 'Accept': 'application/json', 'Content-Type': 'application/json' }, from: { mimeType: 'application/json' } }; // end of options // get json response request(options, function(error, response, body) { let dataOutput = JSON.stringify(JSON.parse(body)); } ## POST DHIS2 information let username = 'Your dhis2 username'; let password = 'Your dhis2 password'; let jsonPayload = '{ "dataSet": "iReVg2xgFPL", //Monthly eLMIS Dataset-final "completeDate": "2018-05-13", "period": "201801", "orgUnit": "WPqHGtwerEu", // Jhalokati Civil Surgeons Office, Jhalokati "dataValues": [ { "dataElement": "ta8v9gj6aQC", "value": "100" }, { "dataElement": "yQfHIwxvCmb", "value": "200" }, { "dataElement": "CRueZPFX52M", "value": "300" } ] }'; let postApiUrl= 'yoururl/dhis28/api/dataValueSets'; let options = { method: 'POST', url: postApiUrl, body: JSON.stringify(jsonPayload), headers: { 'Authorization': fn.base_64_auth(username,password), 'Accept': 'application/json', 'Content-Type': 'application/json' }, from: { mimeType: 'application/json' } }; // end of options // get json response request(options, function(error, response, body) { let getResponse = JSON.stringify(JSON.parse(response)); }
## POST to DHIS2 String username = "your dhis2 username"; String password = "your dhis2 password"; URL url = new URL("baseUrl/api/organisationUnits/YmlGwRSMfmX.json?fields=ancestors,children,name"); # open connection HttpURLConnection con = (HttpURLConnection) url.openConnection(); # add request header con.setDoOutput(true); con.setRequestProperty("Authorization", "Basic " + encoding); con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); String value = "value="+orgUnitValue+""; # Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(value); wr.flush(); wr.close(); int responseCode = con.getResponseCode();
dhis.json file: { "dhis": { "baseurl": "https://server.datim.org", "username": "username", "password": "password" }, "database": { "host": "localhost", "port": 5432, "username": "username", "password": "password", "dbname": "dhis2" } } #!/usr/bin/env python3 # modifyDataStore.py, a python script by Ben Guaraldi import sys import json import requests import collections # Get parameters from dish.json config = json.load(open('/opt/dhis2/dish.json', 'r')) url = config['dhis']['baseurl'] + '/api/dataStore/assignments/organisationUnitLevels.json' # Get the current JSON from the organisationUnitLevels key of the assignments namespace in the data store credentials = (config['dhis']['username'], config['dhis']['password']) req = requests.get(url, auth=credentials) j = req.json() # Construct a new JSON from the current JSON, adding a new key and resorting it j['Python'] = j['Angola'].copy() j['Python']['name3'] = 'Python' k = sorted(j.items()) # Since dicts don't have an order to their indices, # we need to create an ordered dict and then # save the sorted values to it l = collections.OrderedDict() for i in k: l[i[0]] = i[1] # Replace the old JSON with the new JSON print(requests.put(url, data=json.dumps(l), auth=credentials, headers={'content-type': 'application/json'})) # Report completion and exit with status 0 print('Exiting normally') exit(0)
dhis.json file: { "dhis": { "baseurl": "https://server-url", "username": "username", "password": "password" }, "database": { "host": "localhost", "port": 5432, "username": "username", "password": "password", "dbname": "dhis2" } } #!/usr/bin/env Rscript # modifyDataStore.R, an R script by Jason Pickering loosely based # on a script by Ben Guaraldi (and lightly edited by same) require(httr) require(rlist) require(jsonlite) require(magrittr) require(assertthat) # Get parameters from dish.json config <- fromJSON('/opt/dhis2/dish.json') url<-paste0(config$dhis$baseurl, '/api/dataStore/assignments/organisationUnitLevels.json') # Get the current JSON from the organisationUnitLevels key of the assignments namespace in the data store j <-GET(url, authenticate(config$dhis$username, config$dhis$password)) %>% content(., "text") %>% fromJSON(.) # Construct a new JSON from the current JSON, adding a new key and resorting it j$Pirate <- j$Zimbabwe j$Pirate$name3 <- 'Pirate' j <- j[order(names(j))] # Replace the old JSON with the new JSON r <- PUT(url, body = toJSON(j, auto_unbox = TRUE), content_type_json()) # Report completion message('Exiting normally')
dhis.json file: { "dhis": { "baseurl": "https://server.datim.org", "username": "username", "password": "password" }, "database": { "host": "localhost", "port": 5432, "username": "username", "password": "password", "dbname": "dhis2" } } #!/usr/bin/env bash # modifyDataStore.sh, a shell script by Ben Guaraldi # If run on OS X, you must install GNU grep to ggrep. # If run on something other than OS X, it will probably work if you change lines # 14, 15, and 16 from ggrep to grep. # Exit immediately upon non-zero exit status set -e ; set -o pipefail # Get parameters from dish.json url=$(ggrep -Pzo '(?s)"dhis".*?\{.*?"baseurl":.*?".*?"' /opt/dhis2/dish.json | awk -F'"' '/baseurl/ {print $4}') username=$(ggrep -Pzo '(?s)"dhis".*?\{.*?"username":.*?".*?"' /opt/dhis2/dish.json | awk -F'"' '/username/ {print $4}') password=$(ggrep -Pzo '(?s)"dhis".*?\{.*?"password":.*?".*?"' /opt/dhis2/dish.json | awk -F'"' '/password/ {print $4}') # Get the current JSON from the organisationUnitLevels key of the assignments namespace in the data store json=$(curl -X GET -u "${username:?}:${password:?}" "${url:?}/api/dataStore/assignments/organisationUnitLevels.json") # Construct a new JSON from the current JSON, adding a new key and resorting it angola=$(echo $json | tr '}' '\n' | head -1) new=$(echo $json | tr '}' '\n' | grep 'Ghana' | sed -e 's/Ghana/Bash/g') sorted=$(printf "$new\n$json" | tr '}' '\n' | sort | grep -v 'Angola') printf "$angola$sorted}}" | tr '\n' '}' | tee /tmp/final.json | wc # Replace the old JSON with the new JSON curl -X PUT -d @/tmp/final.json -H "Content-Type:application/json" -u "${username:?}:${password:?}" "${url:?}/api/dataStore/assignments/organisationUnitLevels" # Remove the temporary file rm /tmp/final.json # Report completion and exit with status 0 echo '.' echo 'Exiting normally' exit 0
## GET DHIS2 information let username = 'Your dhis2 username'; let password = 'Your dhis2 password'; let urlApi=baseUrl."/api/dataValueSets?dataSet=2sae1wexar5&period=201806&orgUnit=khG2ss1Ax2Y"; let options = { method: 'GET', url: urlApi, headers: { 'Authorization': fn.base_64_auth(username,password), 'Accept': 'application/json', 'Content-Type': 'application/json' }, from: { mimeType: 'application/json' } }; // end of options // get json response request(options, function(error, response, body) { let dataOutput = JSON.stringify(JSON.parse(body)); } ## POST DHIS2 information let username = 'Your dhis2 username'; let password = 'Your dhis2 password'; let jsonPayload = '{ "dataSet": "iReVg2xgFPL", //Monthly eLMIS Dataset-final "completeDate": "2018-05-13", "period": "201801", "orgUnit": "WPqHGtwerEu", // Jhalokati Civil Surgeons Office, Jhalokati "dataValues": [ { "dataElement": "ta8v9gj6aQC", "value": "100" }, { "dataElement": "yQfHIwxvCmb", "value": "200" }, { "dataElement": "CRueZPFX52M", "value": "300" } ] }'; let postApiUrl= 'yoururl/dhis28/api/dataValueSets'; let options = { method: 'POST', url: postApiUrl, body: JSON.stringify(jsonPayload), headers: { 'Authorization': fn.base_64_auth(username,password), 'Accept': 'application/json', 'Content-Type': 'application/json' }, from: { mimeType: 'application/json' } }; // end of options // get json response request(options, function(error, response, body) { let getResponse = JSON.stringify(JSON.parse(response)); }
## POST to DHIS2 String username = "your dhis2 username"; String password = "your dhis2 password"; URL url = new URL("baseUrl/api/organisationUnits/YmlGwRSMfmX.json?fields=ancestors,children,name"); # open connection HttpURLConnection con = (HttpURLConnection) url.openConnection(); # add request header con.setDoOutput(true); con.setRequestProperty("Authorization", "Basic " + encoding); con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); String value = "value="+orgUnitValue+""; # Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(value); wr.flush(); wr.close(); int responseCode = con.getResponseCode();
dhis.json file: { "dhis": { "baseurl": "https://server.datim.org", "username": "username", "password": "password" }, "database": { "host": "localhost", "port": 5432, "username": "username", "password": "password", "dbname": "dhis2" } } #!/usr/bin/env python3 # modifyDataStore.py, a python script by Ben Guaraldi import sys import json import requests import collections # Get parameters from dish.json config = json.load(open('/opt/dhis2/dish.json', 'r')) url = config['dhis']['baseurl'] + '/api/dataStore/assignments/organisationUnitLevels.json' # Get the current JSON from the organisationUnitLevels key of the assignments namespace in the data store credentials = (config['dhis']['username'], config['dhis']['password']) req = requests.get(url, auth=credentials) j = req.json() # Construct a new JSON from the current JSON, adding a new key and resorting it j['Python'] = j['Angola'].copy() j['Python']['name3'] = 'Python' k = sorted(j.items()) # Since dicts don't have an order to their indices, # we need to create an ordered dict and then # save the sorted values to it l = collections.OrderedDict() for i in k: l[i[0]] = i[1] # Replace the old JSON with the new JSON print(requests.put(url, data=json.dumps(l), auth=credentials, headers={'content-type': 'application/json'})) # Report completion and exit with status 0 print('Exiting normally') exit(0)
dhis.json file: { "dhis": { "baseurl": "https://server-url", "username": "username", "password": "password" }, "database": { "host": "localhost", "port": 5432, "username": "username", "password": "password", "dbname": "dhis2" } } #!/usr/bin/env Rscript # modifyDataStore.R, an R script by Jason Pickering loosely based # on a script by Ben Guaraldi (and lightly edited by same) require(httr) require(rlist) require(jsonlite) require(magrittr) require(assertthat) # Get parameters from dish.json config <- fromJSON('/opt/dhis2/dish.json') url<-paste0(config$dhis$baseurl, '/api/dataStore/assignments/organisationUnitLevels.json') # Get the current JSON from the organisationUnitLevels key of the assignments namespace in the data store j <-GET(url, authenticate(config$dhis$username, config$dhis$password)) %>% content(., "text") %>% fromJSON(.) # Construct a new JSON from the current JSON, adding a new key and resorting it j$Pirate <- j$Zimbabwe j$Pirate$name3 <- 'Pirate' j <- j[order(names(j))] # Replace the old JSON with the new JSON r <- PUT(url, body = toJSON(j, auto_unbox = TRUE), content_type_json()) # Report completion message('Exiting normally')
dhis.json file: { "dhis": { "baseurl": "https://server.datim.org", "username": "username", "password": "password" }, "database": { "host": "localhost", "port": 5432, "username": "username", "password": "password", "dbname": "dhis2" } } #!/usr/bin/env bash # modifyDataStore.sh, a shell script by Ben Guaraldi # If run on OS X, you must install GNU grep to ggrep. # If run on something other than OS X, it will probably work if you change lines # 14, 15, and 16 from ggrep to grep. # Exit immediately upon non-zero exit status set -e ; set -o pipefail # Get parameters from dish.json url=$(ggrep -Pzo '(?s)"dhis".*?\{.*?"baseurl":.*?".*?"' /opt/dhis2/dish.json | awk -F'"' '/baseurl/ {print $4}') username=$(ggrep -Pzo '(?s)"dhis".*?\{.*?"username":.*?".*?"' /opt/dhis2/dish.json | awk -F'"' '/username/ {print $4}') password=$(ggrep -Pzo '(?s)"dhis".*?\{.*?"password":.*?".*?"' /opt/dhis2/dish.json | awk -F'"' '/password/ {print $4}') # Get the current JSON from the organisationUnitLevels key of the assignments namespace in the data store json=$(curl -X GET -u "${username:?}:${password:?}" "${url:?}/api/dataStore/assignments/organisationUnitLevels.json") # Construct a new JSON from the current JSON, adding a new key and resorting it angola=$(echo $json | tr '}' '\n' | head -1) new=$(echo $json | tr '}' '\n' | grep 'Ghana' | sed -e 's/Ghana/Bash/g') sorted=$(printf "$new\n$json" | tr '}' '\n' | sort | grep -v 'Angola') printf "$angola$sorted}}" | tr '\n' '}' | tee /tmp/final.json | wc # Replace the old JSON with the new JSON curl -X PUT -d @/tmp/final.json -H "Content-Type:application/json" -u "${username:?}:${password:?}" "${url:?}/api/dataStore/assignments/organisationUnitLevels" # Remove the temporary file rm /tmp/final.json # Report completion and exit with status 0 echo '.' echo 'Exiting normally' exit 0