This topic is locked
[SOLVED]

 REST API Creation from ASPRunner.NET

6/2/2020 10:02:14 AM
ASPRunner.NET General questions
FrankR_ENTA author

A few weeks ago, I spent some time and put together a success Proof of Concept for some work we have to do - to start interfacing with a commercial system via REST API. The support in ASPRunner.NET worked very well, and I created two different REST Connections and I think 5 or so REST Views that were attached to ASPRunner.NET lists and pages. Very nice.
I'm about to return - this time to test REST API Creation from ASPRunner.NET. Via Stored Procedure or C#, ASPRunner.NET will wrap that up into a service endpoint that can be called by other ASPRunner.NET apps or other types of apps. My testing will involve creation of the APIs in an ASPRunner.NET project, and demo consumption of the APIs from a Python script.
If are experimenting with this too, please share your experiences here. Thanks.

FrankR_ENTA author 6/2/2020

Ok, I will start.
I have a first simple case running from ASPRunner.NET.
One interesting thing to note: the documentation talks about creating these endpoints via the SQL or View features. But actually, once you click Create REST API Miscellaneous settings, ASPRunner.NET generates endpoints automatically for all selected tables or custom views.
For my initial test, I created a Custom View called Docs. This was a query to retrieve a list of doctors from a table. I built and ran the app.

Then, in another browser window, I invoked that endpoint this way:



http://localhost:8086/api/v1?table=docs&action=list


That led to a data dump to the page.
I'm now off to go write a short Python script to properly consume that data.

FrankR_ENTA author 6/2/2020

And here is a simple Python script to illustrate accessing that data:





import datetime

import requests
REST_API_URL = "http://localhost:8086/api/v1?table=docs&action=list";
print('Start ' + str(datetime.datetime.now()))
# Call the endpoint

api_response = requests.get(REST_API_URL)

data = api_response.json()

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

for item in itemlist:

print (str(item['id']) + ' ' + item['doctor_last_name'] + ' ' + item['doctor_first_name'] + ' ' + item['NPI'])
print('End ' + str(datetime.datetime.now()))


FrankR_ENTA author 6/2/2020

Now I added Authentication to these endpoints.
I enabled Active Directory authentication in the server application.
The Python client app then would not return the result. This modified version with username and password passed does return the desired result.





import datetime

import requests

from requests.auth import HTTPBasicAuth
REST_API_URL = 'http://localhost:8086/api/v1?table=docs&action=list';

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

for item in itemlist:

print (str(item['id']) + ' ' + item['doctor_last_name'] + ' ' + item['doctor_first_name'] + ' ' + item['NPI'])

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()))


T
Tim 6/4/2020

Hey Frank,
I am excited for the possibilities of REST API also. I have created several views via GET for testing and it's going well. Though, it does take a bit to work out each providers particulars of their implementation.
The one thing I have in production so for is to provide an endpoint to our public website to query our internal DB for available time slots (it's a scheduling app) and to write back when a slot is taken. This couldn't have been any easier. In ASPR I just turned on REST API via the button on the Misc page, and then used this blog post to work out the endpoints I needed to use.
The next thing I want to figure out is how to call REST API endpoints from standard DB table events. For example, when I add a record to a table in my DB, I want to also insert data into an external system. So either in the Before or After record added event, make a call to the external endpoint. Or maybe it will be easier to add the record to the external system via the REST connection and use that event to execute the SQL to insert into my table. Anyway, that is what I'm going to work on next.
Did you ever figure out how to change the API credentials based on the current user (the question you asked in your other post)? That seems like a good thing to be able to do.
Tim

FrankR_ENTA author 6/4/2020



Hey Frank,
I am excited for the possibilities of REST API also. I have created several views via GET for testing and it's going well. Though, it does take a bit to work out each providers particulars of their implementation.
The one thing I have in production so for is to provide an endpoint to our public website to query our internal DB for available time slots (it's a scheduling app) and to write back when a slot is taken. This couldn't have been any easier. In ASPR I just turned on REST API via the button on the Misc page, and then used this blog post to work out the endpoints I needed to use.
The next thing I want to figure out is how to call REST API endpoints from standard DB table events. For example, when I add a record to a table in my DB, I want to also insert data into an external system. So either in the Before or After record added event, make a call to the external endpoint. Or maybe it will be easier to add the record to the external system via the REST connection and use that event to execute the SQL to insert into my table. Anyway, that is what I'm going to work on next.
Did you ever figure out how to change the API credentials based on the current user (the question you asked in your other post)? That seems like a good thing to be able to do.
Tim


I didn't figure out yet how to change the API credentials based on the current user - for the scenario where I am consuming a 3rd party REST API. I am told it is do able, and I am hoping future documentation adds will cover it.
I am now focusing on REST API Creation - creating API endpoints that other applications will consume. Had some easy early success, and I am now working on passing parameters.
Everything I learn will be documented in this thread. I am also this weekend going to test the waters on creating brief ASPRunner.NET Quick Video Guides that are short and illustrate a particular topic. If the initial effort is successful, the goal will be to document all the different use cases for the REST API Support in a series of short videos. If I get that far, the plan would be to loop back to the core ASPRunner.NET functionality and do Quick Video Guides for that as well.
When I was originally interested in REST Support in ASPRunner.NET, my head was on consumption of 3rd Party APIs. Our business is a medical practice, and we need to build apps that use the REST API for the practice's Electronic Health Records system. Once I proved out all that working nicely, I realized the support in ASPRunner.NET for Creation of REST APIs will be just as valuable to us. Depending on how those created API's can scale to properly support lots of connections, that can be a Gold Mine to many of us involved in API/Service development work these days.