This topic is locked
[SOLVED]

 Creating REST API and consuming from other applications

6/23/2020 4:23:03 PM
ASPRunner.NET General questions
FrankR_ENTA author

Just wanted to document something here regarding Creating a REST API with ASPRunner.NET but then consuming it from other places.
If you want to create a REST API and pass a parameter to it, it is important to use :{filter.Field}, not :{search.Field} in the SQL View definition.

Then to consume that, the url with a parameter passed would be: http://localhost:XXXX/api/v1?table=mytable&action=list&q=(Field~equals~somevalue)

FrankR_ENTA author 6/24/2020

Wanted to document a little more in case it can help someone down the road starting on this:
So, with an ASPRunner.NET project with REST API support selected, I created a new SQL View called PatientHistory. It is a SQL query to retrieve a list of history records for a patient. The database is the database of our commercial EHR - Electronic Health Records system. I work for a medical practice. This is a Perfect use for a REST API.
The SQL was copied into the List definition tab of the SQL View. When you Run the SQL from that tab, if it runs successfully, ASPRunner.NET nicely interprets the result and shows you the list of possible data elements coming back from the query. To define a parameter to this, add a Filter. My filter was the Id of the patient, called a PersonId in our system.

So, the WHERE clause of the SQL became:



WHERE (PersonId) = ':{filter.PersonId}')


That's it from the ASPRunner.NET side. When the project is run, this endpoint is now available:



http://localhost:8086/api/v1?table=patienthistory&action=list&q=(PersonId~equals~xxxxxxxx)


Next was consuming that from another application. Here is a short Python script simply illustrating how to call that endpoint and retrieve the results.

I will post again in the future about REST Authentication when I learn more about what is available in the ASPRunner.NET REST Support.

This project uses Authentication, in the form of Windows Authentication, so I needed to pass valid Windows credentials to be able to use the API from the script.



#------------------------------------------------------------------------------------------------------------------------------

# Endpoints are Created using the new REST Support in ASPRunner.NET 10.4.

# This is a simple separate external client test driver for those endpoints.

# FR, 6/2020

#------------------------------------------------------------------------------------------------------------------------------

import datetime

import requests

from requests.auth import HTTPBasicAuth
APPSERVER = 'http://localhost:8086'
#------------------------------------------------------------------------------------------------------------------------------

# Get Patient History

#------------------------------------------------------------------------------------------------------------------------------

REST_API_URL = APPSERVER + '/api/v1?table=patienthistory&action=list&q=(PersonId~equals~12345678)'
USERNAME = '##########'

PASSWORD = '##########'
print('Start ' + str(datetime.datetime.now()))
try:
# Call the endpoint

api_response = requests.get(REST_API_URL, auth=HTTPBasicAuth(USERNAME, PASSWORD))
if api_response.status_code == 200:

print('Results: ')
data = api_response.json()

itemlist = data['data']
# Iterate the list of results

for item in itemlist:

print (str(item['Date']) + ' ' + str(item['Location']) + ' ' + str(item['Resource']) + ' ' + str(item['Event']) + ' ' + str(item['Status']))

else:

print ('Error retrieving data: ' + str(api_response.status_code) + ' ' + str(api_response.reason))
except:

print ('Unexpected error retrieving data')
print('End ' + str(datetime.datetime.now()))

print(' ')

print(' ')
jadachDevClub member 6/24/2020

Thanks for sharing