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

Allow to upload document file when creating SupportingDocumentResource #573

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions src/Twilio/Http/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using Twilio.Rest;
using Twilio.Types;

#if !NET35
using System.Net;
Expand Down Expand Up @@ -66,6 +67,13 @@ public class Request
/// </summary>
public List<KeyValuePair<string, string>> HeaderParams { get; private set; }

#if !NET35
/// <summary>
/// Files to upload
/// </summary>
public List<KeyValuePair<string, IUploadFile>> Files { get; private set; }
#endif

/// <summary>
/// Create a new Twilio request
/// </summary>
Expand All @@ -78,6 +86,9 @@ public Request(HttpMethod method, string url)
QueryParams = new List<KeyValuePair<string, string>>();
PostParams = new List<KeyValuePair<string, string>>();
HeaderParams = new List<KeyValuePair<string, string>>();
#if !NET35
Files = new List<KeyValuePair<string, IUploadFile>>();
#endif
}

/// <summary>
Expand All @@ -91,6 +102,7 @@ public Request(HttpMethod method, string url)
/// <param name="postParams">Post data</param>
/// <param name="edge">Twilio edge</param>
/// <param name="headerParams">Custom header data</param>
/// <param name="files">List of files to upload</param>
public Request(
HttpMethod method,
Domain domain,
Expand All @@ -100,6 +112,9 @@ public Request(
List<KeyValuePair<string, string>> postParams = null,
string edge = null,
List<KeyValuePair<string, string>> headerParams = null
#if !NET35
, List<KeyValuePair<string, IUploadFile>> files = null
#endif
)
{
Method = method;
Expand All @@ -110,6 +125,9 @@ public Request(
QueryParams = queryParams ?? new List<KeyValuePair<string, string>>();
PostParams = postParams ?? new List<KeyValuePair<string, string>>();
HeaderParams = headerParams ?? new List<KeyValuePair<string, string>>();
#if !NET35
Files = files ?? new List<KeyValuePair<string, IUploadFile>>();
#endif
}

/// <summary>
Expand Down
26 changes: 25 additions & 1 deletion src/Twilio/Http/SystemNetHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,31 @@ 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.Files.Count > 0)
{
var boundary = "---------" + Guid.NewGuid().ToString().ToLower();
var multiPartContent = new MultipartFormDataContent(boundary);
multiPartContent.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data")
{
Parameters = { new NameValueHeaderValue("boundary", boundary) }
};

foreach (var postParam in request.PostParams)
{
multiPartContent.Add(new StringContent(postParam.Value), postParam.Key);
}

foreach (var file in request.Files)
{
multiPartContent.Add(new StreamContent(file.Value.Stream), file.Key, file.Value.FileName);
}

httpRequest.Content = multiPartContent;
}
else
{
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 @@ -33,6 +33,7 @@ public static implicit operator Domain(string value)
public static readonly Domain Monitor = new Domain("monitor");
public static readonly Domain Notify = new Domain("notify");
public static readonly Domain Numbers = new Domain("numbers");
public static readonly Domain NumbersUpload = new Domain("numbers-upload");
public static readonly Domain Preview = new Domain("preview");
public static readonly Domain Pricing = new Domain("pricing");
public static readonly Domain Proxy = new Domain("proxy");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using Twilio.Base;
using Twilio.Types;
using Twilio.Converters;

namespace Twilio.Rest.Numbers.V2.RegulatoryCompliance
Expand All @@ -29,6 +30,10 @@ public class CreateSupportingDocumentOptions : IOptions<SupportingDocumentResour
/// </summary>
public object Attributes { get; set; }

#if !NET35
public IUploadFile File { get; set; }
#endif

/// <summary>
/// Construct a new CreateSupportingDocumentOptions
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ public static implicit operator StatusEnum(string value)

private static Request BuildCreateRequest(CreateSupportingDocumentOptions options, ITwilioRestClient client)
{
#if !NET35
if (options.File != null)
{
return new Request(
HttpMethod.Post,
Rest.Domain.NumbersUpload,
"/v2/RegulatoryCompliance/SupportingDocuments",
postParams: options.GetParams(),
headerParams: null,
files: new List<KeyValuePair<string, IUploadFile>> { new KeyValuePair<string, IUploadFile>("File", options.File) }
);
}
#endif

return new Request(
HttpMethod.Post,
Rest.Domain.Numbers,
Expand All @@ -63,7 +77,7 @@ public static SupportingDocumentResource Create(CreateSupportingDocumentOptions
return FromJson(response.Content);
}

#if !NET35
#if !NET35
/// <summary>
/// Create a new Supporting Document.
/// </summary>
Expand All @@ -77,7 +91,7 @@ public static async System.Threading.Tasks.Task<SupportingDocumentResource> Crea
var response = await client.RequestAsync(BuildCreateRequest(options, client));
return FromJson(response.Content);
}
#endif
#endif

/// <summary>
/// Create a new Supporting Document.
Expand All @@ -96,7 +110,7 @@ public static SupportingDocumentResource Create(string friendlyName,
return Create(options, client);
}

#if !NET35
#if !NET35
/// <summary>
/// Create a new Supporting Document.
/// </summary>
Expand All @@ -113,7 +127,7 @@ public static async System.Threading.Tasks.Task<SupportingDocumentResource> Crea
var options = new CreateSupportingDocumentOptions(friendlyName, type){Attributes = attributes};
return await CreateAsync(options, client);
}
#endif
#endif

private static Request BuildReadRequest(ReadSupportingDocumentOptions options, ITwilioRestClient client)
{
Expand Down Expand Up @@ -142,7 +156,7 @@ public static ResourceSet<SupportingDocumentResource> Read(ReadSupportingDocumen
return new ResourceSet<SupportingDocumentResource>(page, options, client);
}

#if !NET35
#if !NET35
/// <summary>
/// Retrieve a list of all Supporting Document for an account.
/// </summary>
Expand All @@ -158,7 +172,7 @@ public static async System.Threading.Tasks.Task<ResourceSet<SupportingDocumentRe
var page = Page<SupportingDocumentResource>.FromJson("results", response.Content);
return new ResourceSet<SupportingDocumentResource>(page, options, client);
}
#endif
#endif

/// <summary>
/// Retrieve a list of all Supporting Document for an account.
Expand All @@ -175,7 +189,7 @@ public static ResourceSet<SupportingDocumentResource> Read(int? pageSize = null,
return Read(options, client);
}

#if !NET35
#if !NET35
/// <summary>
/// Retrieve a list of all Supporting Document for an account.
/// </summary>
Expand All @@ -190,7 +204,7 @@ public static async System.Threading.Tasks.Task<ResourceSet<SupportingDocumentRe
var options = new ReadSupportingDocumentOptions(){PageSize = pageSize, Limit = limit};
return await ReadAsync(options, client);
}
#endif
#endif

/// <summary>
/// Fetch the target page of records
Expand Down Expand Up @@ -272,7 +286,7 @@ public static SupportingDocumentResource Fetch(FetchSupportingDocumentOptions op
return FromJson(response.Content);
}

#if !NET35
#if !NET35
/// <summary>
/// Fetch specific Supporting Document Instance.
/// </summary>
Expand All @@ -286,7 +300,7 @@ public static async System.Threading.Tasks.Task<SupportingDocumentResource> Fetc
var response = await client.RequestAsync(BuildFetchRequest(options, client));
return FromJson(response.Content);
}
#endif
#endif

/// <summary>
/// Fetch specific Supporting Document Instance.
Expand All @@ -300,7 +314,7 @@ public static SupportingDocumentResource Fetch(string pathSid, ITwilioRestClient
return Fetch(options, client);
}

#if !NET35
#if !NET35
/// <summary>
/// Fetch specific Supporting Document Instance.
/// </summary>
Expand All @@ -313,7 +327,7 @@ public static async System.Threading.Tasks.Task<SupportingDocumentResource> Fetc
var options = new FetchSupportingDocumentOptions(pathSid);
return await FetchAsync(options, client);
}
#endif
#endif

private static Request BuildUpdateRequest(UpdateSupportingDocumentOptions options, ITwilioRestClient client)
{
Expand All @@ -340,7 +354,7 @@ public static SupportingDocumentResource Update(UpdateSupportingDocumentOptions
return FromJson(response.Content);
}

#if !NET35
#if !NET35
/// <summary>
/// Update an existing Supporting Document.
/// </summary>
Expand All @@ -354,7 +368,7 @@ public static async System.Threading.Tasks.Task<SupportingDocumentResource> Upda
var response = await client.RequestAsync(BuildUpdateRequest(options, client));
return FromJson(response.Content);
}
#endif
#endif

/// <summary>
/// Update an existing Supporting Document.
Expand All @@ -373,7 +387,7 @@ public static SupportingDocumentResource Update(string pathSid,
return Update(options, client);
}

#if !NET35
#if !NET35
/// <summary>
/// Update an existing Supporting Document.
/// </summary>
Expand All @@ -390,7 +404,7 @@ public static async System.Threading.Tasks.Task<SupportingDocumentResource> Upda
var options = new UpdateSupportingDocumentOptions(pathSid){FriendlyName = friendlyName, Attributes = attributes};
return await UpdateAsync(options, client);
}
#endif
#endif

private static Request BuildDeleteRequest(DeleteSupportingDocumentOptions options, ITwilioRestClient client)
{
Expand All @@ -416,7 +430,7 @@ public static bool Delete(DeleteSupportingDocumentOptions options, ITwilioRestCl
return response.StatusCode == System.Net.HttpStatusCode.NoContent;
}

#if !NET35
#if !NET35
/// <summary>
/// Delete a specific Supporting Document.
/// </summary>
Expand All @@ -430,7 +444,7 @@ public static async System.Threading.Tasks.Task<bool> DeleteAsync(DeleteSupporti
var response = await client.RequestAsync(BuildDeleteRequest(options, client));
return response.StatusCode == System.Net.HttpStatusCode.NoContent;
}
#endif
#endif

/// <summary>
/// Delete a specific Supporting Document.
Expand All @@ -444,7 +458,7 @@ public static bool Delete(string pathSid, ITwilioRestClient client = null)
return Delete(options, client);
}

#if !NET35
#if !NET35
/// <summary>
/// Delete a specific Supporting Document.
/// </summary>
Expand All @@ -456,7 +470,7 @@ public static async System.Threading.Tasks.Task<bool> DeleteAsync(string pathSid
var options = new DeleteSupportingDocumentOptions(pathSid);
return await DeleteAsync(options, client);
}
#endif
#endif

/// <summary>
/// Converts a JSON string into a SupportingDocumentResource object
Expand Down
24 changes: 24 additions & 0 deletions src/Twilio/Types/UploadFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#if !NET35
using System.IO;

namespace Twilio.Types
{
public interface IUploadFile
{
string FileName { get; }
Stream Stream { get; }
}

public class UploadFile: IUploadFile
{
public UploadFile(string fileName, Stream stream)
{
FileName = fileName;
Stream = stream;
}

public string FileName { get; private set; }
public Stream Stream { get; private set; }
}
}
#endif
Loading