@@ -6,18 +6,165 @@ Welcome to SQL-APIConsumer project!. It's Database Project built in C# whose mai
66
77## Getting Started
88
9- This project has two main procedures defined below:
9+ The main procedure of this project is APICaller_Web_Extended, which can be used to call API of different web methods. It return an extended result including headers, and data of the server, within the result. In case we do need all these details, just need the results, instead we could use APICaller_WebMethod.
1010
11- 1 . ** APICaller_GET(SqlString URL)**
11+ With these two extended procedures we are able to change the content-type, through the header parameter.
12+
13+ 1 . ** SqlInt32 APICaller_Web_Extended(SqlString httpMethod, SqlString URL, SqlString Headers, SqlString JsonBody)**
14+ 1 . ** SqlInt32 APICaller_WebMethod(SqlString httpMethod, SqlString URL, SqlString JsonBody)**
15+
16+ ** APICaller_Web_Extended**
17+
18+ Below parameters received. This procedure return an integer depending on the execution. 0: Sucess. -1: Failed.
19+
20+ |Parameter |Description |Posible Value |Sample
21+ | :---: | :---: | :--- | :--- |
22+ | @httpMethod | HTTP Method that would be call | GET, POST, PUT,DELETE,PATCH| 'GET'|
23+ | @URL | URL intended to call | Valid URL | 'https://www.routingnumbers.info/api/name.json?rn=122242597' |
24+ | @Headers | Header related to request, if needed. | '' | '[ {"Name": "Content-Type", "Value" :"text/javascript; charset=utf-8" }] '|
25+ | @JsonBody | Json Body if needed. HTTP Get required a blank body | '' | ''|
26+
27+ Returned information related to HTTP Response by APICaller_Web_Extended:
28+
29+ | Parameter | Description |
30+ | :---: | :--- |
31+ | JsonResult | Result returned by API called |
32+ | ContentType | Returned Content Type |
33+ | ServerName | Server Name called |
34+ | StatusCode | HTTP Status Code reponse. Sample: 200,404,500 |
35+ | Description | HTTP Status response. Sample: OK |
36+ | Json_Headers | Header result |
37+
38+
39+ ### ** Sample calling APICaller_Web_Extended: GET**
40+
41+ ![ alt text] ( https://github.com/geral2/SQL-APIConsumer/blob/release_v2.3/images/Web_GET_Extended_Result.png )
42+
43+ ![ alt text] ( https://github.com/geral2/SQL-APIConsumer/blob/release_v2.3/images/Web_GET_Extended_Query.png )
44+
45+ ```
46+ GO
47+ DECLARE @httpMethod nvarchar(max) = 'GET'
48+ DECLARE @URL nvarchar(max) = 'https://www.routingnumbers.info/api/name.json?rn=122242597'
49+ DECLARE @Headers nvarchar(max) = '[{"Name": "Content-Type", "Value" :"text/javascript; charset=utf-8" }]';
50+
51+ DECLARE @JsonBody nvarchar(max) = ''
52+
53+ Declare @ts as table
54+ (
55+ Json_Result nvarchar(max),
56+ ContentType varchar(100),
57+ ServerName varchar(100),
58+ Statuscode varchar(100),
59+ Descripcion varchar(100),
60+ Json_Headers nvarchar(max)
61+ )
62+
63+ DECLARE @i AS INT
64+
65+ INSERT INTO @ts
66+
67+ EXECUTE @i = [dbo].[APICaller_Web_Extended]
68+ @httpMethod
69+ ,@URL
70+ ,@Headers
71+ ,@JsonBody
72+
73+ SELECT * FROM @ts
74+
75+ SELECT
76+ [name]
77+ ,[rn]
78+ ,[message]
79+ ,[code]
80+ FROM (
81+ SELECT Context = Json_Result
82+ from @ts
83+ )tb
84+ OUTER APPLY OPENJSON (context)
85+ WITH
86+ ( [name] VARCHAR(20) '$.name'
87+ , [rn] VARCHAR(20) '$.rn'
88+ , [message] VARCHAR(20) '$.message'
89+ , [code] INT '$.code'
90+ );
91+
92+ SELECT *
93+ FROM OPENJSON((select Json_Headers from @ts))
94+ WITH (
95+ Header NVARCHAR(MAX) '$."Name"'
96+ ,Value NVARCHAR(MAX) '$."Value"'
97+ ) a
98+ ```
99+
100+ ### ** Sample calling APICaller_Web_Extended: POST**
101+
102+ ![ alt text] ( https://github.com/geral2/SQL-APIConsumer/blob/release_v2.3/images/POST_Extended_Result.png )
103+
104+ ![ alt text] ( https://github.com/geral2/SQL-APIConsumer/blob/release_v2.3/images/POST_Extended_query.png )
105+
106+ ```
107+ GO
108+ DECLARE @httpMethod nvarchar(max) = 'POST'
109+ DECLARE @URL nvarchar(max) = 'https://url-shortener-service.p.rapidapi.com/shorten'
110+ DECLARE @Headers nvarchar(max) = '[{ "Name": "Content-Type", "Value" :"application/x-www-form-urlencoded" }
111+ ,{ "Name": "X-RapidAPI-Host","Value" :"url-shortener-service.p.rapidapi.com"}
112+ ,{ "Name": "X-RapidAPI-Key", "Value" :"c56b333d25mshdbfec15f02f096ep19fa94jsne5189032cf7d"}
113+ ,{"Name": "useQueryString","Value" :"true"}]';
114+
115+ DECLARE @JsonBody nvarchar(max) = 'url=https://www.linkedin.com/in/geraldo-diaz/'
116+
117+ Declare @ts as table
118+ (
119+ Json_Result NVARCHAR(MAX),
120+ ContentType VARCHAR(100),
121+ ServerName VARCHAR(100),
122+ Statuscode VARCHAR(100),
123+ Descripcion VARCHAR(100),
124+ Json_Headers NVARCHAR(MAX)
125+ )
126+
127+ DECLARE @i AS INT
128+
129+ INSERT INTO @ts
130+ EXECUTE @i = [dbo].[APICaller_Web_Extended]
131+ @httpMethod
132+ ,@URL
133+ ,@Headers
134+ ,@JsonBody
135+
136+ SELECT * FROM @ts
137+
138+ SELECT
139+ Result = [name]
140+ FROM (
141+ SELECT Context = Json_Result
142+ from @ts
143+ )tb
144+ OUTER APPLY OPENJSON (context)
145+ WITH
146+ ( [name] VARCHAR(20) '$.result_url' );
147+
148+ SELECT *
149+ FROM OPENJSON((select Json_Headers from @ts))
150+ WITH (
151+ Header NVARCHAR(MAX) '$."Name"'
152+ ,Value NVARCHAR(MAX) '$."Value"'
153+ ) a
154+ ```
155+
156+ Initially the procedures below were the main objects of this project, but these were deprecated due the generic webmethod above:
157+
158+ 1 . ** APICaller_GET(SqlString URL)**
121591 . ** APICaller_POST(SqlString URL, SqlString JsonBody)**
13160
14- The same also support Authentications header like Token or JWT.
161+ The same also support Authentications header like Token or JWT (Deprecated) .
15162
161631 . ** APICaller_GETAuth(SqlString URL, SqlString Authorization)**
171641 . ** APICaller_POSTAuth(SqlString URL, SqlString Authorization, SqlString JsonBody)**
18165(More info in the wiki)
19166
20- It even support sending multiples headers in a Json Format.
167+ It even support sending multiples headers in a Json Format (Deprecated) .
21168
221691 . ** APICaller_GET_headers(SqlString URL, SqlString Headers)**
231701 . ** APICaller_POST_headers(SqlString URL, SqlString Headers)**
@@ -30,14 +177,6 @@ This new procedure is exclusive for Calling API with enconded contentType (appli
301771 . ** APICaller_GET_Extended(SqlString URL, SqlString Headers, SqlString JsonBody)**
311781 . ** APICaller_POST_Extended(SqlString URL, SqlString Headers, SqlString JsonBody)**
32179
33- With these two extended procedures we are able to change the content-type, through the header parameter.
34- And return information related to HTTP Response like:
35- - ContentType
36- - Server
37- - StatusCode
38- - Status Description
39- - Response Headers
40-
41180There are a few Utilities functions;
42181
431821 . ** GetTimestamp**
@@ -92,7 +231,6 @@ USE [TestDB]
92231GO
93232EXEC dbo.sp_changedbowner @loginame = N'sa', @map = false
94233GO
95- WITH PERMISSION_SET = UNSAFE--external_access
96234```
97235Error mentioned above:
98236Msg 15404, Level 16, State 11, Line 1
@@ -143,6 +281,25 @@ If you do not know the path where this dll is located or this command above does
143281After that we can create our CLR Stored procedures:
144282
145283```
284+ GO
285+ PRINT N'Creating [dbo].[APICaller_WebMethod]...';
286+ GO
287+
288+ CREATE PROCEDURE [dbo].[APICaller_WebMethod]
289+ @httpMethod NVARCHAR (MAX) NULL, @URL NVARCHAR (MAX) NULL, @JsonBody NVARCHAR (MAX) NULL
290+ AS EXTERNAL NAME [API_Consumer].[StoredProcedures].[APICaller_WebMethod]
291+
292+
293+ GO
294+ PRINT N'Creating [dbo].[APICaller_Web_Extended]...';
295+ GO
296+
297+ CREATE PROCEDURE [dbo].[APICaller_Web_Extended]
298+ @httpMethod NVARCHAR (MAX) NULL, @URL NVARCHAR (MAX) NULL, @Headers NVARCHAR (MAX) NULL, @JsonBody NVARCHAR (MAX) NULL
299+ AS EXTERNAL NAME [API_Consumer].[StoredProcedures].[APICaller_Web_Extended]
300+
301+ GO
302+
146303PRINT N'Creating [dbo].[Create_HMACSHA256]...';
147304
148305GO
0 commit comments