Skip to content
baileyjs78 edited this page Aug 19, 2019 · 1 revision

SQL API Interface

Best practices for using Feature Toggles within SQL are to have your C# application pass the value as a parameter. However, if you need to access the values directly in SQL, you will need 3 parts

  1. Access the API over HTTP
  2. Parse the JSON into a table
  3. Select the value from the table

Access the API

To access HTTP in SQL, I have used a stored proc spHTTPRequest such as

DECLARE @URL varchar(2000), 
	  @HttpMethodName varchar(50),
	  @RequestBody varchar(MAX), 
	  @Response varchar(MAX), 
	  @StatusCode varchar(1000), 
	  @StatusText varchar(1000),
	  @Environment varchar(50),
	  @Application varchar(50),
	  @MogglesServer varchar(50)

DECLARE @ParsedJSON TABLE(parent_ID int,Name varchar(255),Value varchar(255))

SET @Environment='DEV'
SET @Application='My App'
SET @MogglesServer='moggles.mydomain.com'
SET @URL = 'http://'+@MogglesServer+'/api/FeatureToggles/getApplicationFeatureToggles?Environment='+@Environment+'&applicationName='+@Application
SET @HttpMethodName = 'GET'
SET @RequestBody = NULL

EXEC [master].dbo.spHTTPRequest @URL, @HttpMethodName, @RequestBody, @Response OUT, @StatusCode OUT, @StatusText OUT

The full code for spHTTPRequest.

Parse JSON

If you are using MS SQL server 2014 or newer you can use the built in JSON features.

IF  (@StatusCode = '200' AND ISJSON(@Response) = 1)
BEGIN
	SELECT featureToggleName,isEnabled
	FROM OPENJSON(@Response)
	WITH ( featureToggleName nvarchar(255) '$.featureToggleName', isEnabled bit '$.isEnabled')
END