Skip to content

Commit d1843e9

Browse files
author
gdiaz
committed
Fix bug #14. Show correct server error.
1 parent 9f249a8 commit d1843e9

21 files changed

+959
-38
lines changed

.vs/SQL-APIConsumer/v15/.suo

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

.vs/slnx.sqlite

16 KB
Binary file not shown.

API_Consumer/Consumers/APIConsumer.cs

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using Newtonsoft.Json;
22
using System;
3+
using System.Collections.Generic;
34
using System.IO;
45
using System.Net;
6+
using System.Runtime.Serialization;
7+
using API_Consumer;
58

69
namespace SQLAPI_Consumer
710
{
@@ -38,6 +41,7 @@ public static string POSTMethod(string url, string JsonBody, string Authorizatio
3841
string ContentResult = string.Empty ;
3942
try
4043
{
44+
SetSSL();
4145
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
4246
request.ContentType = CONTENTTYPE;
4347
request.Method = POST_WebMethod;
@@ -58,6 +62,76 @@ public static string POSTMethod(string url, string JsonBody, string Authorizatio
5862
ContentResult = result;
5963
}
6064
}
65+
catch (WebException ex)
66+
{
67+
using (var stream = ex.Response.GetResponseStream())
68+
using (var reader = new StreamReader(stream))
69+
{
70+
var result = reader.ReadToEnd();
71+
ContentResult = result;
72+
}
73+
}
74+
catch (Exception ex)
75+
{
76+
ContentResult = ex.Message.ToString();
77+
throw ex;
78+
}
79+
80+
return ContentResult;
81+
}
82+
83+
/// <summary>
84+
/// POST to Resful API sending Json body.
85+
/// </summary>
86+
/// <param name="url">API URL</param>
87+
/// <param name="JsonBody">Content Application By Default Json</param>
88+
/// <param name="JsonHeaders">Headers added in Json format: Authorization token, user-passwrod, JWT, etc.</param>
89+
/// <returns>String Api result</returns>
90+
public static string POSTMethod_Header(string url, string JsonBody = "", string JsonHeaders = "")
91+
{
92+
string ContentResult = string.Empty;
93+
try
94+
{
95+
SetSSL();
96+
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
97+
request.ContentType = CONTENTTYPE;
98+
request.Method = POST_WebMethod;
99+
100+
if (!string.IsNullOrEmpty(JsonHeaders))
101+
{
102+
List<Headers> _headers = JsonConvert.DeserializeObject<List<Headers>>(JsonHeaders);
103+
104+
foreach (var Header in _headers)
105+
{
106+
if (!string.IsNullOrEmpty(Header.Name) && !string.IsNullOrEmpty(Header.Value))
107+
request.Headers.Add(Header.Name, Header.Value);
108+
}
109+
}
110+
111+
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
112+
{
113+
if(!String.IsNullOrEmpty(JsonBody))
114+
streamWriter.Write(JsonBody);
115+
116+
streamWriter.Flush();
117+
}
118+
119+
var httpResponse = (HttpWebResponse)request.GetResponse();
120+
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
121+
{
122+
var result = streamReader.ReadToEnd();
123+
ContentResult = result;
124+
}
125+
}
126+
catch (WebException ex)
127+
{
128+
using (var stream = ex.Response.GetResponseStream())
129+
using (var reader = new StreamReader(stream))
130+
{
131+
var result = reader.ReadToEnd();
132+
ContentResult = result;
133+
}
134+
}
61135
catch (Exception ex)
62136
{
63137
ContentResult = ex.Message.ToString();
@@ -78,6 +152,7 @@ public static string GETMethod(string url, string Authorization = "")
78152
string ContentResult = string.Empty;
79153
try
80154
{
155+
SetSSL();
81156
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
82157
request.ContentType = CONTENTTYPE;
83158
request.Method = GET_WebMethod;
@@ -92,6 +167,125 @@ public static string GETMethod(string url, string Authorization = "")
92167
ContentResult = result;
93168
}
94169
}
170+
catch (WebException ex)
171+
{
172+
using (var stream = ex.Response.GetResponseStream())
173+
using (var reader = new StreamReader(stream))
174+
{
175+
var result = reader.ReadToEnd();
176+
ContentResult = result;
177+
}
178+
}
179+
catch (Exception ex)
180+
{
181+
ContentResult = ex.Message.ToString();
182+
throw ex;
183+
}
184+
185+
return ContentResult;
186+
}
187+
188+
/// <summary>
189+
/// Request GET Method to the URL API provided.
190+
/// </summary>
191+
/// <param name="url">API URL</param>
192+
/// <param name="Authorization">Header Authorization</param>
193+
/// <returns>String Api result</returns>
194+
public static string GETMethod_Headers(string url, string Headers)
195+
{
196+
string ContentResult = string.Empty;
197+
try
198+
{
199+
SetSSL();
200+
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
201+
request.ContentType = CONTENTTYPE;
202+
request.Method = GET_WebMethod;
203+
204+
if (!string.IsNullOrEmpty(Headers))
205+
{
206+
List<Headers> _headers = JsonConvert.DeserializeObject<List<Headers>>(Headers);
207+
208+
foreach (var Header in _headers)
209+
{
210+
if (!string.IsNullOrEmpty(Header.Name) && !string.IsNullOrEmpty(Header.Value))
211+
request.Headers.Add(Header.Name, Header.Value);
212+
}
213+
}
214+
215+
var httpResponse = (HttpWebResponse)request.GetResponse();
216+
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
217+
{
218+
var result = streamReader.ReadToEnd();
219+
ContentResult = result;
220+
}
221+
}
222+
catch (WebException ex)
223+
{
224+
using (var stream = ex.Response.GetResponseStream())
225+
using (var reader = new StreamReader(stream))
226+
{
227+
var result = reader.ReadToEnd();
228+
ContentResult = result;
229+
}
230+
}
231+
catch (Exception ex)
232+
{
233+
ContentResult = ex.Message.ToString();
234+
throw ex;
235+
}
236+
237+
return ContentResult;
238+
}
239+
240+
/// <summary>
241+
/// Request GET Method to the URL API provided.
242+
/// </summary>
243+
/// <param name="url">API URL</param>
244+
/// <param name="Authorization">Header Authorization</param>
245+
/// <returns>String Api result</returns>
246+
public static string GETMethod_Headers(string url, string JsonBody = "", string Headers = "")
247+
{
248+
string ContentResult = string.Empty;
249+
try
250+
{
251+
SetSSL();
252+
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
253+
request.ContentType = CONTENTTYPE;
254+
request.Method = GET_WebMethod;
255+
256+
if (!string.IsNullOrEmpty(Headers))
257+
{
258+
List<Headers> _headers = JsonConvert.DeserializeObject<List<Headers>>(Headers);
259+
260+
foreach (var Header in _headers)
261+
{
262+
if (!string.IsNullOrEmpty(Header.Name) && !string.IsNullOrEmpty(Header.Value))
263+
request.Headers.Add(Header.Name, Header.Value);
264+
}
265+
}
266+
267+
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
268+
{
269+
streamWriter.Write(JsonBody);
270+
streamWriter.Flush();
271+
}
272+
273+
var httpResponse = (HttpWebResponse)request.GetResponse();
274+
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
275+
{
276+
var result = streamReader.ReadToEnd();
277+
ContentResult = result;
278+
}
279+
}
280+
catch (WebException ex)
281+
{
282+
using (var stream = ex.Response.GetResponseStream())
283+
using (var reader = new StreamReader(stream))
284+
{
285+
var result = reader.ReadToEnd();
286+
ContentResult = result;
287+
}
288+
}
95289
catch (Exception ex)
96290
{
97291
ContentResult = ex.Message.ToString();
@@ -113,6 +307,7 @@ public static string GETMethod(string url, string Id, string Authorization = "")
113307
string ContentResult = string.Empty;
114308
try
115309
{
310+
SetSSL();
116311
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Concat(url,"/",Id));
117312
request.ContentType = CONTENTTYPE;
118313
request.Method = GET_WebMethod;
@@ -127,6 +322,15 @@ public static string GETMethod(string url, string Id, string Authorization = "")
127322
ContentResult = result;
128323
}
129324
}
325+
catch (WebException ex)
326+
{
327+
using (var stream = ex.Response.GetResponseStream())
328+
using (var reader = new StreamReader(stream))
329+
{
330+
var result = reader.ReadToEnd();
331+
ContentResult = result;
332+
}
333+
}
130334
catch (Exception ex)
131335
{
132336
ContentResult = ex.Message.ToString();
@@ -148,6 +352,7 @@ public static string GETMethod<T>(string url, ref T ObjectResult, string Authori
148352
string ContentResult = string.Empty;
149353
try
150354
{
355+
SetSSL();
151356
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
152357
request.ContentType = CONTENTTYPE;
153358
request.Method = GET_WebMethod;
@@ -163,6 +368,15 @@ public static string GETMethod<T>(string url, ref T ObjectResult, string Authori
163368
ContentResult = result;
164369
}
165370
}
371+
catch (WebException ex)
372+
{
373+
using (var stream = ex.Response.GetResponseStream())
374+
using (var reader = new StreamReader(stream))
375+
{
376+
var result = reader.ReadToEnd();
377+
ContentResult = result;
378+
}
379+
}
166380
catch (Exception ex)
167381
{
168382
ContentResult = ex.Message.ToString();
@@ -184,6 +398,7 @@ public static string GETMethod<T>(string url, string Id, ref T ObjectResult, str
184398
string ContentResult = string.Empty;
185399
try
186400
{
401+
SetSSL();
187402
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Concat(url,"/",Id));
188403
request.ContentType = CONTENTTYPE;
189404
request.Method = GET_WebMethod;
@@ -199,6 +414,15 @@ public static string GETMethod<T>(string url, string Id, ref T ObjectResult, str
199414
ContentResult = result;
200415
}
201416
}
417+
catch (WebException ex)
418+
{
419+
using (var stream = ex.Response.GetResponseStream())
420+
using (var reader = new StreamReader(stream))
421+
{
422+
var result = reader.ReadToEnd();
423+
ContentResult = result;
424+
}
425+
}
202426
catch (Exception ex)
203427
{
204428
ContentResult = ex.Message.ToString();
@@ -207,5 +431,12 @@ public static string GETMethod<T>(string url, string Id, ref T ObjectResult, str
207431

208432
return ContentResult;
209433
}
434+
435+
private static void SetSSL()
436+
{
437+
ServicePointManager.Expect100Continue = true;
438+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
439+
| SecurityProtocolType.Ssl3;
440+
}
210441
}
211442
}

API_Consumer/Consumers/Helper.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using Microsoft.SqlServer.Server;
2+
using System;
23
using System.Collections.Generic;
34
using System.Data;
5+
using System.Security.Cryptography;
6+
using System.Text;
47

58
namespace SQLAPI_Consumer
69
{
@@ -110,5 +113,38 @@ public static void SendEmptyResult(SqlMetaData[] Header)
110113
SqlContext.Pipe.SendResultsEnd();
111114
}
112115
}
116+
117+
private static readonly Encoding SignatureEncoding = Encoding.UTF8;
118+
119+
/// <summary>
120+
/// public method to return that return SHA256
121+
/// </summary>
122+
/// <param name="message">parameters in URL</param>
123+
/// <param name="secret">SK</param>
124+
/// <returns>string SHA256</returns>
125+
public static string CreateSignature(string message, string secret)
126+
{
127+
128+
byte[] keyBytes = SignatureEncoding.GetBytes(secret);
129+
byte[] messageBytes = SignatureEncoding.GetBytes(message);
130+
HMACSHA256 hmacsha256 = new HMACSHA256(keyBytes);
131+
132+
byte[] bytes = hmacsha256.ComputeHash(messageBytes);
133+
134+
return BitConverter.ToString(bytes).Replace("-", "").ToLower();
135+
}
136+
137+
/// <summary>
138+
/// Timestamp for signature
139+
/// </summary>
140+
/// <returns>string</returns>
141+
public static string GetTimestamp()
142+
{
143+
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
144+
var timestamp = (long)(DateTime.Now.ToUniversalTime() - epoch).TotalMilliseconds;
145+
return timestamp.ToString();
146+
//long milliseconds = System.DateTimeOffset.Now.ToUnixTimeMilliseconds();
147+
//return milliseconds.ToString();
148+
}
113149
}
114150
}

API_Consumer/DTO/Header.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace API_Consumer
6+
{
7+
public class Headers
8+
{
9+
public string Name;
10+
public string Value;
11+
}
12+
}

0 commit comments

Comments
 (0)