From 9efe809c1df176d0bf96386bdc4b1d1d44d91199 Mon Sep 17 00:00:00 2001 From: Derek Beattie Date: Mon, 25 Aug 2014 01:08:34 -0700 Subject: [PATCH 1/3] Add file upload --- src/PortableRest/ContentTypes.cs | 4 ++++ src/PortableRest/FileParameter.cs | 34 ++++++++++++++++++++++++++++ src/PortableRest/PortableRest.csproj | 1 + src/PortableRest/RestClient.cs | 27 +++++++++++++++++++++- src/PortableRest/RestRequest.cs | 17 ++++++++++++++ 5 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/PortableRest/FileParameter.cs diff --git a/src/PortableRest/ContentTypes.cs b/src/PortableRest/ContentTypes.cs index d122763..cd7b04f 100644 --- a/src/PortableRest/ContentTypes.cs +++ b/src/PortableRest/ContentTypes.cs @@ -30,5 +30,9 @@ public enum ContentTypes /// /// Xml, + /// + /// + /// + MultipartFormData } } diff --git a/src/PortableRest/FileParameter.cs b/src/PortableRest/FileParameter.cs new file mode 100644 index 0000000..4a5fb5a --- /dev/null +++ b/src/PortableRest/FileParameter.cs @@ -0,0 +1,34 @@ +namespace PortableRest +{ + /// + /// + /// + public class FileParameter + { + /// + /// + /// + public string FileName { get; set; } + /// + /// + /// + public byte[] Data { get; set; } + /// + /// + /// + public string Name { get; set; } + + /// + /// + /// + /// + /// + /// + public FileParameter(string name, byte[] bytes, string fileName) + { + FileName = fileName; + Data = bytes; + Name = name; + } + } +} diff --git a/src/PortableRest/PortableRest.csproj b/src/PortableRest/PortableRest.csproj index 7db4f66..1366d5b 100644 --- a/src/PortableRest/PortableRest.csproj +++ b/src/PortableRest/PortableRest.csproj @@ -82,6 +82,7 @@ + diff --git a/src/PortableRest/RestClient.cs b/src/PortableRest/RestClient.cs index 6652650..4f848f2 100644 --- a/src/PortableRest/RestClient.cs +++ b/src/PortableRest/RestClient.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Net; using System.Net.Http; +using System.Net.Http.Headers; using System.Reflection; using System.Runtime.Serialization; using System.Text; @@ -328,7 +329,31 @@ private static object Transform(XNode node, RestRequest request) if (restRequest.Parameters.Count > 0) { message.Content = new ByteArrayContent(restRequest.Parameters[0].GetEncodedValue() as byte[]); - } + } + else if (restRequest.ContentType == ContentTypes.MultipartFormData) + { + var multipartPartFormDataContent = new MultipartFormDataContent(); + var contentJson = new StringContent(restRequest.GetRequestBody()); + multipartPartFormDataContent.Add(contentJson); + message.Content = contentJson; + + if (restRequest.Files.Any()) + { + foreach (var fileParameter in restRequest.Files) + { + var streamContent = new StreamContent(new MemoryStream(fileParameter.Data, 0, fileParameter.Data.Length)); + streamContent.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse("form-data"); + streamContent.Headers.ContentDisposition.Parameters.Add( + new NameValueHeaderValue("name", string.Format("\"{0}\"", fileParameter.Name))); + streamContent.Headers.ContentDisposition.Parameters.Add( + new NameValueHeaderValue("filename", string.Format("\"{0}\"", fileParameter.FileName))); + + multipartPartFormDataContent.Add(streamContent); + } + + message.Content = multipartPartFormDataContent; + } + } } else { diff --git a/src/PortableRest/RestRequest.cs b/src/PortableRest/RestRequest.cs index bfe5611..858374b 100644 --- a/src/PortableRest/RestRequest.cs +++ b/src/PortableRest/RestRequest.cs @@ -32,6 +32,11 @@ public class RestRequest /// internal List Parameters { get; set; } + /// + /// + /// + internal List Files { get; set; } + #endregion #region Properties @@ -93,6 +98,7 @@ public RestRequest() { UrlSegments = new List(); Parameters = new List(); + Files = new List(); Headers = new Dictionary(); Method = HttpMethod.Get; } @@ -192,6 +198,17 @@ public void AddParameter(string key, object value, ParameterEncoding encoding = Parameters.Add(new EncodedParameter(key, value, encoding)); } + /// + /// Adds the bytes to the Files collection with the specified file name + /// + /// The parameter name to use in the request + /// The file data + /// The file name to use for the uploaded file + /// This request + public void AddFile(string name, byte[] bytes, string fileName) + { + Files.Add(new FileParameter(name, bytes, fileName)); + } /// /// Replaces tokenized segments of the URL with a desired value. From 51e84b28c5e2bacd75babe494baa4ffafcf5e867 Mon Sep 17 00:00:00 2001 From: Jeff Hansen Date: Fri, 31 Oct 2014 14:10:11 +0100 Subject: [PATCH 2/3] Added JsonDeserializerSettings to RestClient - we need this for custom converters. --- src/.nuget/NuGet.Config | 6 + src/.nuget/NuGet.targets | 144 ++++++++++++++++++ .../PortableRest.Tests.csproj | 24 +-- src/PortableRest.Tests/app.config | 2 +- src/PortableRest.Tests/packages.config | 2 +- src/PortableRest.sln | 4 +- src/PortableRest/PortableRest.csproj | 21 ++- src/PortableRest/RestClient.cs | 15 +- src/PortableRest/packages.config | 8 +- 9 files changed, 196 insertions(+), 30 deletions(-) create mode 100644 src/.nuget/NuGet.Config create mode 100644 src/.nuget/NuGet.targets diff --git a/src/.nuget/NuGet.Config b/src/.nuget/NuGet.Config new file mode 100644 index 0000000..67f8ea0 --- /dev/null +++ b/src/.nuget/NuGet.Config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/.nuget/NuGet.targets b/src/.nuget/NuGet.targets new file mode 100644 index 0000000..3f8c37b --- /dev/null +++ b/src/.nuget/NuGet.targets @@ -0,0 +1,144 @@ + + + + $(MSBuildProjectDirectory)\..\ + + + false + + + false + + + true + + + false + + + + + + + + + + + $([System.IO.Path]::Combine($(SolutionDir), ".nuget")) + + + + + $(SolutionDir).nuget + + + + $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName.Replace(' ', '_')).config + $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config + + + + $(MSBuildProjectDirectory)\packages.config + $(PackagesProjectConfig) + + + + + $(NuGetToolsPath)\NuGet.exe + @(PackageSource) + + "$(NuGetExePath)" + mono --runtime=v4.0.30319 "$(NuGetExePath)" + + $(TargetDir.Trim('\\')) + + -RequireConsent + -NonInteractive + + "$(SolutionDir) " + "$(SolutionDir)" + + + $(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir) + $(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols + + + + RestorePackages; + $(BuildDependsOn); + + + + + $(BuildDependsOn); + BuildPackage; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/PortableRest.Tests/PortableRest.Tests.csproj b/src/PortableRest.Tests/PortableRest.Tests.csproj index d7ff358..15867c7 100644 --- a/src/PortableRest.Tests/PortableRest.Tests.csproj +++ b/src/PortableRest.Tests/PortableRest.Tests.csproj @@ -50,12 +50,10 @@ true ..\PortableRest.snk - + ..\packages\AsyncOAuth.0.8.4\lib\AsyncOAuth.dll - - False ..\packages\FluentAssertions.3.0.107\lib\net45\FluentAssertions.dll @@ -77,15 +75,12 @@ ..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.dll - True ..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.dll - True ..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll - True False @@ -97,17 +92,15 @@ - - False - ..\packages\Microsoft.Net.Http.2.2.22\lib\net45\System.Net.Http.Extensions.dll + + ..\packages\Microsoft.Net.Http.2.2.28\lib\net45\System.Net.Http.Extensions.dll False ..\packages\Microsoft.AspNet.WebApi.Client.5.2.0-rc\lib\net45\System.Net.Http.Formatting.dll - - False - ..\packages\Microsoft.Net.Http.2.2.22\lib\net45\System.Net.Http.Primitives.dll + + ..\packages\Microsoft.Net.Http.2.2.28\lib\net45\System.Net.Http.Primitives.dll @@ -194,6 +187,13 @@ + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + diff --git a/src/PortableRest.Tests/app.config b/src/PortableRest.Tests/app.config index b74c831..c736c48 100644 --- a/src/PortableRest.Tests/app.config +++ b/src/PortableRest.Tests/app.config @@ -4,7 +4,7 @@ - + diff --git a/src/PortableRest.Tests/packages.config b/src/PortableRest.Tests/packages.config index 4d58944..bb0fba2 100644 --- a/src/PortableRest.Tests/packages.config +++ b/src/PortableRest.Tests/packages.config @@ -9,7 +9,7 @@ - + diff --git a/src/PortableRest.sln b/src/PortableRest.sln index 4e3c82f..e274f86 100644 --- a/src/PortableRest.sln +++ b/src/PortableRest.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2013 -VisualStudioVersion = 12.0.30501.0 +VisualStudioVersion = 12.0.30723.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PortableRest", "PortableRest\PortableRest.csproj", "{A3546D1A-CE87-49BB-800A-98018C1FCA00}" EndProject @@ -16,7 +16,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{155D3255-6321-4241-8357-162D6A883535}" ProjectSection(SolutionItems) = preProject + .nuget\NuGet.Config = .nuget\NuGet.Config .nuget\NuGet.exe = .nuget\NuGet.exe + .nuget\NuGet.targets = .nuget\NuGet.targets EndProjectSection EndProject Global diff --git a/src/PortableRest/PortableRest.csproj b/src/PortableRest/PortableRest.csproj index 7db4f66..98dc6c1 100644 --- a/src/PortableRest/PortableRest.csproj +++ b/src/PortableRest/PortableRest.csproj @@ -93,11 +93,9 @@ ..\packages\Microsoft.Bcl.Async.1.0.168\lib\portable-net40+sl4+win8+wp71+wpa81\Microsoft.Threading.Tasks.dll - True ..\packages\Microsoft.Bcl.Async.1.0.168\lib\portable-net40+sl4+win8+wp71+wpa81\Microsoft.Threading.Tasks.Extensions.dll - True ..\packages\Newtonsoft.Json.6.0.4\lib\portable-net40+sl5+wp80+win8+wpa81\Newtonsoft.Json.dll @@ -106,13 +104,13 @@ ..\packages\Microsoft.Bcl.1.1.9\lib\portable-net40+sl5+win8+wp8+wpa81\System.IO.dll - ..\packages\Microsoft.Net.Http.2.2.27-beta\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.dll + ..\packages\Microsoft.Net.Http.2.2.28\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.dll - ..\packages\Microsoft.Net.Http.2.2.27-beta\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.Extensions.dll + ..\packages\Microsoft.Net.Http.2.2.28\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.Extensions.dll - ..\packages\Microsoft.Net.Http.2.2.27-beta\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.Primitives.dll + ..\packages\Microsoft.Net.Http.2.2.28\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.Primitives.dll ..\packages\Microsoft.Bcl.1.1.9\lib\portable-net40+sl5+win8+wp8+wpa81\System.Runtime.dll @@ -122,10 +120,17 @@ - + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + - - + +