Skip to content

Commit d07eca8

Browse files
committed
Added POST with retry login in SqlClr asembly
1 parent c3004e2 commit d07eca8

3 files changed

Lines changed: 65 additions & 25 deletions

File tree

samples/features/sql-clr/Curl/Curl.cs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Data.SqlTypes;
44
using System.Net;
5+
using System.Threading;
56

67
/// <summary>
78
/// Provides CURL-like functionalities in T-SQL code.
@@ -13,29 +14,19 @@ public partial class Curl
1314
public static SqlChars Get(SqlChars H, SqlChars url)
1415
{
1516
var client = new WebClient();
16-
if (!H.IsNull)
17-
{
18-
var header = H.ToSqlString().Value;
19-
if (!string.IsNullOrWhiteSpace(header))
20-
client.Headers.Add(header);
21-
}
17+
AddHeader(H, client);
2218
return new SqlChars(
2319
client.DownloadString(
2420
Uri.EscapeUriString(url.ToSqlString().Value)
25-
).ToCharArray());
21+
).ToCharArray());
2622
}
2723

2824
[SqlProcedure]
2925
public static void Post(SqlChars H, SqlChars d, SqlChars url)
3026
{
3127
var client = new WebClient();
32-
if (!H.IsNull)
33-
{
34-
var header = H.ToSqlString().Value;
35-
if (!string.IsNullOrWhiteSpace(header))
36-
client.Headers.Add(header);
37-
}
38-
if(d.IsNull)
28+
AddHeader(H, client);
29+
if (d.IsNull)
3930
throw new ArgumentException("You must specify data that will be sent to the endpoint", "@d");
4031
var response =
4132
client.UploadString(
@@ -44,4 +35,47 @@ public static void Post(SqlChars H, SqlChars d, SqlChars url)
4435
);
4536
SqlContext.Pipe.Send(response);
4637
}
38+
39+
[SqlProcedure]
40+
public static void PostWithRetry(SqlChars H, SqlChars d, SqlChars url)
41+
{
42+
var client = new WebClient();
43+
AddHeader(H, client);
44+
if (d.IsNull)
45+
throw new ArgumentException("You must specify data that will be sent to the endpoint", "@d");
46+
int i = RETRY_COUNT;
47+
string response = "";
48+
do try
49+
{
50+
response =
51+
client.UploadString(
52+
Uri.EscapeUriString(url.ToSqlString().Value),
53+
d.ToSqlString().Value
54+
);
55+
i = -1;
56+
break;
57+
}
58+
catch (Exception ex)
59+
{
60+
SqlContext.Pipe.Send("Error:\t" + ex.Message + ". Waiting " + DELAY_ON_ERROR + "ms.");
61+
i--;
62+
Thread.Sleep(DELAY_ON_ERROR);
63+
}
64+
while (i > 0);
65+
if(i==-1)
66+
SqlContext.Pipe.Send("Request is executed." + response);
67+
}
68+
69+
static readonly int RETRY_COUNT = 3;
70+
static readonly int DELAY_ON_ERROR = 50;
71+
72+
private static void AddHeader(SqlChars H, WebClient client)
73+
{
74+
if (!H.IsNull)
75+
{
76+
var header = H.ToSqlString().Value;
77+
if (!string.IsNullOrWhiteSpace(header))
78+
client.Headers.Add(header);
79+
}
80+
}
4781
};

0 commit comments

Comments
 (0)