Skip to content

Commit

Permalink
serializer regex refactor, version 1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
dlmelendez committed Nov 20, 2022
1 parent e1f1f4f commit 36b3b7c
Show file tree
Hide file tree
Showing 10 changed files with 560 additions and 74 deletions.
477 changes: 456 additions & 21 deletions .gitignore

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion PayPalHttp-Dotnet.Tests/PayPalHttp-Dotnet.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10.0</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
Expand Down
25 changes: 20 additions & 5 deletions PayPalHttp-Dotnet.sln
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PayPalHttp-Dotnet.Tests", "PayPalHttp-Dotnet.Tests\PayPalHttp-Dotnet.Tests.csproj", "{721A137C-849F-4F38-8469-313FBDACA8A1}"
# Visual Studio Version 17
VisualStudioVersion = 17.4.33110.190
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PayPalHttp-Dotnet.Tests", "PayPalHttp-Dotnet.Tests\PayPalHttp-Dotnet.Tests.csproj", "{721A137C-849F-4F38-8469-313FBDACA8A1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PayPalHttp-Dotnet", "PayPalHttp-Dotnet\PayPalHttp-Dotnet.csproj", "{84E2B926-5A3B-4F95-9B63-291E0240C1B4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PayPalHttp-Dotnet", "PayPalHttp-Dotnet\PayPalHttp-Dotnet.csproj", "{84E2B926-5A3B-4F95-9B63-291E0240C1B4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{721A137C-849F-4F38-8469-313FBDACA8A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{721A137C-849F-4F38-8469-313FBDACA8A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{721A137C-849F-4F38-8469-313FBDACA8A1}.Debug|x64.ActiveCfg = Debug|Any CPU
{721A137C-849F-4F38-8469-313FBDACA8A1}.Debug|x64.Build.0 = Debug|Any CPU
{721A137C-849F-4F38-8469-313FBDACA8A1}.Debug|x86.ActiveCfg = Debug|Any CPU
{721A137C-849F-4F38-8469-313FBDACA8A1}.Debug|x86.Build.0 = Debug|Any CPU
{721A137C-849F-4F38-8469-313FBDACA8A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{721A137C-849F-4F38-8469-313FBDACA8A1}.Release|Any CPU.Build.0 = Release|Any CPU
{721A137C-849F-4F38-8469-313FBDACA8A1}.Release|x64.ActiveCfg = Release|Any CPU
{721A137C-849F-4F38-8469-313FBDACA8A1}.Release|x64.Build.0 = Release|Any CPU
{721A137C-849F-4F38-8469-313FBDACA8A1}.Release|x86.ActiveCfg = Release|Any CPU
{721A137C-849F-4F38-8469-313FBDACA8A1}.Release|x86.Build.0 = Release|Any CPU
{84E2B926-5A3B-4F95-9B63-291E0240C1B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{84E2B926-5A3B-4F95-9B63-291E0240C1B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84E2B926-5A3B-4F95-9B63-291E0240C1B4}.Debug|x64.ActiveCfg = Debug|x64
Expand All @@ -32,4 +41,10 @@ Global
{84E2B926-5A3B-4F95-9B63-291E0240C1B4}.Release|x86.ActiveCfg = Release|x86
{84E2B926-5A3B-4F95-9B63-291E0240C1B4}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A522D1EE-4E5C-4029-836F-2B7DF2026BE7}
EndGlobalSection
EndGlobal
46 changes: 21 additions & 25 deletions PayPalHttp-Dotnet/Encoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,36 @@ namespace PayPalHttp
{
public class Encoder
{
private List<ISerializer> serializers;
private static readonly Dictionary<string, ISerializer> DefaultSerializers = new Dictionary<string, ISerializer>();

private readonly Dictionary<string, ISerializer> _serializerLookup;

static Encoder()
{
RegisterSerializer(new JsonSerializer(), DefaultSerializers);
RegisterSerializer(new TextSerializer(), DefaultSerializers);
RegisterSerializer(new MultipartSerializer(), DefaultSerializers);
RegisterSerializer(new FormEncodedSerializer(), DefaultSerializers);
}

public Encoder()
{
serializers = new List<ISerializer>();
RegisterSerializer(new JsonSerializer());
RegisterSerializer(new TextSerializer());
RegisterSerializer(new MultipartSerializer());
RegisterSerializer(new FormEncodedSerializer());
_serializerLookup = new Dictionary<string, ISerializer>(DefaultSerializers);
}

public void RegisterSerializer(ISerializer serializer)
private static void RegisterSerializer(ISerializer serializer, Dictionary<string, ISerializer> serializerLookup)
{
if (serializer != null)
{
serializers.Add(serializer);
serializerLookup[serializer.GetContentTypeRegexPattern()] = serializer;
}
}

public void RegisterSerializer(ISerializer serializer)
{
RegisterSerializer(serializer, _serializerLookup);
}

public HttpContent SerializeRequest(HttpRequest request)
{
if (request.ContentType == null)
Expand Down Expand Up @@ -85,27 +96,12 @@ public object DeserializeResponse(HttpContent content, Type responseType)

private ISerializer GetSerializer(string contentType)
{
foreach (var serializer in serializers)
{
Regex pattern = new Regex(serializer.GetContentTypeRegexPattern());
if (pattern.Match(contentType).Success)
{
return serializer;
}
}

return null;
return _serializerLookup.Values.FirstOrDefault(f => f.GetContentRegEx().Match(contentType).Success);
}

private string GetSupportedContentTypes()
{
List<string> contentTypes = new List<string>();
foreach (var serializer in this.serializers)
{
contentTypes.Add(serializer.GetContentTypeRegexPattern());
}

return String.Join(", ", contentTypes);
return String.Join(", ", _serializerLookup.Keys);
}

private static byte[] Gzip(string source)
Expand Down
19 changes: 14 additions & 5 deletions PayPalHttp-Dotnet/FormEncodedSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text.RegularExpressions;

namespace PayPalHttp
{
public class FormEncodedSerializer: ISerializer
{
public string GetContentTypeRegexPattern()
{
return "application/x-www-form-urlencoded";
}
private const string RegExPattern = "application/x-www-form-urlencoded";
private static readonly Regex _pattern = new Regex(RegExPattern, RegexOptions.Compiled);

public object Decode(HttpContent content, Type responseType)
{
throw new IOException($"Unable to deserialize Content-Type: {this.GetContentTypeRegexPattern()}.");
throw new IOException($"Unable to deserialize Content-Type: {RegExPattern}.");
}

public HttpContent Encode(HttpRequest request)
Expand All @@ -27,5 +26,15 @@ public HttpContent Encode(HttpRequest request)

return new FormUrlEncodedContent((Dictionary<string, string>)request.Body);
}

public Regex GetContentRegEx()
{
return _pattern;
}

public string GetContentTypeRegexPattern()
{
return RegExPattern;
}
}
}
3 changes: 3 additions & 0 deletions PayPalHttp-Dotnet/ISerializer.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;

namespace PayPalHttp
{
public interface ISerializer
{
string GetContentTypeRegexPattern();
Regex GetContentRegEx();
HttpContent Encode(HttpRequest request);
object Decode(HttpContent content, Type responseType);
}
Expand Down
19 changes: 14 additions & 5 deletions PayPalHttp-Dotnet/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
using System.Net.Http;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text.RegularExpressions;

namespace PayPalHttp
{
public class JsonSerializer : ISerializer
{
public string GetContentTypeRegexPattern()
{
return "application/json";
}
private const string RegExPattern = "application/json";
private static readonly Regex _pattern = new Regex(RegExPattern, RegexOptions.Compiled);

public object Decode(HttpContent content, Type responseType)
{
Expand All @@ -33,9 +32,19 @@ public HttpContent Encode(HttpRequest request)
ms.Position = 0;
using (var sr = new StreamReader(ms))
{
return new StringContent(sr.ReadToEnd(), System.Text.Encoding.UTF8, "application/json");
return new StringContent(sr.ReadToEnd(), System.Text.Encoding.UTF8, RegExPattern);
}
}
}

public Regex GetContentRegEx()
{
return _pattern;
}

public string GetContentTypeRegexPattern()
{
return RegExPattern;
}
}
}
19 changes: 14 additions & 5 deletions PayPalHttp-Dotnet/MultipartSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace PayPalHttp
{
public class MultipartSerializer : ISerializer
{
public string GetContentTypeRegexPattern()
{
return "^multipart/.*$";
}
private const string RegExPattern = "^multipart/.*$";
private static readonly Regex _pattern = new Regex(RegExPattern, RegexOptions.Compiled);

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

private string GetMimeMapping(string filename)
Expand Down Expand Up @@ -85,5 +84,15 @@ public HttpContent Encode(HttpRequest request)
form.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);
return form;
}

public Regex GetContentRegEx()
{
return _pattern;
}

public string GetContentTypeRegexPattern()
{
return RegExPattern;
}
}
}
7 changes: 4 additions & 3 deletions PayPalHttp-Dotnet/PayPalHttp-Dotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Title>PayPalHttp</Title>
<Version>1.0.1</Version>
<Copyright>Copyright PayPal, Inc. 2019</Copyright>
<LangVersion>10.0</LangVersion>
<Title>ElCamino.PayPalHttp</Title>
<Version>1.7.0</Version>
<Copyright>Copyright PayPal, Inc. 2019 / David Melendez 2022</Copyright>
<Description>PayPalHttp is a generic http client designed to be used with code-generated projects.</Description>
<Authors>PayPal</Authors>
<PackageReleaseNotes>First Release</PackageReleaseNotes>
Expand Down
16 changes: 12 additions & 4 deletions PayPalHttp-Dotnet/TextSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
using System;
using System.Net.Http;
using System.Text.RegularExpressions;

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

public object Decode(HttpContent content, Type responseType)
{
return content.ReadAsStringAsync().Result;
}

public string GetContentTypeRegexPattern()
public HttpContent Encode(HttpRequest request)
{
return "^text/.*$";
return new StringContent(request.Body.ToString());
}

public HttpContent Encode(HttpRequest request)
public Regex GetContentRegEx()
{
return new StringContent(request.Body.ToString());
return _pattern;
}

public string GetContentTypeRegexPattern()
{
return RegExPattern;
}
}
}

0 comments on commit 36b3b7c

Please sign in to comment.