SRS Demo Instance
You can pull data from a number of workgroups from multiple instances in SRS. The SRS has two processes for extracting data through its API. The POST method is the first one, and the second one is the GET method. The POST and GET must done in sequence. You can not just use one of them only. I will provide additional details about these two approaches below.
As long as it supports basic authentication, any programming language can be utilised to access data in the SRS. I am using Python code as an example to extract data from the SRS. In this particular case, I extract person-specific information and subsequently convert that information from JSON to a Pandas dataframe. It is important to mention that the data of this individual is just a sample.
Below is a section where variables are declared and Python libraries are included. In the payload, you define the parameter that will be passed to the SRS. Defining multiple payloads may be required. Each payload may be used for each workgroup. The same case with the API username and password. You may need to define each API username and password for each instance.
import pandas as pd
import requests
from requests.auth import HTTPBasicAuth
import os
import time
import json
demo_srs_usr = os.environ["DEMO_SRS_USR"]
demo_srs_pwd = os.environ["DEMO_SRS_PWD"]
# Payload
payload = json.dumps({
"title": "ApiDemo",
"version": "API V2",
"start_date": "31/07/2023",
"end_date": "06/08/2023",
"workgroup_name": "JGM 2",
"cluster_name": "JGM Cluster"
})
# Headers
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}Below section is where the POST method is called. You may need to define a multiple endpoint. Same as the payload above, you need to define each endpoint for each instance. Once SRS receives this POST request, SRS will generate the data by running a SQL. In this example, I create a very simple SQL to generate static data. Below is the SQL that I used.
SELECT 1 AS clientid, 1 AS aliasid, 'A1' AS alphacode, 'John' AS firstname, 'Doe' AS lastname, '1980-01-01' AS dob, 1 AS sex, 'No' AS indigenous, 'None' AS language_main_not_english, 'English' AS language
UNION ALL
SELECT 2, 2, 'A2', 'Jane', 'Smith', '1990-02-02', 2, 'Yes', 'Spanish', 'Spanish'
UNION ALL
SELECT 3, 3, 'A3', 'Michael', 'Johnson', '1985-03-03', 1, 'No', 'None', 'English'
UNION ALL
SELECT 4, 4, 'A4', 'Emily', 'Williams', '1992-04-04', 2, 'No', 'French', 'French'
UNION ALL
SELECT 5, 5, 'A5', 'David', 'Brown', '1988-05-05', 1, 'Yes', 'Mandarin', 'Mandarin'# Create a session object
session = requests.Session()
# Set the authentication for the session
session.auth = HTTPBasicAuth(demo_srs_usr, demo_srs_pwd)
# URL of the endpoint
url = "https://srs-demo.infoxchangeapps.net.au/api/v1/report"
# Make the POST request using the session
response = session.post(url, headers=headers, data=payload)
# Print the response
print(response.text)
# Parse the JSON response and extract the report_id
response_data = response.json()
report_id = response_data.get("report_id")
# Print the report_id
print(f"Report ID: {report_id}")
# Pause for 5 seconds
time.sleep(5)Once you have the report_id from the POST session, then you can now send the GET method as shown below. The data in JSON format will be provided. That is it.
# Construct the new URL with the report_id
new_url = f"https://srs-demo.infoxchangeapps.net.au/api/v1/report/{report_id}"
# Make a GET request to the new URL
response = session.get(new_url, headers=headers)
# Print the response from the new URL
print(response.text)This part below is converting the JSON to Panda Datafram.
response_dict = json.loads(response.text)
# Extract relevant data
column_headers = response_dict['data']['api_demo']['column_headers']
data = response_dict['data']['api_demo']['data']
# Create DataFrame
srs_df = pd.DataFrame(data, columns=column_headers)
print(srs_df)