Skip to content

Commit 4049179

Browse files
committed
add async api method
1 parent 7f7a934 commit 4049179

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed

web-call/async/APIAsync.sql

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
--======================================================
2+
-- Usage: APIAsync
3+
-- Notes:
4+
-- Dependencies: Required to enable 'show advanced options' and 'Ole Automation Procedures'
5+
/*
6+
sp_configure 'show advanced options', 1;
7+
go
8+
RECONFIGURE;
9+
GO
10+
sp_configure 'Ole Automation Procedures', 1;
11+
GO
12+
RECONFIGURE;
13+
GO
14+
*/
15+
-- History:
16+
-- Date Author Description
17+
-- 2020-05-21 DN Intial
18+
--======================================================
19+
DROP PROCEDURE IF EXISTS APIAsync
20+
GO
21+
CREATE PROCEDURE APIAsync @Url varchar(8000),
22+
@Method varchar(5) = 'GET',--POST
23+
@BodyData nvarchar(max) = NULL,--normally json object string : '{"key":"value"}',
24+
@Authorization varchar(8000) = NULL,--Basic auth token, APIAsync key,...
25+
@ContentType varchar(255) = 'application/json'--'application/xml'
26+
AS
27+
BEGIN
28+
SET NOCOUNT ON;
29+
30+
DECLARE @vWin int --token of WinHttp object
31+
DECLARE @vReturnCode int
32+
DECLARE @tResponse TABLE (ResponseText nvarchar(4000))
33+
34+
EXEC @vReturnCode = sp_OACreate 'WinHttp.WinHttpRequest.5.1',@vWin OUT
35+
IF @vReturnCode <> 0 GOTO EXCEPTION
36+
37+
-- This section can be put in a loop
38+
-- Store Win object into a temp table
39+
PRINT 'Win = ' + CONVERT(varchar(255), @vWin)
40+
SELECT @vReturnCode = dbo.SendAsync(@vWin, @Url, @Method, @BodyData, @Authorization, @ContentType)
41+
IF @vReturnCode <> 0 GOTO EXCEPTION
42+
43+
44+
-- This section can be put in a loop
45+
-- Process for each Win object from the temp table
46+
PRINT 'Get response by Win = ' + CONVERT(varchar(255), @vWin)
47+
INSERT
48+
INTO @tResponse
49+
SELECT ResponseText
50+
FROM dbo.GetResponseAsync(@vWin)
51+
WHERE ResponseText IS NOT NULL
52+
53+
IF @vReturnCode = 0
54+
GOTO RESULT
55+
56+
EXCEPTION:
57+
BEGIN
58+
DECLARE @tException TABLE
59+
(
60+
Error binary(4),
61+
Source varchar(8000),
62+
Description varchar(8000),
63+
HelpFile varchar(8000),
64+
HelpID varchar(8000)
65+
)
66+
67+
INSERT INTO @tException EXEC sp_OAGetErrorInfo @vWin
68+
INSERT
69+
INTO @tResponse
70+
(
71+
ResponseText
72+
)
73+
SELECT (
74+
SELECT *
75+
FROM @tException
76+
FOR JSON AUTO
77+
) AS ResponseText
78+
END
79+
80+
--FINALLY
81+
RESULT:
82+
--Dispose objects
83+
IF @vWin IS NOT NULL
84+
EXEC sp_OADestroy @vWin
85+
86+
--Result
87+
SELECT *
88+
FROM @tResponse
89+
90+
RETURN
91+
END
92+
/*
93+
EXEC APIAsync @Method = 'GET', @Url = 'http://dummy.restapiexample.com/api/v1/employees'
94+
EXEC API @Method = 'GET', @Url = 'http://dummy.restapiexample.com/api/v1/employees'
95+
96+
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--======================================================
2+
-- Usage: GetResponseAsync
3+
-- Notes:
4+
-- Dependencies: Required to enable 'show advanced options' and 'Ole Automation Procedures'
5+
/*
6+
sp_configure 'show advanced options', 1;
7+
go
8+
RECONFIGURE;
9+
GO
10+
sp_configure 'Ole Automation Procedures', 1;
11+
GO
12+
RECONFIGURE;
13+
GO
14+
*/
15+
-- History:
16+
-- Date Author Description
17+
-- 2020-05-21 DN Intial
18+
--======================================================
19+
DROP FUNCTION IF EXISTS GetResponseAsync
20+
GO
21+
CREATE FUNCTION GetResponseAsync (@Win INT)
22+
RETURNS @Result TABLE (Win INT, ResponseText nvarchar(4000))
23+
AS
24+
BEGIN
25+
DECLARE @vResponse nvarchar(4000)
26+
DECLARE @vTimeoutSec INT = 60
27+
DECLARE @vSuccess BIT = 0
28+
29+
--Wait
30+
EXEC sp_OAMethod @Win, 'WaitForResponse', NULL, @vTimeoutSec, @vSuccess OUT
31+
32+
--Response
33+
IF @vSuccess IS NOT NULL
34+
BEGIN
35+
EXEC sp_OAGetProperty @Win,'ResponseText', @vResponse OUT
36+
INSERT INTO @Result SELECT @Win, @vResponse
37+
END
38+
39+
IF @Win IS NOT NULL EXEC sp_OADestroy @Win
40+
41+
RETURN
42+
END
43+
/*
44+
-- See APIAsync
45+
*/

web-call/async/SendAsync.sql

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
--======================================================
2+
-- Usage: SendAsync
3+
-- Notes:
4+
-- Dependencies: Required to enable 'show advanced options' and 'Ole Automation Procedures'
5+
/*
6+
sp_configure 'show advanced options', 1;
7+
go
8+
RECONFIGURE;
9+
GO
10+
sp_configure 'Ole Automation Procedures', 1;
11+
GO
12+
RECONFIGURE;
13+
GO
14+
*/
15+
-- History:
16+
-- Date Author Description
17+
-- 2020-06-17 DN Intial
18+
--======================================================
19+
DROP FUNCTION IF EXISTS SendAsync
20+
GO
21+
CREATE FUNCTION SendAsync (
22+
@Win INT,
23+
@Url varchar(8000),
24+
@Method varchar(5) = 'GET',--POST
25+
@BodyData nvarchar(max) = NULL,--normally json object string : '{"key":"value"}',
26+
@Authorization varchar(8000) = NULL,--Basic auth token, SendAsync key,...
27+
@ContentType varchar(255) = 'application/json'--'application/xml'
28+
)
29+
RETURNS INT
30+
AS
31+
BEGIN
32+
DECLARE @vReturnCode int
33+
DECLARE @tResponse TABLE (ResponseText nvarchar(max))
34+
35+
EXEC @vReturnCode = sp_OAMethod @Win, 'Open', NULL, @Method/*Method*/, @Url /*Url*/, 'true' /*IsAsync*/
36+
IF @vReturnCode <> 0 GOTO RESULT
37+
38+
IF @Authorization IS NOT NULL
39+
BEGIN
40+
EXEC @vReturnCode = sp_OAMethod @Win, 'SetRequestHeader', NULL, 'Authorization', @Authorization
41+
IF @vReturnCode <> 0 GOTO RESULT
42+
END
43+
44+
IF @ContentType IS NOT NULL
45+
BEGIN
46+
EXEC @vReturnCode = sp_OAMethod @Win, 'SetRequestHeader', NULL, 'Content-Type', @ContentType
47+
IF @vReturnCode <> 0 GOTO RESULT
48+
END
49+
50+
IF @BodyData IS NOT NULL
51+
BEGIN
52+
EXEC @vReturnCode = sp_OAMethod @Win,'Send', NULL, @BodyData
53+
IF @vReturnCode <> 0 GOTO RESULT
54+
END
55+
ELSE
56+
BEGIN
57+
EXEC @vReturnCode = sp_OAMethod @Win,'Send'
58+
IF @vReturnCode <> 0 GOTO RESULT
59+
END
60+
61+
RESULT:
62+
RETURN @vReturnCode
63+
END
64+
/*
65+
-- See APIAsync
66+
*/

0 commit comments

Comments
 (0)