Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added json request body support in csharp #686

Merged
merged 6 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Twilio/Constant/EnumConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Newtonsoft.Json;
using Twilio.Converters;
using Twilio.Types;

namespace Twilio.Constant
{
public class EnumConstants
{
[JsonConverter(typeof(StringEnumConverter))]
public sealed class ContentTypeEnum : StringEnum
{
private ContentTypeEnum(string value) : base(value) {}
public static implicit operator ContentTypeEnum(string value)
{
return new ContentTypeEnum(value);
}
public static readonly ContentTypeEnum JSON = new ContentTypeEnum("application/json");
public static readonly ContentTypeEnum FORM_URLENCODED = new ContentTypeEnum("application/x-www-form-urlencoded");
}
}
}
11 changes: 11 additions & 0 deletions src/Twilio/Http/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twilio.Constant;
using Twilio.Rest;

#if !NET35
Expand Down Expand Up @@ -65,6 +66,16 @@ public class Request
/// Header params
/// </summary>
public List<KeyValuePair<string, string>> HeaderParams { get; private set; }

/// <summary>
/// Content Type
/// </summary>
public EnumConstants.ContentTypeEnum ContentType { get; set; }

/// <summary>
/// Body
/// </summary>
public string Body { get; set; }

/// <summary>
/// Create a new Twilio request
Expand Down
11 changes: 10 additions & 1 deletion src/Twilio/Http/SystemNetHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Text;
using Twilio.Constant;

namespace Twilio.Http
{
Expand Down Expand Up @@ -61,7 +63,14 @@ public override async Task<Response> MakeRequestAsync(Request request)
var httpRequest = BuildHttpRequest(request);
if (!Equals(request.Method, HttpMethod.Get))
{
httpRequest.Content = new FormUrlEncodedContent(request.PostParams);
if (request.ContentType == null)
request.ContentType = EnumConstants.ContentTypeEnum.FORM_URLENCODED;

if (Equals(request.ContentType, EnumConstants.ContentTypeEnum.JSON))
httpRequest.Content = new StringContent(request.Body, Encoding.UTF8, "application/json");

else if(Equals(request.ContentType, EnumConstants.ContentTypeEnum.FORM_URLENCODED))
httpRequest.Content = new FormUrlEncodedContent(request.PostParams);
}

this.LastRequest = request;
Expand Down
1 change: 1 addition & 0 deletions src/Twilio/Rest/Domain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static implicit operator Domain(string value)
public static readonly Domain Video = new Domain("video");
public static readonly Domain Voice = new Domain("voice");
public static readonly Domain Wireless = new Domain("wireless");
public static readonly Domain PreviewMessaging = new Domain("preview.messaging");
}

}
20 changes: 20 additions & 0 deletions test/Twilio.Test/Constant/EnumConstantsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NUnit.Framework;
using Twilio.Constant;

namespace Twilio.Tests.Constant
{
[TestFixture]
public class EnumConstantsTest
{

[Test]
public void EnumConstantComparison()
{
var jsonContentType = EnumConstants.ContentTypeEnum.JSON;
var formUrlEncodedType = EnumConstants.ContentTypeEnum.FORM_URLENCODED;

Assert.AreEqual("application/json", jsonContentType.ToString());
Assert.AreEqual("application/x-www-form-urlencoded", formUrlEncodedType.ToString());
}
}
}
26 changes: 23 additions & 3 deletions test/Twilio.Test/Http/SystemNetHttpClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using NUnit.Framework;
using Twilio.Constant;
using Twilio.Http;
using HttpMethod = Twilio.Http.HttpMethod;

Expand Down Expand Up @@ -218,6 +219,25 @@ public void TestMakeRequestWithParams()
Assert.IsNotNull(internalRequest.Content);
Assert.IsInstanceOf<FormUrlEncodedContent>(internalRequest.Content);
}

[Test]
public void TestMakeRequestWithJsonBody()
{
this._mockHttp.Respond("https://api.twilio.com/v1/Resource.json", HttpStatusCode.OK);

Request testRequest = new Request(HttpMethod.Post, "https://api.twilio.com/v1/Resource.json");
testRequest.ContentType = EnumConstants.ContentTypeEnum.JSON;
testRequest.Body = "{\"status\":\"awe\"}";

this.TwilioHttpClient.MakeRequest(testRequest);

HttpRequestMessage internalRequest = this._mockHttp.InternalRequest;

Assert.IsNotNull(internalRequest.Content);
Assert.IsInstanceOf<StringContent>(internalRequest.Content);
Assert.AreEqual("application/json", internalRequest.Content.Headers.ContentType.MediaType);
Assert.AreEqual("utf-8", internalRequest.Content.Headers.ContentType.CharSet);
}

[Test]
public void TestMakeRequestAddsHeadersAndUserAgent()
Expand Down Expand Up @@ -252,14 +272,14 @@ public void TestMakeRequestAddUserAgentExtensions()

Request testRequest = new Request(HttpMethod.Get, "https://api.twilio.com/v1/Resource.json");
testRequest.UserAgentExtensions = userAgentExtensions;
testRequest.SetAuth("username", "password");
testRequest.SetAuth("username", "password");

this.TwilioHttpClient.MakeRequest(testRequest);

HttpRequestMessage internalRequest = this._mockHttp.InternalRequest;
string userAgent = internalRequest.Headers.UserAgent.ToString();
string[] actualUserAgent = userAgent.Split(' ');
var actualUserAgentExtensions = actualUserAgent.ToList().GetRange(actualUserAgent.Length - userAgentExtensions.Length, userAgentExtensions.Length);
var actualUserAgentExtensions = actualUserAgent.ToList().GetRange(actualUserAgent.Length - userAgentExtensions.Length, userAgentExtensions.Length);
CollectionAssert.AreEqual(userAgentExtensions, actualUserAgentExtensions);
}
}
Expand Down
Loading