Skip to content

Commit d4296a0

Browse files
authored
Merge pull request #4 from geral2/Development
Development
2 parents 37bfd37 + 61b358e commit d4296a0

17 files changed

+227
-19
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@
33
################################################################################
44

55
/.vs/SQL-APIConsumer/v15/Server/sqlite3/db.lock
6+
/API_Consumer/SQL-APIConsumer.dbmdl
7+
/API_Consumer/SQL-APIConsumer.jfm
8+
/API_Consumer/bin/Debug/SQL-APIConsumer_1.publish.sql
9+
/API_Consumer/bin/Debug/SQL-APIConsumer_2.publish.sql
10+
/API_Consumer/bin/Debug/SQL-APIConsumer.publish.sql
11+
/API_Consumer/bin/Debug/DeploymentReport_6.txt
12+
/API_Consumer/bin/Debug/DeploymentReport_4.txt
13+
/API_Consumer/bin/Debug/DeploymentReport_5.txt

.vs/SQL-APIConsumer/v15/.suo

16.5 KB
Binary file not shown.
416 KB
Binary file not shown.
0 Bytes
Binary file not shown.
3.54 MB
Binary file not shown.

API_Consumer/Consumers/APIConsumer.cs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,34 @@ public static class APIConsumer
1616
/// </summary>
1717
private const string CONTENTTYPE = "application/json; charset=utf-8";
1818

19+
/// <summary>
20+
/// Fixed string for POST method
21+
/// </summary>
22+
private const string POST_WebMethod = "POST";
23+
24+
/// <summary>
25+
/// Fixed string for GET method
26+
/// </summary>
27+
private const string GET_WebMethod = "GET";
28+
1929
/// <summary>
2030
/// POST to Resful API sending Json body.
2131
/// </summary>
2232
/// <param name="url">API URL</param>
2333
/// <param name="JsonBody">Content Application By Default Json</param>
34+
/// <param name="Token">Header Authorization token</param>
2435
/// <returns>String Api result</returns>
25-
public static string POSTMethod(string url, string JsonBody)
36+
public static string POSTMethod(string url, string JsonBody, string Token = "")
2637
{
2738
string ContentResult = string.Empty ;
2839
try
2940
{
3041
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
3142
request.ContentType = CONTENTTYPE;
32-
request.Method = "POST";
43+
request.Method = POST_WebMethod;
44+
45+
if (!String.IsNullOrEmpty(Token))
46+
request.Headers.Add(HttpRequestHeader.Authorization, Token);
3347

3448
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
3549
{
@@ -57,15 +71,19 @@ public static string POSTMethod(string url, string JsonBody)
5771
/// Request GET Method to the URL API provided.
5872
/// </summary>
5973
/// <param name="url">API URL</param>
74+
/// <param name="Token">Header Authorization Token</param>
6075
/// <returns>String Api result</returns>
61-
public static string GETMethod(string url)
76+
public static string GETMethod(string url, string Token = "")
6277
{
6378
string ContentResult = string.Empty;
6479
try
6580
{
6681
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
6782
request.ContentType = CONTENTTYPE;
68-
request.Method = "GET";
83+
request.Method = GET_WebMethod;
84+
85+
if (!String.IsNullOrEmpty(Token))
86+
request.Headers.Add(HttpRequestHeader.Authorization, Token);
6987

7088
var httpResponse = (HttpWebResponse)request.GetResponse();
7189
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
@@ -88,15 +106,19 @@ public static string GETMethod(string url)
88106
/// </summary>
89107
/// <param name="url">API URL</param>
90108
/// <param name="Id">Id</param>
109+
/// <param name="Token">Authorization Token</param>
91110
/// <returns>String Api result</returns>
92-
public static string GETMethod(string url, string Id)
111+
public static string GETMethod(string url, string Id, string Token = "")
93112
{
94113
string ContentResult = string.Empty;
95114
try
96115
{
97116
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Concat(url,"/",Id));
98117
request.ContentType = CONTENTTYPE;
99-
request.Method = "GET";
118+
request.Method = GET_WebMethod;
119+
120+
if (!string.IsNullOrEmpty(Token))
121+
request.Headers.Add(HttpRequestHeader.Authorization, Token);
100122

101123
var httpResponse = (HttpWebResponse)request.GetResponse();
102124
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
@@ -119,15 +141,19 @@ public static string GETMethod(string url, string Id)
119141
/// </summary>
120142
/// <typeparam name="T">Object used to deserialize the result</typeparam>
121143
/// <param name="url">API URL</param>
144+
/// <param name="Token">Authorization token</param>
122145
/// <returns>String Api result</returns>
123-
public static string GETMethod<T>(string url, ref T ObjectResult)
146+
public static string GETMethod<T>(string url, ref T ObjectResult, string Token = "")
124147
{
125148
string ContentResult = string.Empty;
126149
try
127150
{
128151
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
129152
request.ContentType = CONTENTTYPE;
130-
request.Method = "GET";
153+
request.Method = GET_WebMethod;
154+
155+
if (!string.IsNullOrEmpty(Token))
156+
request.Headers.Add(HttpRequestHeader.Authorization, Token);
131157

132158
var httpResponse = (HttpWebResponse)request.GetResponse();
133159
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
@@ -151,15 +177,19 @@ public static string GETMethod<T>(string url, ref T ObjectResult)
151177
/// </summary>
152178
/// <typeparam name="T">Object used to deserialize the result</typeparam>
153179
/// <param name="url">API URL</param>
180+
/// <param name="Token">Authorization token</param>
154181
/// <returns>String Api result</returns>
155-
public static string GETMethod<T>(string url, string Id, ref T ObjectResult)
182+
public static string GETMethod<T>(string url, string Id, ref T ObjectResult, string Token = "")
156183
{
157184
string ContentResult = string.Empty;
158185
try
159186
{
160187
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Concat(url,"/",Id));
161188
request.ContentType = CONTENTTYPE;
162-
request.Method = "GET";
189+
request.Method = GET_WebMethod;
190+
191+
if (!string.IsNullOrEmpty(Token))
192+
request.Headers.Add(HttpRequestHeader.Authorization, Token);
163193

164194
var httpResponse = (HttpWebResponse)request.GetResponse();
165195
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))

API_Consumer/Procedures/APICaller_GET.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,29 @@ public static void APICaller_GET (SqlString URL)
2424
}
2525
catch (Exception ex)
2626
{
27-
Helper.SendResultValue("Result", ex.Message.ToString());
27+
Helper.SendResultValue("Error", ex.Message.ToString());
28+
}
29+
}
30+
31+
/// <summary>
32+
/// It's a generic procedure used to consume Api throught GET method.
33+
/// Returns the result as a varchar(max). Could be used to return Json.
34+
/// </summary>
35+
/// <param name="URL">Api GET Method</param>
36+
/// <param name="Token">Authorization Token</param>
37+
[Microsoft.SqlServer.Server.SqlProcedure]
38+
public static void APICaller_GET_Auth(SqlString URL, SqlString Token)
39+
{
40+
try
41+
{
42+
string Result = APIConsumer.GETMethod(URL.ToString(), Token.ToString());
43+
44+
Helper.SendResultValue("Result", Result);
45+
46+
}
47+
catch (Exception ex)
48+
{
49+
Helper.SendResultValue("Error", ex.Message.ToString());
2850
}
2951
}
3052
}

API_Consumer/Procedures/APICaller_POST.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,27 @@ public static void APICaller_POST(SqlString URL, SqlString JsonBody)
2828
Helper.SendResultValue("Error", ex.Message.ToString());
2929
}
3030
}
31+
32+
/// <summary>
33+
/// It's a generic procedure used to consume Api throught POST method.
34+
/// It could either returns a result or not.
35+
/// </summary>
36+
/// <param name="URL">Consumer POST Method of Api</param>
37+
/// <param name="Token">Authorization Token</param>
38+
/// <param name="JsonBody">Json to be sent as body</param>
39+
[Microsoft.SqlServer.Server.SqlProcedure]
40+
public static void APICaller_POST_Auth(SqlString URL, SqlString Token, SqlString JsonBody)
41+
{
42+
try
43+
{
44+
string Result = APIConsumer.POSTMethod(URL.ToString(), JsonBody.ToString(), Token.ToString());
45+
46+
Helper.SendResultValue("Result", Result);
47+
48+
}
49+
catch (Exception ex)
50+
{
51+
Helper.SendResultValue("Error", ex.Message.ToString());
52+
}
53+
}
3154
}
1 KB
Binary file not shown.
2 KB
Binary file not shown.

0 commit comments

Comments
 (0)