Skip to content

Commit

Permalink
Restsharp109 (#89)
Browse files Browse the repository at this point in the history
* it builds... but is likely busted

* add tests

* add changelog

* remove oauth1 implementation for signing
remove signing abstract methods
add test internal access

* add tests

* use web defaults, closer to newtonsoft

* review comments

* review comments 1

* improve test

* 109

* Update CHANGELOG.md
  • Loading branch information
mjkkirschner authored Oct 23, 2023
1 parent 10415a9 commit 56fce43
Show file tree
Hide file tree
Showing 48 changed files with 363 additions and 833 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### version 3.0
* update RestSharp to 109.0.1, this comes with many API breaking changes, some affecting us, see below:
* removal of basic auth - our API no longer makes sense with RestSharp immutable clients.
* switch from newtonsoft.json to system.text.json
* add unit test project
* drop all targets below net6.
* OAuth1 is no longer supported, we no longer sign requests.
* Request base classes are modified to remove support for signing requests.
### version 2.5
* added user/votes request route
* Add net6.0 as a target framework
Expand Down
1 change: 0 additions & 1 deletion pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ soc2:
hidden_email_list: dynamo_ws_access
repository: env.HARMONY_REPO
third_party_lib_paths:
- "third_party"
- "bin\\release"

deployment:
Expand Down
4 changes: 2 additions & 2 deletions src/AssemblyInfoGenerator/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
// to distinguish one build from another. AssemblyFileVersion is specified
// in AssemblyVersionInfo.cs so that it can be easily incremented by the
// automated build process.
[assembly: AssemblyVersion("2.5.0.5065")]
[assembly: AssemblyVersion("3.0.0.2886")]


// By default, the "Product version" shown in the file properties window is
Expand All @@ -55,4 +55,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("2.5.0.5065")]
[assembly: AssemblyFileVersion("3.0.0.2886")]
6 changes: 3 additions & 3 deletions src/AssemblyInfoGenerator/AssemblyInfo.tt
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ using System.Runtime.InteropServices;
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("<#= this.MajorVersion #>.<#= this.MinorVersion #>.<#= this.BuildNumber #>.<#= this.RevisionNumber #>")]
<#+
int MajorVersion = 2;
int MinorVersion = 5;
int MajorVersion = 3;
int MinorVersion = 0;
int BuildNumber = 0;
// The datetime baseline we choose using this algorithm will affect build number and all nuget packages uploaded
// Please only change when major or minor version got incremented
int RevisionNumber = ((int)(DateTime.UtcNow - new DateTime(2022,1,1)).TotalDays)*10+((int)DateTime.UtcNow.Hour)/3;
int RevisionNumber = ((int)(DateTime.UtcNow - new DateTime(2023,1,1)).TotalDays)*10+((int)DateTime.UtcNow.Hour)/3;
#>
16 changes: 11 additions & 5 deletions src/GregClient.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ VisualStudioVersion = 17.0.32014.148
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GregClient", "GregClient\GregClient.csproj", "{644207B4-7E7F-474A-952E-3453960D8A01}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssemblyInfoGenerator", "AssemblyInfoGenerator\AssemblyInfoGenerator.csproj", "{1B9F8D94-D6D5-4151-BF72-0F6C20A85ABC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssemblyInfoGenerator", "AssemblyInfoGenerator\AssemblyInfoGenerator.csproj", "{133FC760-5699-46D9-BEA6-E816B5F01016}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GregClientTests", "GregClientTests\GregClientTests.csproj", "{04331317-6CF9-42EE-B6A8-2B08C608C810}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -17,10 +19,14 @@ Global
{644207B4-7E7F-474A-952E-3453960D8A01}.Debug|Any CPU.Build.0 = Debug|Any CPU
{644207B4-7E7F-474A-952E-3453960D8A01}.Release|Any CPU.ActiveCfg = Release|Any CPU
{644207B4-7E7F-474A-952E-3453960D8A01}.Release|Any CPU.Build.0 = Release|Any CPU
{1B9F8D94-D6D5-4151-BF72-0F6C20A85ABC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1B9F8D94-D6D5-4151-BF72-0F6C20A85ABC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B9F8D94-D6D5-4151-BF72-0F6C20A85ABC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B9F8D94-D6D5-4151-BF72-0F6C20A85ABC}.Release|Any CPU.Build.0 = Release|Any CPU
{133FC760-5699-46D9-BEA6-E816B5F01016}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{133FC760-5699-46D9-BEA6-E816B5F01016}.Debug|Any CPU.Build.0 = Debug|Any CPU
{133FC760-5699-46D9-BEA6-E816B5F01016}.Release|Any CPU.ActiveCfg = Release|Any CPU
{133FC760-5699-46D9-BEA6-E816B5F01016}.Release|Any CPU.Build.0 = Release|Any CPU
{04331317-6CF9-42EE-B6A8-2B08C608C810}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{04331317-6CF9-42EE-B6A8-2B08C608C810}.Debug|Any CPU.Build.0 = Debug|Any CPU
{04331317-6CF9-42EE-B6A8-2B08C608C810}.Release|Any CPU.ActiveCfg = Release|Any CPU
{04331317-6CF9-42EE-B6A8-2B08C608C810}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 0 additions & 3 deletions src/GregClient/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@
<add key="EnableDebugLogs" value="false"/>
<add key="Timeout" value="300"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
</configuration>
48 changes: 0 additions & 48 deletions src/GregClient/AuthProviders/BasicProvider.cs

This file was deleted.

3 changes: 3 additions & 0 deletions src/GregClient/AuthProviders/IAuthProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface IAuthProvider

LoginState LoginState { get; }
string Username { get; }
//TODO ideally would not force the use of restsharp just to set auth.
/// <summary>
/// This method should sign the request and params as OAuth1.
/// OAuth parameters should be added to the query string.
Expand All @@ -28,6 +29,8 @@ public interface IAuthProvider
/// </summary>
public interface IOAuth2AuthProvider : IAuthProvider
{
//TODO ideally would not force the use of restsharp just to set auth.

/// <summary>
/// This method should add the JWT access token to the Authorization header.
/// Package manager expects a header in the form: Authorization: Bearer [accesstoken]
Expand Down
37 changes: 11 additions & 26 deletions src/GregClient/Converters/DependencyConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Greg.Responses;
using Newtonsoft.Json;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Greg.Converters
{
Expand All @@ -9,42 +10,26 @@ namespace Greg.Converters
/// - a string, which is interpreted as the id of the dependency
/// - an object having the expected properties, which is the default behavior
/// </summary>
public class DependencyConverter : JsonConverter
public class DependencyConverter : JsonConverter<Dependency>
{
public override bool CanWrite
{
get
{
return false;
}
}

public override bool CanConvert(Type objectType)
{
return objectType == typeof(Dependency);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.ValueType == typeof(string))
public override Dependency Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
// This is interpreted as the id of the dependency.
Dependency dep = (Dependency)existingValue ?? new Dependency();
dep._id = (string)reader.Value;
var dep =new Dependency();
dep._id = reader.GetString();
return dep;
}
else
{
// Use the default deserialization behavior.
existingValue = existingValue ?? serializer.ContractResolver.ResolveContract(objectType).DefaultCreator();
serializer.Populate(reader, existingValue);
return existingValue;
//do not pass the same options here as this method is called with, that will result in the converter being called again and again.
return JsonSerializer.Deserialize<Dependency>(ref reader);
}
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
public override void Write(Utf8JsonWriter writer, Dependency value, JsonSerializerOptions options)
{
// Not needed as CanWrite is false.
throw new NotImplementedException();
}
}
Expand Down
34 changes: 6 additions & 28 deletions src/GregClient/GregClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Greg.Requests;
using Greg.Responses;
using Greg.Utility;
using RestSharp;
using System;
using System.Net;
Expand All @@ -9,9 +10,7 @@ namespace Greg
public class GregClient : IGregClient
{
private readonly RestClient _client;

public string BaseUrl { get { return _client.BaseUrl.ToString(); } }

public string BaseUrl { get { return _client.Options.BaseUrl.ToString(); } }
public readonly IAuthProvider _authProvider;
public IAuthProvider AuthProvider
{
Expand All @@ -21,9 +20,7 @@ public IAuthProvider AuthProvider
public GregClient(IAuthProvider provider, string packageManagerUrl)
{

#if LT_NET47
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
#else

// https://stackoverflow.com/questions/2819934/detect-windows-version-in-net
// if the current OS is windows 7 or lower
// set TLS to 1.2.
Expand All @@ -32,41 +29,22 @@ public GregClient(IAuthProvider provider, string packageManagerUrl)
{
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
}
#endif
_authProvider = provider;
_client = new RestClient(packageManagerUrl);
}

private IRestResponse ExecuteInternal(Request m)
private RestResponse ExecuteInternal(Request m)
{
var req = new RestRequest(m.Path, m.HttpMethod);

var req = new RestRequest(m.Path, m.HttpMethod.ToRestSharpHTTPMethod());
m.Build(ref req);

if (m.RequiresAuthorization)
{
//oauth2 - we don't need to sign/encode any parameters.
//Also don't need to create a temp request to avoid adsso putting header/body params
//in the query string. Just use original request, and inject access token.
if (AuthProvider is IOAuth2AuthProvider)
{
AuthProvider.SignRequest(ref req, _client);
}
//oauth1
else
{ // Issue: auth api was adding body params to the query string.
// Details: https://jira.autodesk.com/browse/DYN-1795
// Build a subset of the original request, with only specific parameters that we need to authenticate.
// This means headers added in SignRequest will not exist on the request made to DPM.
var reqToSign = new RestRequest(req.Resource, req.Method);
var authParams = m.GetParamsToSign(ref req);
foreach (var par in authParams)
{
reqToSign.AddParameter(par);
}
// All reqToSign.Parameters will be added in the reqToSign.Resource.
AuthProvider.SignRequest(ref reqToSign, _client);
req.Resource = reqToSign.Resource;
}
}

try
Expand Down
14 changes: 2 additions & 12 deletions src/GregClient/GregClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Greg</RootNamespace>
<AssemblyName>Greg</AssemblyName>
<TargetFrameworks>net452;net48;net5.0;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0</TargetFrameworks>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
Expand Down Expand Up @@ -41,15 +41,8 @@
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' OR '$(TargetFramework)' == 'net48'">
<Reference Include="System.Configuration" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Web" />
</ItemGroup>
<ItemGroup Label="Common package dependencies">
<PackageReference Include="RestSharp" Version="106.12.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="RestSharp" Version="109.0.1" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\CHANGELOG.md">
Expand All @@ -71,9 +64,6 @@
<IncludeAssets>none</IncludeAssets>
</ProjectReference>
</ItemGroup>
<ItemGroup Label="Compile time reference assemblies">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2" PrivateAssets="All" />
</ItemGroup>
<Target Name="ReadPackageVersionFromOutputAssemblySingleTfm" Returns="@(PackAssembly)" Condition="'$(IsCrossTargetingBuild)' != 'true'">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="PackAssembly" />
Expand Down
Loading

0 comments on commit 56fce43

Please sign in to comment.