This topic is locked
[SOLVED]

REST API Support Addition - retrieving data from an INSERT

4/23/2021 11:00:21 AM
ASPRunner.NET Tips and tricks
FrankR_ENTA author

I have used Runner REST API support very successfully for multiple projects. While I have consumed existing REST APIs with the support, my focus has actually been on Creation of REST APIs. The generated services have been high peforming, error free, and they scaled well.

I came up with an obstacle recently. I need to create an INSERT operation that would write a record. We always code our access layer as SQL Server Stored Procedures, and then we invoke those procs from our apps.

We started with a stored proc to do the INSERT.

----------------------------------------------------------------------------------------------------------
-- INSERT a record into the customers table.
-- For demonstration purposes.
----------------------------------------------------------------------------------------------------------
ALTER PROCEDURE [dbo].[usp_InsertCustomer]
-- Add the parameters for the stored procedure here
-- Input Parameters
@first_name nvarchar(50) = NULL,
@last_name nvarchar(50) = NULL

AS
BEGIN

DECLARE @result_code int
DECLARE @result_message nvarchar(250)
DECLARE @cust_id int

SET NOCOUNT ON;

----------------------------------------------------------------------------------------------------------
-- Perform the recorod INSERT
----------------------------------------------------------------------------------------------------------
INSERT INTO [dbo].[customers]
(
[first_name]
,[last_name]
)
VALUES
(
@first_name,
@last_name
)

----------------------------------------------------------------------------------------------------------
-- Check the status of the INSERT
----------------------------------------------------------------------------------------------------------
SET @result_code = @@ERROR;
IF (@result_code <> 0)
BEGIN
SET @result_message = 'Failure'
END
ELSE
BEGIN
-- Return the id of the new appointment record
SET @cust_id = SCOPE_IDENTITY()
SET @result_message = 'Success'
END

-- Return results
SELECT @result_code as N'result_code',
@result_message as N'result_message',
@cust_id as N'cust_id'
END
FrankR_ENTA author 4/23/2021

When we invoked that proc as an INSERT, it by default returned a return code, return message, and, the data that was sent as input parms. However, since this was an INSERT, we really needed - the id of the record that was created.

Xlinesoft Support came to the rescue - again. Per direction of Sergey, I generated C# for the stored proc invocation, and then we modified it this way:

string sql = "EXEC [dbo].[usp_InsertCustomer]\n @first_name = ':{first_name}',\n @last_name = ':{last_name}'";
XVar preparedSQL = DB.PrepareSQL( sql );

//XVar result = DB.Exec(preparedSQL);
QueryResult result = DB.Query(preparedSQL);

if( XVar.Equals( result, false ) ) {
dataSource.setError( DB.LastError() );
return false;
}

//return command.values;
return result.fetchAssoc();

The two lines below the commented out lines were the new lines provided by Sergey.

That simple change worked flawlessly, and the proc is now returning the record id that was created.

To test this, we use this client Python script invocation:

#------------------------------------------------------------------------------------------------------------------------------
# APIDemo Client Python script for invoking InsertCustomer endpoint.
#------------------------------------------------------------------------------------------------------------------------------
import datetime
import sys
import requests
from requests.auth import HTTPBasicAuth

APPSERVER = 'http://localhost:8086'

print(' ')

#------------------------------------------------------------------------------------------------------------------------------
# Endpoint: InsertCustomer
#------------------------------------------------------------------------------------------------------------------------------
REST_API_URL = APPSERVER + '/api/v1?table=insertcustomer&action=insert'

try:

# Establish parameters in body
body = {
'first_name' : 'Joe',
'last_name' : 'Smith'
}

# Call the endpoint
api_response = requests.post(REST_API_URL, data = body)

if api_response.status_code == 200:
print('Results: ')

api_response_data = api_response.json()
print(str(api_response_data))

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

except:
print ('Unexpected error retrieving data')

print(' ')

yielding these results:

Results:
{'success': True, 'data': {'result_code': 0, 'result_message': 'Success', 'cust_id': 70}}

Fantastic.