Skip to content

Commit

Permalink
Merge pull request #3 from dlmelendez/rel/1.9
Browse files Browse the repository at this point in the history
Rel/1.9
  • Loading branch information
dlmelendez committed Apr 7, 2023
2 parents ff662a9 + c333c1d commit 1ee35a8
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 75 deletions.
6 changes: 3 additions & 3 deletions PayPalHttp-Dotnet.Tests/HttpClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public async Task Execute_UsesDefaultUserAgentHeader()
);

var request = new HttpRequest("/", HttpMethod.Get);
var resp = await Client().Execute(request);
_ = await Client().Execute(request);

Assert.Equal("PayPalHttp-Dotnet HTTP/1.1", GetLastRequest().RequestMessage.Headers["User-Agent"]);
}
Expand Down Expand Up @@ -155,7 +155,7 @@ public async Task Execute_doesNotMutateOriginalRequest()

var request = new SimpleRequest();

var response = await Client().Execute(request);
_ = await Client().Execute(request);

var headerCount = 0;
foreach (var header in request.Headers)
Expand All @@ -181,7 +181,7 @@ public async Task AddInjector_usesCustomInjectorsToModifyRequest()

client.AddInjector(new TestInjector());

var response = await client.Execute(request);
_ = await client.Execute(request);
Assert.Equal("Custom Injector", GetLastRequest().RequestMessage.Headers["User-Agent"]);
}

Expand Down
10 changes: 5 additions & 5 deletions PayPalHttp-Dotnet.Tests/PayPalHttp-Dotnet.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
<LangVersion>10.0</LangVersion>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<LangVersion>11.0</LangVersion>
<IsPackable>false</IsPackable>
<Version>1.8.0</Version>
<Version>1.9.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="WireMock.Net" Version="1.5.10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="WireMock.Net" Version="1.5.21" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
Expand Down
22 changes: 5 additions & 17 deletions PayPalHttp-Dotnet/Encoder.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Text.RegularExpressions;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace PayPalHttp
Expand Down Expand Up @@ -52,12 +50,7 @@ public async Task<HttpContent> SerializeRequestAsync(HttpRequest request)

request.ContentType = request.ContentType.ToLower();

ISerializer serializer = GetSerializer(request.ContentType);
if (serializer == null)
{
throw new IOException($"Unable to serialize request with Content-Type {request.ContentType}. Supported encodings are {GetSupportedContentTypes()}");
}

ISerializer serializer = GetSerializer(request.ContentType) ?? throw new IOException($"Unable to serialize request with Content-Type {request.ContentType}. Supported encodings are {GetSupportedContentTypes()}");
var content = await serializer.EncodeAsync(request).ConfigureAwait(false);

if ("gzip".Equals(request.ContentEncoding))
Expand All @@ -77,12 +70,7 @@ public async Task<object> DeserializeResponseAsync(HttpContent content, Type res
}
var contentType = content.Headers.ContentType.ToString();
contentType = contentType.ToLower();
ISerializer serializer = GetSerializer(contentType);
if (serializer == null)
{
throw new IOException($"Unable to deserialize response with Content-Type {contentType}. Supported encodings are {GetSupportedContentTypes()}");
}

ISerializer serializer = GetSerializer(contentType) ?? throw new IOException($"Unable to deserialize response with Content-Type {contentType}. Supported encodings are {GetSupportedContentTypes()}");
var contentEncoding = content.Headers.ContentEncoding.FirstOrDefault();

if ("gzip".Equals(contentEncoding))
Expand Down
11 changes: 10 additions & 1 deletion PayPalHttp-Dotnet/FormEncodedSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@

namespace PayPalHttp
{
public class FormEncodedSerializer: ISerializer
public partial class FormEncodedSerializer: ISerializer
{
private const string RegExPattern = "application/x-www-form-urlencoded";
#if NET7_0_OR_GREATER
private static readonly Regex _pattern = ContentTypeRegEx();
#else
private static readonly Regex _pattern = new(RegExPattern, RegexOptions.Compiled);
#endif

public Task<object> DecodeAsync(HttpContent content, Type responseType)
{
Expand All @@ -37,5 +41,10 @@ public string GetContentTypeRegexPattern()
{
return RegExPattern;
}

#if NET7_0_OR_GREATER
[GeneratedRegex(RegExPattern, RegexOptions.Compiled)]
private static partial Regex ContentTypeRegEx();
#endif
}
}
1 change: 0 additions & 1 deletion PayPalHttp-Dotnet/HttpException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using System.Net.Http;
using System.Net;
using System.Net.Http.Headers;
namespace PayPalHttp
Expand Down
1 change: 0 additions & 1 deletion PayPalHttp-Dotnet/HttpRequest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;

namespace PayPalHttp
{
Expand Down
3 changes: 1 addition & 2 deletions PayPalHttp-Dotnet/HttpResponse.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Net;
using System.Net;
using System.Net.Http.Headers;

namespace PayPalHttp
Expand Down
5 changes: 2 additions & 3 deletions PayPalHttp-Dotnet/IInjector.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;

namespace PayPalHttp
{
public interface IInjector
public interface IInjector
{
Task<T> InjectAsync<T>(T request) where T: HttpRequest;
}
Expand Down
3 changes: 1 addition & 2 deletions PayPalHttp-Dotnet/ISerializer.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PayPalHttp
{
public interface ISerializer
public interface ISerializer
{
string GetContentTypeRegexPattern();
Regex GetContentRegEx();
Expand Down
5 changes: 1 addition & 4 deletions PayPalHttp-Dotnet/JsonPartContent.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace PayPalHttp
{
Expand Down
13 changes: 11 additions & 2 deletions PayPalHttp-Dotnet/JsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using System;
using System.IO;
using System.Net.Http;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PayPalHttp
{
public class JsonSerializer : ISerializer
public partial class JsonSerializer : ISerializer
{
private const string RegExPattern = "application/json";
#if NET7_0_OR_GREATER
private static readonly Regex _pattern = ContextTypeRegEx();
#else
private static readonly Regex _pattern = new(RegExPattern, RegexOptions.Compiled);
#endif

public async Task<object> DecodeAsync(HttpContent content, Type responseType)
{
Expand Down Expand Up @@ -39,5 +43,10 @@ public string GetContentTypeRegexPattern()
{
return RegExPattern;
}

#if NET7_0_OR_GREATER
[GeneratedRegex(RegExPattern, RegexOptions.Compiled)]
private static partial Regex ContextTypeRegEx();
#endif
}
}
62 changes: 32 additions & 30 deletions PayPalHttp-Dotnet/MultipartSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PayPalHttp
{
public class MultipartSerializer : ISerializer
public partial class MultipartSerializer : ISerializer
{
private const string RegExPattern = "^multipart/.*$";
#if NET7_0_OR_GREATER
private static readonly Regex _pattern = ContentTypeRegEx();
#else
private static readonly Regex _pattern = new(RegExPattern, RegexOptions.Compiled);

#endif
public Task<object> DecodeAsync(HttpContent content, Type responseType)
{
throw new IOException($"Unable to deserialize Content-Type: multipart/form-data.");
}

private static string GetMimeMapping(string filename)
{
switch (Path.GetExtension(filename))
return Path.GetExtension(filename) switch
{
case ".jpeg":
return "image/jpeg";
case ".jpg":
return "image/jpeg";
case ".gif":
return "image/gif";
case ".png":
return "image/png";
case ".pdf":
return "application/pdf";
default:
return "application/octet-stream";
}
".jpeg" => "image/jpeg",
".jpg" => "image/jpeg",
".gif" => "image/gif",
".png" => "image/png",
".pdf" => "application/pdf",
_ => "application/octet-stream",
};
}

public async Task<HttpContent> EncodeAsync(HttpRequest request)
Expand All @@ -52,31 +47,33 @@ public async Task<HttpContent> EncodeAsync(HttpRequest request)

foreach (KeyValuePair<string, object> item in body)
{
if (item.Value is FileStream)
if (item.Value is FileStream file)
{
var file = (FileStream)item.Value;
try {
try
{
MemoryStream memoryStream = new();
await file.CopyToAsync(memoryStream).ConfigureAwait(false);
var fileContent = new ByteArrayContent(memoryStream.ToArray());
var fileName = Path.GetFileName(file.Name);
// This is necessary to quote values since the web server is picky; .NET normally does not quote
fileContent.Headers.Add("Content-Disposition", "form-data; name=\"" + (string)item.Key + "\"; filename=\"" + fileName + "\"");
string mimeType = MultipartSerializer.GetMimeMapping(fileName);
fileContent.Headers.Add("Content-Disposition", "form-data; name=\"" + item.Key + "\"; filename=\"" + fileName + "\"");
string mimeType = GetMimeMapping(fileName);
fileContent.Headers.Add("Content-Type", mimeType);

form.Add(fileContent, (string)item.Key);
} finally {
form.Add(fileContent, item.Key);
}
finally
{
file.Dispose();
}
}
else if (item.Value is HttpContent)
else if (item.Value is HttpContent httpContent)
{
form.Add((HttpContent)item.Value, (string)item.Key);
form.Add(httpContent, item.Key);
}
else
{
form.Add(new StringContent((string)item.Value), (string)item.Key);
form.Add(new StringContent((string)item.Value), item.Key);
}
}

Expand All @@ -95,5 +92,10 @@ public string GetContentTypeRegexPattern()
{
return RegExPattern;
}

#if NET7_0_OR_GREATER
[GeneratedRegex(RegExPattern, RegexOptions.Compiled)]
private static partial Regex ContentTypeRegEx();
#endif
}
}
4 changes: 2 additions & 2 deletions PayPalHttp-Dotnet/PayPalHttp-Dotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0;net7.0</TargetFrameworks>
<LangVersion>10.0</LangVersion>
<LangVersion>11.0</LangVersion>
<Title>ElCamino.PayPalHttp</Title>
<AssemblyName>ElCamino.PayPalHttp</AssemblyName>

Expand All @@ -18,7 +18,7 @@
<RepositoryUrl>https://github.com/dlmelendez/paypalhttp_dotnet.git</RepositoryUrl>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>

<Version>1.8.0</Version>
<Version>1.9.0</Version>
<PackageProjectUrl>https://github.com/dlmelendez/paypalhttp_dotnet</PackageProjectUrl>
<!--<DebugType>Full</DebugType>-->
<!-- DebugType Full is needed for test code coverage, but .nuget symbols doesn't like it-->
Expand Down
12 changes: 10 additions & 2 deletions PayPalHttp-Dotnet/TextSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

namespace PayPalHttp
{
public class TextSerializer : ISerializer
public partial class TextSerializer : ISerializer
{
private const string RegExPattern = "^text/.*$";
#if NET7_0_OR_GREATER
private static readonly Regex _pattern = ContentTypeRegEx();
#else
private static readonly Regex _pattern = new(RegExPattern, RegexOptions.Compiled);

#endif
public async Task<object> DecodeAsync(HttpContent content, Type responseType)
{
return await content.ReadAsStringAsync().ConfigureAwait(false);
Expand All @@ -29,5 +32,10 @@ public string GetContentTypeRegexPattern()
{
return RegExPattern;
}

#if NET7_0_OR_GREATER
[GeneratedRegex(RegExPattern, RegexOptions.Compiled)]
private static partial Regex ContentTypeRegEx();
#endif
}
}

0 comments on commit 1ee35a8

Please sign in to comment.