Skip to content

Commit c50283c

Browse files
committed
Pull request #1: Support Application JSON content for POST API
Merge in SDK/csharp_telesign from feature/PD-91980 to master * commit '60ebd8519b6bbe214b7a1ba6d64ac74a56b3dab9': Fix Version SDK Support Application JSON content for POST API
2 parents f6a7b16 + 60ebd85 commit c50283c

6 files changed

Lines changed: 141 additions & 75 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ bld/
2828
.vs/
2929
# Uncomment if you have tasks that create the project's static files in wwwroot
3030
#wwwroot/
31-
31+
.vscode/
3232
# MSTest test Results
3333
[Tt]est[Rr]esult*/
3434
[Bb]uild[Ll]og.*

RELEASE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2.3.0
2+
- Moved support for application/json content-type from PhoneIdClient class to RestClient class
3+
- Added two testing post method for application json test.
4+
15
2.2.1
26
- Added support for application/json content-type
37

Telesign.Test/RestClientTest.cs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,36 @@ public void TestRestClientPost()
213213
Assert.IsNotNull(this.requestHeaders.Last()["Authorization"]);
214214
}
215215

216+
public void TestRestClientPost2()
217+
{
218+
string testResource = "/test/resource";
219+
Dictionary<string, object> testParams = new Dictionary<string, object>();
220+
testParams.Add("test", "123_\u03ff_test");
221+
testParams.Add("testInteger", 123);
222+
223+
RestClient client = new RestClient(this.customerId,
224+
this.apiKey,
225+
string.Format("http://localhost:{0}", this.mockServer.Port));
226+
227+
client.Post(testResource, testParams);
228+
229+
Assert.AreEqual("POST", this.requests.Last().HttpMethod, "method is not as expected");
230+
Assert.AreEqual("/test/resource", this.requests.Last().RawUrl, "path is not as expected");
231+
232+
Assert.AreEqual("{\"test\":\"123_Ͽ_test\",\"testInteger\":123}", this.requestBodies.Last(), "body is not as expected");
233+
234+
Assert.AreEqual("application/json", this.requestHeaders.Last()["Content-Type"], "Content-Type header is not as expected");
235+
Assert.AreEqual("HMAC-SHA256", this.requestHeaders.Last()["x-ts-auth-method"], "x-ts-auth-method header is not as expected");
236+
237+
Guid dummyGuid;
238+
Assert.IsTrue(Guid.TryParse(this.requestHeaders.Last()["x-ts-nonce"], out dummyGuid), "x-ts-nonce header is not a valid UUID");
239+
240+
DateTime dummyDateTime;
241+
Assert.IsTrue(DateTime.TryParse(this.requestHeaders.Last()["Date"], out dummyDateTime), "Date header is not valid rfc2616 format");
242+
243+
Assert.IsNotNull(this.requestHeaders.Last()["Authorization"]);
244+
}
245+
216246
[Test]
217247
public void TestRestClientGet()
218248
{
@@ -302,8 +332,8 @@ public void TestRestClientDelete()
302332

303333
Assert.IsNotNull(this.requestHeaders.Last()["Authorization"]);
304334
}
305-
306-
335+
336+
307337
[Test]
308338
public async Task TestRestClientPostAsync()
309339
{
@@ -334,6 +364,36 @@ public async Task TestRestClientPostAsync()
334364
Assert.IsNotNull(this.requestHeaders.Last()["Authorization"]);
335365
}
336366

367+
public async Task TestRestClientPost2Async()
368+
{
369+
string testResource = "/test/resource";
370+
Dictionary<string, object> testParams = new Dictionary<string, object>();
371+
testParams.Add("test", "123_\u03ff_test");
372+
testParams.Add("testInteger", 123);
373+
374+
RestClient client = new RestClient(this.customerId,
375+
this.apiKey,
376+
string.Format("http://localhost:{0}", this.mockServer.Port));
377+
378+
client.Post(testResource, testParams);
379+
380+
Assert.AreEqual("POST", this.requests.Last().HttpMethod, "method is not as expected");
381+
Assert.AreEqual("/test/resource", this.requests.Last().RawUrl, "path is not as expected");
382+
383+
Assert.AreEqual("{\"test\":\"123_Ͽ_test\",\"testInteger\":123}", this.requestBodies.Last(), "body is not as expected");
384+
385+
Assert.AreEqual("application/json", this.requestHeaders.Last()["Content-Type"], "Content-Type header is not as expected");
386+
Assert.AreEqual("HMAC-SHA256", this.requestHeaders.Last()["x-ts-auth-method"], "x-ts-auth-method header is not as expected");
387+
388+
Guid dummyGuid;
389+
Assert.IsTrue(Guid.TryParse(this.requestHeaders.Last()["x-ts-nonce"], out dummyGuid), "x-ts-nonce header is not a valid UUID");
390+
391+
DateTime dummyDateTime;
392+
Assert.IsTrue(DateTime.TryParse(this.requestHeaders.Last()["Date"], out dummyDateTime), "Date header is not valid rfc2616 format");
393+
394+
Assert.IsNotNull(this.requestHeaders.Last()["Authorization"]);
395+
}
396+
337397
[Test]
338398
public async Task TestRestClientGetAsync()
339399
{

Telesign/PhoneIdClient.cs

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -45,77 +45,6 @@ public PhoneIdClient(string customerId,
4545
proxyPassword)
4646
{ }
4747

48-
/// <summary>
49-
/// Generic TeleSign REST API request handler.
50-
/// </summary>
51-
/// <param name="resource">The partial resource URI to perform the request against.</param>
52-
/// <param name="method">The HTTP method name, as an upper case string.</param>
53-
/// <param name="parameters">Params to perform the request with. With application/json content type, Dictionary with string keys and object values should be used, otherwise string keys and string values</param>
54-
/// <returns></returns>
55-
protected TelesignResponse Execute(string resource, HttpMethod method, Dictionary<string, object> parameters)
56-
{
57-
return ExecuteAsync(resource, method, parameters).Result;
58-
}
59-
60-
protected async Task<TelesignResponse> ExecuteAsync(string resource, HttpMethod method, Dictionary<string, object> parameters)
61-
{
62-
string contentType = "application/json";
63-
HttpRequestMessage request;
64-
string fieldsToSign = null;
65-
if (parameters == null)
66-
parameters = new Dictionary<string, object>();
67-
68-
string resourceUri = string.Format("{0}{1}", this.restEndpoint, resource);
69-
70-
fieldsToSign = JsonConvert.SerializeObject(parameters);
71-
request = new HttpRequestMessage(method, resourceUri);
72-
request.Content = new StringContent(fieldsToSign);
73-
request.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
74-
75-
Dictionary<string, string> headers = RestClient.GenerateTelesignHeaders(
76-
this.customerId,
77-
this.apiKey,
78-
method.ToString().ToUpper(),
79-
resource,
80-
fieldsToSign,
81-
null,
82-
null,
83-
RestClient.UserAgent,
84-
contentType);
85-
86-
foreach (KeyValuePair<string, string> header in headers)
87-
{
88-
// .NET considers content-type header to be part of the content, not the request
89-
// when content-type is set as request header, exception is raised
90-
// proper way to set content-type is through content property of the request object
91-
if (header.Key == "Content-Type")
92-
continue;
93-
request.Headers.Add(header.Key, header.Value);
94-
}
95-
96-
HttpResponseMessage response = await this.httpClient.SendAsync(request).ConfigureAwait(false);
97-
98-
TelesignResponse tsResponse = new TelesignResponse(response, isAsync: true);
99-
await tsResponse.Initialize();
100-
return tsResponse;
101-
}
102-
103-
/// <summary>
104-
/// Generic TeleSign REST API POST handler.
105-
/// </summary>
106-
/// <param name="resource">The partial resource URI to perform the request against.</param>
107-
/// <param name="parameters">Body params to perform the POST request with.</param>
108-
/// <returns>The TelesignResponse for the request.</returns>
109-
public TelesignResponse Post(string resource, Dictionary<string, object> parameters)
110-
{
111-
return Execute(resource, HttpMethod.Post, parameters);
112-
}
113-
114-
public Task<TelesignResponse> PostAsync(string resource, Dictionary<string, object> parameters)
115-
{
116-
return ExecuteAsync(resource, HttpMethod.Post, parameters);
117-
}
118-
11948
/// <summary>
12049
/// The PhoneID API provides a cleansed phone number, phone type, and telecom carrier information to determine the
12150
/// best communication method - SMS or voice.

Telesign/RestClient.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,5 +381,78 @@ private async Task<TelesignResponse> ExecuteAsync(string resource, HttpMethod me
381381
await tsResponse.Initialize();
382382
return tsResponse;
383383
}
384+
385+
/*Overriding Method only work for Json Content type*/
386+
387+
/// <summary>
388+
/// Generic TeleSign REST API request handler.
389+
/// </summary>
390+
/// <param name="resource">The partial resource URI to perform the request against.</param>
391+
/// <param name="method">The HTTP method name, as an upper case string.</param>
392+
/// <param name="parameters">Params to perform the request with. With application/json content type, Dictionary with string keys and object values should be used, otherwise string keys and string values</param>
393+
/// <returns></returns>
394+
protected TelesignResponse Execute(string resource, HttpMethod method, Dictionary<string, object> parameters)
395+
{
396+
return ExecuteAsync(resource, method, parameters).Result;
397+
}
398+
399+
protected async Task<TelesignResponse> ExecuteAsync(string resource, HttpMethod method, Dictionary<string, object> parameters)
400+
{
401+
string contentType = "application/json";
402+
HttpRequestMessage request;
403+
string fieldsToSign = null;
404+
if (parameters == null)
405+
parameters = new Dictionary<string, object>();
406+
407+
string resourceUri = string.Format("{0}{1}", this.restEndpoint, resource);
408+
409+
fieldsToSign = JsonConvert.SerializeObject(parameters);
410+
request = new HttpRequestMessage(method, resourceUri);
411+
request.Content = new StringContent(fieldsToSign);
412+
request.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
413+
414+
Dictionary<string, string> headers = RestClient.GenerateTelesignHeaders(
415+
this.customerId,
416+
this.apiKey,
417+
method.ToString().ToUpper(),
418+
resource,
419+
fieldsToSign,
420+
null,
421+
null,
422+
RestClient.UserAgent,
423+
contentType);
424+
425+
foreach (KeyValuePair<string, string> header in headers)
426+
{
427+
// .NET considers content-type header to be part of the content, not the request
428+
// when content-type is set as request header, exception is raised
429+
// proper way to set content-type is through content property of the request object
430+
if (header.Key == "Content-Type")
431+
continue;
432+
request.Headers.Add(header.Key, header.Value);
433+
}
434+
435+
HttpResponseMessage response = await this.httpClient.SendAsync(request).ConfigureAwait(false);
436+
437+
TelesignResponse tsResponse = new TelesignResponse(response, isAsync: true);
438+
await tsResponse.Initialize();
439+
return tsResponse;
440+
}
441+
442+
/// <summary>
443+
/// Generic TeleSign REST API POST handler.
444+
/// </summary>
445+
/// <param name="resource">The partial resource URI to perform the request against.</param>
446+
/// <param name="parameters">Body params to perform the POST request with.</param>
447+
/// <returns>The TelesignResponse for the request.</returns>
448+
public TelesignResponse Post(string resource, Dictionary<string, object> parameters)
449+
{
450+
return Execute(resource, HttpMethod.Post, parameters);
451+
}
452+
453+
public Task<TelesignResponse> PostAsync(string resource, Dictionary<string, object> parameters)
454+
{
455+
return ExecuteAsync(resource, HttpMethod.Post, parameters);
456+
}
384457
}
385458
}

Telesign/telesign.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package>
33
<metadata>
44
<id>Telesign</id>
5-
<version>2.2.2</version>
5+
<version>2.3.0</version>
66
<title>Telesign</title>
77
<authors>Telesign</authors>
88
<owners>Telesign</owners>

0 commit comments

Comments
 (0)