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: add custom header support #540

Merged
merged 4 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 27 additions & 3 deletions src/Twilio/Http/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public class Request
/// </summary>
public List<KeyValuePair<string, string>> PostParams { get; private set; }

/// <summary>
/// Header params
/// </summary>
public List<KeyValuePair<string, string>> HeaderParams { get; private set; }

/// <summary>
/// Create a new Twilio request
/// </summary>
Expand All @@ -67,6 +72,7 @@ public Request(HttpMethod method, string url)
Uri = new Uri(url);
QueryParams = new List<KeyValuePair<string, string>>();
PostParams = new List<KeyValuePair<string, string>>();
HeaderParams = new List<KeyValuePair<string, string>>();
}

/// <summary>
Expand All @@ -79,14 +85,16 @@ public Request(HttpMethod method, string url)
/// <param name="queryParams">Query parameters</param>
/// <param name="postParams">Post data</param>
/// <param name="edge">Twilio edge</param>
/// <param name="headerParams">Custom header data</param>
public Request(
HttpMethod method,
Domain domain,
string uri,
string region = null,
List<KeyValuePair<string, string>> queryParams = null,
List<KeyValuePair<string, string>> postParams = null,
string edge = null
string edge = null,
List<KeyValuePair<string, string>> headerParams = null
)
{
Method = method;
Expand All @@ -96,6 +104,7 @@ public Request(

QueryParams = queryParams ?? new List<KeyValuePair<string, string>>();
PostParams = postParams ?? new List<KeyValuePair<string, string>>();
HeaderParams = headerParams ?? new List<KeyValuePair<string, string>>();
}

/// <summary>
Expand Down Expand Up @@ -209,6 +218,16 @@ public void AddPostParam(string name, string value)
AddParam(PostParams, name, value);
}

/// <summary>
/// Add a header parameter
/// </summary>
/// <param name="name">name of parameter</param>
/// <param name="value">value of parameter</param>
public void AddHeaderParam(string name, string value)
{
AddParam(HeaderParams, name, value);
}

private static void AddParam(ICollection<KeyValuePair<string, string>> list, string name, string value)
{
list.Add(new KeyValuePair<string, string>(name, value));
Expand Down Expand Up @@ -239,7 +258,11 @@ public override bool Equals(object obj)
return Method.Equals(other.Method) &&
buildUri().Equals(other.buildUri()) &&
QueryParams.All(other.QueryParams.Contains) &&
PostParams.All(other.PostParams.Contains);
other.QueryParams.All(QueryParams.Contains) &&
PostParams.All(other.PostParams.Contains) &&
other.PostParams.All(PostParams.Contains) &&
HeaderParams.All(other.HeaderParams.Contains) &&
other.HeaderParams.All(HeaderParams.Contains);
}

/// <summary>
Expand All @@ -253,7 +276,8 @@ public override int GetHashCode()
return (Method?.GetHashCode() ?? 0) ^
(buildUri()?.GetHashCode() ?? 0) ^
(QueryParams?.GetHashCode() ?? 0) ^
(PostParams?.GetHashCode() ?? 0);
(PostParams?.GetHashCode() ?? 0) ^
(HeaderParams?.GetHashCode() ?? 0);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/Twilio/Http/SystemNetHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ private HttpRequestMessage BuildHttpRequest(Request request)
var libraryVersion = "twilio-csharp/" + AssemblyInfomation.AssemblyInformationalVersion + PlatVersion;
httpRequest.Headers.TryAddWithoutValidation("User-Agent", libraryVersion);

foreach (var header in request.HeaderParams) {
httpRequest.Headers.TryAddWithoutValidation(header.Key, header.Value);
}

return httpRequest;
}
}
Expand Down
54 changes: 54 additions & 0 deletions src/Twilio/Rest/Chat/V2/Service/Channel/MemberOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public class CreateMemberOptions : IOptions<MemberResource>
/// A valid JSON string that contains application-specific data
/// </summary>
public string Attributes { get; set; }
/// <summary>
/// The X-Twilio-Webhook-Enabled HTTP request header
/// </summary>
public MemberResource.WebhookEnabledTypeEnum XTwilioWebhookEnabled { get; set; }

/// <summary>
/// Construct a new CreateMemberOptions
Expand Down Expand Up @@ -151,6 +155,20 @@ public List<KeyValuePair<string, string>> GetParams()

return p;
}

/// <summary>
/// Generate the necessary header parameters
/// </summary>
public List<KeyValuePair<string, string>> GetHeaderParams()
{
var p = new List<KeyValuePair<string, string>>();
if (XTwilioWebhookEnabled != null)
{
p.Add(new KeyValuePair<string, string>("X-Twilio-Webhook-Enabled", XTwilioWebhookEnabled.ToString()));
}

return p;
}
}

/// <summary>
Expand Down Expand Up @@ -220,6 +238,10 @@ public class DeleteMemberOptions : IOptions<MemberResource>
/// The SID of the Member resource to delete
/// </summary>
public string PathSid { get; }
/// <summary>
/// The X-Twilio-Webhook-Enabled HTTP request header
/// </summary>
public MemberResource.WebhookEnabledTypeEnum XTwilioWebhookEnabled { get; set; }

/// <summary>
/// Construct a new DeleteMemberOptions
Expand All @@ -242,6 +264,20 @@ public List<KeyValuePair<string, string>> GetParams()
var p = new List<KeyValuePair<string, string>>();
return p;
}

/// <summary>
/// Generate the necessary header parameters
/// </summary>
public List<KeyValuePair<string, string>> GetHeaderParams()
{
var p = new List<KeyValuePair<string, string>>();
if (XTwilioWebhookEnabled != null)
{
p.Add(new KeyValuePair<string, string>("X-Twilio-Webhook-Enabled", XTwilioWebhookEnabled.ToString()));
}

return p;
}
}

/// <summary>
Expand Down Expand Up @@ -285,6 +321,10 @@ public class UpdateMemberOptions : IOptions<MemberResource>
/// A valid JSON string that contains application-specific data
/// </summary>
public string Attributes { get; set; }
/// <summary>
/// The X-Twilio-Webhook-Enabled HTTP request header
/// </summary>
public MemberResource.WebhookEnabledTypeEnum XTwilioWebhookEnabled { get; set; }

/// <summary>
/// Construct a new UpdateMemberOptions
Expand Down Expand Up @@ -337,6 +377,20 @@ public List<KeyValuePair<string, string>> GetParams()

return p;
}

/// <summary>
/// Generate the necessary header parameters
/// </summary>
public List<KeyValuePair<string, string>> GetHeaderParams()
{
var p = new List<KeyValuePair<string, string>>();
if (XTwilioWebhookEnabled != null)
{
p.Add(new KeyValuePair<string, string>("X-Twilio-Webhook-Enabled", XTwilioWebhookEnabled.ToString()));
}

return p;
}
}

}
33 changes: 24 additions & 9 deletions src/Twilio/Rest/Chat/V2/Service/Channel/MemberResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ private static Request BuildCreateRequest(CreateMemberOptions options, ITwilioRe
HttpMethod.Post,
Rest.Domain.Chat,
"/v2/Services/" + options.PathServiceSid + "/Channels/" + options.PathChannelSid + "/Members",
postParams: options.GetParams()
postParams: options.GetParams(),
headerParams: options.GetHeaderParams()
);
}

Expand Down Expand Up @@ -161,6 +162,7 @@ public static async System.Threading.Tasks.Task<MemberResource> CreateAsync(Crea
/// <param name="dateCreated"> The ISO 8601 date and time in GMT when the resource was created </param>
/// <param name="dateUpdated"> The ISO 8601 date and time in GMT when the resource was updated </param>
/// <param name="attributes"> A valid JSON string that contains application-specific data </param>
/// <param name="xTwilioWebhookEnabled"> The X-Twilio-Webhook-Enabled HTTP request header </param>
/// <param name="client"> Client to make requests to Twilio </param>
/// <returns> A single instance of Member </returns>
public static MemberResource Create(string pathServiceSid,
Expand All @@ -172,9 +174,10 @@ public static MemberResource Create(string pathServiceSid,
DateTime? dateCreated = null,
DateTime? dateUpdated = null,
string attributes = null,
MemberResource.WebhookEnabledTypeEnum xTwilioWebhookEnabled = null,
ITwilioRestClient client = null)
{
var options = new CreateMemberOptions(pathServiceSid, pathChannelSid, identity){RoleSid = roleSid, LastConsumedMessageIndex = lastConsumedMessageIndex, LastConsumptionTimestamp = lastConsumptionTimestamp, DateCreated = dateCreated, DateUpdated = dateUpdated, Attributes = attributes};
var options = new CreateMemberOptions(pathServiceSid, pathChannelSid, identity){RoleSid = roleSid, LastConsumedMessageIndex = lastConsumedMessageIndex, LastConsumptionTimestamp = lastConsumptionTimestamp, DateCreated = dateCreated, DateUpdated = dateUpdated, Attributes = attributes, XTwilioWebhookEnabled = xTwilioWebhookEnabled};
return Create(options, client);
}

Expand All @@ -192,6 +195,7 @@ public static MemberResource Create(string pathServiceSid,
/// <param name="dateCreated"> The ISO 8601 date and time in GMT when the resource was created </param>
/// <param name="dateUpdated"> The ISO 8601 date and time in GMT when the resource was updated </param>
/// <param name="attributes"> A valid JSON string that contains application-specific data </param>
/// <param name="xTwilioWebhookEnabled"> The X-Twilio-Webhook-Enabled HTTP request header </param>
/// <param name="client"> Client to make requests to Twilio </param>
/// <returns> Task that resolves to A single instance of Member </returns>
public static async System.Threading.Tasks.Task<MemberResource> CreateAsync(string pathServiceSid,
Expand All @@ -203,9 +207,10 @@ public static async System.Threading.Tasks.Task<MemberResource> CreateAsync(stri
DateTime? dateCreated = null,
DateTime? dateUpdated = null,
string attributes = null,
MemberResource.WebhookEnabledTypeEnum xTwilioWebhookEnabled = null,
ITwilioRestClient client = null)
{
var options = new CreateMemberOptions(pathServiceSid, pathChannelSid, identity){RoleSid = roleSid, LastConsumedMessageIndex = lastConsumedMessageIndex, LastConsumptionTimestamp = lastConsumptionTimestamp, DateCreated = dateCreated, DateUpdated = dateUpdated, Attributes = attributes};
var options = new CreateMemberOptions(pathServiceSid, pathChannelSid, identity){RoleSid = roleSid, LastConsumedMessageIndex = lastConsumedMessageIndex, LastConsumptionTimestamp = lastConsumptionTimestamp, DateCreated = dateCreated, DateUpdated = dateUpdated, Attributes = attributes, XTwilioWebhookEnabled = xTwilioWebhookEnabled};
return await CreateAsync(options, client);
}
#endif
Expand Down Expand Up @@ -356,7 +361,8 @@ private static Request BuildDeleteRequest(DeleteMemberOptions options, ITwilioRe
HttpMethod.Delete,
Rest.Domain.Chat,
"/v2/Services/" + options.PathServiceSid + "/Channels/" + options.PathChannelSid + "/Members/" + options.PathSid + "",
queryParams: options.GetParams()
queryParams: options.GetParams(),
headerParams: options.GetHeaderParams()
);
}

Expand Down Expand Up @@ -395,14 +401,16 @@ public static async System.Threading.Tasks.Task<bool> DeleteAsync(DeleteMemberOp
/// <param name="pathServiceSid"> The SID of the Service to delete the resource from </param>
/// <param name="pathChannelSid"> The SID of the channel the Member resource to delete belongs to </param>
/// <param name="pathSid"> The SID of the Member resource to delete </param>
/// <param name="xTwilioWebhookEnabled"> The X-Twilio-Webhook-Enabled HTTP request header </param>
/// <param name="client"> Client to make requests to Twilio </param>
/// <returns> A single instance of Member </returns>
public static bool Delete(string pathServiceSid,
string pathChannelSid,
string pathSid,
MemberResource.WebhookEnabledTypeEnum xTwilioWebhookEnabled = null,
ITwilioRestClient client = null)
{
var options = new DeleteMemberOptions(pathServiceSid, pathChannelSid, pathSid);
var options = new DeleteMemberOptions(pathServiceSid, pathChannelSid, pathSid){XTwilioWebhookEnabled = xTwilioWebhookEnabled};
return Delete(options, client);
}

Expand All @@ -413,14 +421,16 @@ public static bool Delete(string pathServiceSid,
/// <param name="pathServiceSid"> The SID of the Service to delete the resource from </param>
/// <param name="pathChannelSid"> The SID of the channel the Member resource to delete belongs to </param>
/// <param name="pathSid"> The SID of the Member resource to delete </param>
/// <param name="xTwilioWebhookEnabled"> The X-Twilio-Webhook-Enabled HTTP request header </param>
/// <param name="client"> Client to make requests to Twilio </param>
/// <returns> Task that resolves to A single instance of Member </returns>
public static async System.Threading.Tasks.Task<bool> DeleteAsync(string pathServiceSid,
string pathChannelSid,
string pathSid,
MemberResource.WebhookEnabledTypeEnum xTwilioWebhookEnabled = null,
ITwilioRestClient client = null)
{
var options = new DeleteMemberOptions(pathServiceSid, pathChannelSid, pathSid);
var options = new DeleteMemberOptions(pathServiceSid, pathChannelSid, pathSid){XTwilioWebhookEnabled = xTwilioWebhookEnabled};
return await DeleteAsync(options, client);
}
#endif
Expand All @@ -431,7 +441,8 @@ private static Request BuildUpdateRequest(UpdateMemberOptions options, ITwilioRe
HttpMethod.Post,
Rest.Domain.Chat,
"/v2/Services/" + options.PathServiceSid + "/Channels/" + options.PathChannelSid + "/Members/" + options.PathSid + "",
postParams: options.GetParams()
postParams: options.GetParams(),
headerParams: options.GetHeaderParams()
);
}

Expand Down Expand Up @@ -478,6 +489,7 @@ public static async System.Threading.Tasks.Task<MemberResource> UpdateAsync(Upda
/// <param name="dateCreated"> The ISO 8601 date and time in GMT when the resource was created </param>
/// <param name="dateUpdated"> The ISO 8601 date and time in GMT when the resource was updated </param>
/// <param name="attributes"> A valid JSON string that contains application-specific data </param>
/// <param name="xTwilioWebhookEnabled"> The X-Twilio-Webhook-Enabled HTTP request header </param>
/// <param name="client"> Client to make requests to Twilio </param>
/// <returns> A single instance of Member </returns>
public static MemberResource Update(string pathServiceSid,
Expand All @@ -489,9 +501,10 @@ public static MemberResource Update(string pathServiceSid,
DateTime? dateCreated = null,
DateTime? dateUpdated = null,
string attributes = null,
MemberResource.WebhookEnabledTypeEnum xTwilioWebhookEnabled = null,
ITwilioRestClient client = null)
{
var options = new UpdateMemberOptions(pathServiceSid, pathChannelSid, pathSid){RoleSid = roleSid, LastConsumedMessageIndex = lastConsumedMessageIndex, LastConsumptionTimestamp = lastConsumptionTimestamp, DateCreated = dateCreated, DateUpdated = dateUpdated, Attributes = attributes};
var options = new UpdateMemberOptions(pathServiceSid, pathChannelSid, pathSid){RoleSid = roleSid, LastConsumedMessageIndex = lastConsumedMessageIndex, LastConsumptionTimestamp = lastConsumptionTimestamp, DateCreated = dateCreated, DateUpdated = dateUpdated, Attributes = attributes, XTwilioWebhookEnabled = xTwilioWebhookEnabled};
return Update(options, client);
}

Expand All @@ -510,6 +523,7 @@ public static MemberResource Update(string pathServiceSid,
/// <param name="dateCreated"> The ISO 8601 date and time in GMT when the resource was created </param>
/// <param name="dateUpdated"> The ISO 8601 date and time in GMT when the resource was updated </param>
/// <param name="attributes"> A valid JSON string that contains application-specific data </param>
/// <param name="xTwilioWebhookEnabled"> The X-Twilio-Webhook-Enabled HTTP request header </param>
/// <param name="client"> Client to make requests to Twilio </param>
/// <returns> Task that resolves to A single instance of Member </returns>
public static async System.Threading.Tasks.Task<MemberResource> UpdateAsync(string pathServiceSid,
Expand All @@ -521,9 +535,10 @@ public static async System.Threading.Tasks.Task<MemberResource> UpdateAsync(stri
DateTime? dateCreated = null,
DateTime? dateUpdated = null,
string attributes = null,
MemberResource.WebhookEnabledTypeEnum xTwilioWebhookEnabled = null,
ITwilioRestClient client = null)
{
var options = new UpdateMemberOptions(pathServiceSid, pathChannelSid, pathSid){RoleSid = roleSid, LastConsumedMessageIndex = lastConsumedMessageIndex, LastConsumptionTimestamp = lastConsumptionTimestamp, DateCreated = dateCreated, DateUpdated = dateUpdated, Attributes = attributes};
var options = new UpdateMemberOptions(pathServiceSid, pathChannelSid, pathSid){RoleSid = roleSid, LastConsumedMessageIndex = lastConsumedMessageIndex, LastConsumptionTimestamp = lastConsumptionTimestamp, DateCreated = dateCreated, DateUpdated = dateUpdated, Attributes = attributes, XTwilioWebhookEnabled = xTwilioWebhookEnabled};
return await UpdateAsync(options, client);
}
#endif
Expand Down
Loading