Skip to content

Commit ec58e4c

Browse files
authored
Merge pull request #31 from geral2/release_v2.1
New procedures added and addition of return execution value -1 when Exceptions occurs.
2 parents ada5f6b + caa9bf2 commit ec58e4c

19 files changed

+902
-44
lines changed

.vs/SQL-APIConsumer/v15/.suo

33 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

API_Consumer/Consumers/APIConsumer.cs

Lines changed: 333 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public static class APIConsumer
1919
/// </summary>
2020
private const string CONTENTTYPE = "application/json; charset=utf-8";
2121

22+
/// <summary>
23+
/// Fixed Context type supported.
24+
/// </summary>
25+
private const string CONTENTTYPE_URLENCODED = "application/x-www-form-urlencoded";
26+
2227
/// <summary>
2328
/// Fixed string for POST method
2429
/// </summary>
@@ -29,6 +34,26 @@ public static class APIConsumer
2934
/// </summary>
3035
private const string GET_WebMethod = "GET";
3136

37+
/// <summary>
38+
/// DEFAULT_EXECUTION_RESULT
39+
/// </summary>
40+
public const int DEFAULT_EXECUTION_RESULT = 0;
41+
42+
/// <summary>
43+
/// FAILED_EXECUTION_RESULT
44+
/// </summary>
45+
public const int FAILED_EXECUTION_RESULT = -1;
46+
47+
/// <summary>
48+
/// DEFAULT_COLUMN_RESULT
49+
/// </summary>
50+
public const string DEFAULT_COLUMN_RESULT = "Result";
51+
52+
/// <summary>
53+
/// DEFAULT_COLUMN_RESULT
54+
/// </summary>
55+
public const string DEFAULT_COLUMN_ERROR = "Error";
56+
3257
/// <summary>
3358
/// POST to Resful API sending Json body.
3459
/// </summary>
@@ -135,9 +160,7 @@ public static string POSTMethod(string url, string JsonBody)
135160
{
136161
ContentResult = ex.Message.ToString();
137162
}
138-
139163
}
140-
141164
}
142165
catch (Exception ex)
143166
{
@@ -218,6 +241,199 @@ public static string POSTMethod_Header(string url, string JsonBody = "", string
218241
return ContentResult;
219242
}
220243

244+
/// <summary>
245+
/// POST to Resful API sending Json body.
246+
/// </summary>
247+
/// <param name="url">API URL</param>
248+
/// <param name="JsonBody">Content Application By Default Json</param>
249+
/// <param name="JsonHeaders">Headers added in Json format: Authorization token, user-passwrod, JWT, etc.</param>
250+
/// <returns>String Api result</returns>
251+
public static string POSTMethod_Extended(ref ExtendedResult extResult, string url, string JsonBody = "", string JsonHeaders = "")
252+
{
253+
string ContentResult = string.Empty;
254+
try
255+
{
256+
SetSSL();
257+
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
258+
//request.ContentType = ContentType;
259+
request.Method = POST_WebMethod;
260+
261+
if (!string.IsNullOrEmpty(JsonHeaders))
262+
{
263+
List<Headers> _headers = JsonConvert.DeserializeObject<List<Headers>>(JsonHeaders);
264+
265+
foreach (var Header in _headers)
266+
{
267+
if (!string.IsNullOrEmpty(Header.Name) && !string.IsNullOrEmpty(Header.Value))
268+
{
269+
if (Header.Name.Contains("Content-Type"))
270+
{
271+
request.ContentType = Header.Value;
272+
}
273+
else
274+
{
275+
request.Headers.Add(Header.Name, Header.Value);
276+
}
277+
}
278+
}
279+
}
280+
281+
if (request.ContentType.ToLower() == CONTENTTYPE_URLENCODED.ToLower())
282+
{
283+
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes((!String.IsNullOrEmpty(JsonBody)) ? JsonBody : "");
284+
// Set the ContentLength property of the WebRequest.
285+
request.ContentLength = byteArray.Length;
286+
287+
using (var streamWriter = request.GetRequestStream())
288+
{
289+
streamWriter.Write(byteArray, 0, byteArray.Length);
290+
// Close the Stream object.
291+
streamWriter.Close();
292+
// Get the response.
293+
294+
streamWriter.Flush();
295+
}
296+
}
297+
else
298+
{
299+
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
300+
{
301+
if (!String.IsNullOrEmpty(JsonBody))
302+
streamWriter.Write(JsonBody);
303+
304+
streamWriter.Flush();
305+
}
306+
}
307+
308+
var httpResponse = (HttpWebResponse)request.GetResponse();
309+
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
310+
{
311+
var result = streamReader.ReadToEnd();
312+
ContentResult = result;
313+
314+
extResult.ContentType = httpResponse.ContentType;
315+
extResult.Server = httpResponse.Server;
316+
extResult.Result = ContentResult;
317+
extResult.StatusCode = httpResponse.StatusCode.ToString();
318+
extResult.StatusDescription = httpResponse.StatusDescription;
319+
320+
for (int i = 0; i < httpResponse.Headers.Count; ++i)
321+
{
322+
extResult.headers.Add(
323+
new Headers()
324+
{
325+
Name = httpResponse.Headers.Keys[i],
326+
Value = httpResponse.Headers[i]
327+
}
328+
);
329+
}
330+
}
331+
}
332+
catch (WebException ex)
333+
{
334+
using (var stream = ex.Response?.GetResponseStream())
335+
{
336+
if (stream != null)
337+
{
338+
using (var reader = new StreamReader(stream))
339+
{
340+
var result = reader.ReadToEnd();
341+
ContentResult = result;
342+
}
343+
}
344+
else
345+
{
346+
ContentResult = ex.Message.ToString();
347+
}
348+
}
349+
}
350+
catch (Exception ex)
351+
{
352+
ContentResult = ex.Message.ToString();
353+
throw ex;
354+
}
355+
356+
return ContentResult;
357+
}
358+
359+
360+
/// <summary>
361+
/// POST to Resful API sending Json body.
362+
/// </summary>
363+
/// <param name="url">API URL</param>
364+
/// <param name="JsonBody">Content Application By Default Json</param>
365+
/// <param name="JsonHeaders">Headers added in Json format: Authorization token, user-passwrod, JWT, etc.</param>
366+
/// <returns>String Api result</returns>
367+
public static string POSTMethod_urlencoded(string url, string JsonBody, string JsonHeaders = "")
368+
{
369+
string ContentResult = string.Empty;
370+
try
371+
{
372+
SetSSL();
373+
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
374+
request.ContentType = CONTENTTYPE_URLENCODED;
375+
request.Method = POST_WebMethod;
376+
377+
if (!string.IsNullOrEmpty(JsonHeaders))
378+
{
379+
List<Headers> _headers = JsonConvert.DeserializeObject<List<Headers>>(JsonHeaders);
380+
381+
foreach (var Header in _headers)
382+
{
383+
if (!string.IsNullOrEmpty(Header.Name) && !string.IsNullOrEmpty(Header.Value))
384+
request.Headers.Add(Header.Name, Header.Value);
385+
}
386+
}
387+
388+
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes((!String.IsNullOrEmpty(JsonBody)) ? JsonBody : "");
389+
// Set the ContentLength property of the WebRequest.
390+
request.ContentLength = byteArray.Length;
391+
392+
using (var streamWriter = request.GetRequestStream())
393+
{
394+
streamWriter.Write(byteArray, 0, byteArray.Length);
395+
// Close the Stream object.
396+
streamWriter.Close();
397+
// Get the response.
398+
399+
400+
streamWriter.Flush();
401+
}
402+
403+
var httpResponse = (HttpWebResponse)request.GetResponse();
404+
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
405+
{
406+
var result = streamReader.ReadToEnd();
407+
ContentResult = result;
408+
}
409+
}
410+
catch (WebException ex)
411+
{
412+
using (var stream = ex.Response?.GetResponseStream())
413+
{
414+
if (stream != null)
415+
{
416+
using (var reader = new StreamReader(stream))
417+
{
418+
var result = reader.ReadToEnd();
419+
ContentResult = result;
420+
}
421+
}
422+
else
423+
{
424+
ContentResult = ex.Message.ToString();
425+
}
426+
}
427+
}
428+
catch (Exception ex)
429+
{
430+
ContentResult = ex.Message.ToString();
431+
throw ex;
432+
}
433+
434+
return ContentResult;
435+
}
436+
221437
/// <summary>
222438
/// Request GET Method to the URL API provided.
223439
/// </summary>
@@ -562,6 +778,121 @@ public static string GETMethod<T>(string url, string Id, ref T ObjectResult, str
562778

563779
return ContentResult;
564780
}
781+
782+
/// <summary>
783+
/// Request GET Method to the URL API provided.
784+
/// </summary>
785+
/// <param name="url">API URL</param>
786+
/// <param name="Authorization">Header Authorization</param>
787+
/// <returns>String Api result</returns>
788+
public static string GETMethod_Extended(ref ExtendedResult extResult, string url, string JsonBody = "", string Headers = "" )
789+
{
790+
string ContentResult = string.Empty;
791+
try
792+
{
793+
SetSSL();
794+
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
795+
//request.ContentType = ContentType;
796+
request.Method = GET_WebMethod;
797+
798+
if (!string.IsNullOrEmpty(Headers))
799+
{
800+
List<Headers> _headers = JsonConvert.DeserializeObject<List<Headers>>(Headers);
801+
802+
foreach (var Header in _headers)
803+
{
804+
if (!string.IsNullOrEmpty(Header.Name) && !string.IsNullOrEmpty(Header.Value))
805+
{
806+
if (Header.Name.Contains("Content-Type"))
807+
{
808+
request.ContentType = Header.Value;
809+
}
810+
else
811+
{
812+
request.Headers.Add(Header.Name, Header.Value);
813+
}
814+
}
815+
816+
}
817+
}
818+
819+
if (request.ContentType.ToLower() == CONTENTTYPE_URLENCODED.ToLower())
820+
{
821+
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes((!String.IsNullOrEmpty(JsonBody)) ? JsonBody : "");
822+
// Set the ContentLength property of the WebRequest.
823+
request.ContentLength = byteArray.Length;
824+
825+
using (var streamWriter = request.GetRequestStream())
826+
{
827+
streamWriter.Write(byteArray, 0, byteArray.Length);
828+
// Close the Stream object.
829+
streamWriter.Close();
830+
// Get the response.
831+
832+
streamWriter.Flush();
833+
}
834+
}
835+
else
836+
{
837+
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
838+
{
839+
if (!String.IsNullOrEmpty(JsonBody))
840+
streamWriter.Write(JsonBody);
841+
842+
streamWriter.Flush();
843+
}
844+
}
845+
846+
var httpResponse = (HttpWebResponse)request.GetResponse();
847+
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
848+
{
849+
var result = streamReader.ReadToEnd();
850+
ContentResult = result;
851+
extResult.ContentType = httpResponse.ContentType;
852+
extResult.Server = httpResponse.Server;
853+
extResult.Result = ContentResult;
854+
extResult.StatusCode = httpResponse.StatusCode.ToString();
855+
extResult.StatusDescription = httpResponse.StatusDescription;
856+
857+
for (int i = 0; i < httpResponse.Headers.Count; ++i)
858+
{
859+
extResult.headers.Add(
860+
new Headers()
861+
{
862+
Name = httpResponse.Headers.Keys[i],
863+
Value = httpResponse.Headers[i]
864+
}
865+
);
866+
}
867+
868+
}
869+
}
870+
catch (WebException ex)
871+
{
872+
using (var stream = ex.Response?.GetResponseStream())
873+
{
874+
if (stream != null)
875+
{
876+
using (var reader = new StreamReader(stream))
877+
{
878+
var result = reader.ReadToEnd();
879+
ContentResult = result;
880+
}
881+
}
882+
else
883+
{
884+
ContentResult = ex.Message.ToString();
885+
}
886+
}
887+
}
888+
catch (Exception ex)
889+
{
890+
ContentResult = ex.Message.ToString();
891+
throw ex;
892+
}
893+
894+
return ContentResult;
895+
}
565896

566897
private static void SetSSL()
567898
{

0 commit comments

Comments
 (0)