Skip to content

Commit

Permalink
Modernizes library by dropping/adding new versions, refactor structure
Browse files Browse the repository at this point in the history
- Drop Net Framework 4.0 support
- Drop Net Core 2.0 support
- Add Net 5.0 support
- Add Net 6.0 support
- Refactor parent folder structure
- Add test suites for .NET 5.0 and .NET 6.0
- Update GitHub CI action for new versions
- Remove old and temp project folders
- New location expected for .pfx file when building Release
- Fix project configurations for different configuration profiles
- Fix EasyPost.nuspec file for new versions
  • Loading branch information
nwithan8 committed Feb 18, 2022
1 parent 299f00e commit df4a22c
Show file tree
Hide file tree
Showing 189 changed files with 6,538 additions and 1,491 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
run: dotnet-format --check --exclude /
NET_Tests:
# derived from https://dev.to/felipetofoli/github-actions-for-net-full-framework-build-and-test-299h
runs-on: windows-2019
runs-on: windows-2022
strategy:
matrix:
dotnet-version: [ 'Net35', 'Net40', 'NetCore31' ]
dotnet-version: [ 'NetFramework35', 'NetCore31', 'Net50', 'Net60' ]
steps:
- uses: actions/checkout@v2
# Set the project name, based on .NET version currently selected
Expand All @@ -40,7 +40,7 @@ jobs:
run: nuget restore EasyPost.sln
# Build the test project
- name: Build Solution
run: msbuild ${{ steps.test_project.outputs.test_file }}\${{ steps.test_project.outputs.test_file }}.csproj /p:platform="Any CPU" /p:configuration="Test" /p:outputPath="bin/Test" /p:target="Rebuild" -restore
run: msbuild ${{ steps.test_project.outputs.test_file }}\${{ steps.test_project.outputs.test_file }}.csproj /p:platform="Any CPU" /p:configuration="Debug" /p:outputPath="bin/Test" /p:target="Rebuild" -restore
# Run the tests
- name: Run Tests
run: vstest.console.exe ${{ steps.test_project.outputs.test_file }}\bin\Test\${{ steps.test_project.outputs.test_file }}.dll
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
120 changes: 120 additions & 0 deletions EasyPost.Net/Client.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Reflection;
using Newtonsoft.Json;
using RestSharp;

namespace EasyPost
{
public class Client
{
private const int DefaultConnectTimeoutMilliseconds = 30000;
private const int DefaultRequestTimeoutMilliseconds = 60000;

private readonly ClientConfiguration _configuration;

private readonly string _dotNetVersion;
private readonly string _libraryVersion;

private readonly RestClient _restClient;
private int? _connectTimeoutMilliseconds;
private int? _requestTimeoutMilliseconds;

public int ConnectTimeoutMilliseconds
{
get => _connectTimeoutMilliseconds ?? DefaultConnectTimeoutMilliseconds;
set => _connectTimeoutMilliseconds = value;
}

public int RequestTimeoutMilliseconds
{
get => _requestTimeoutMilliseconds ?? DefaultRequestTimeoutMilliseconds;
set => _requestTimeoutMilliseconds = value;
}

private string UserAgent => $"EasyPost/v2 CSharpClient/{_libraryVersion} .NET/{_dotNetVersion}";

/// <summary>
/// Constructor for the EasyPost client.
/// </summary>
/// <param name="clientConfiguration">EasyPost.ClientConfiguration object instance to use to configure this client.</param>
public Client(ClientConfiguration clientConfiguration)
{
ServicePointManager.SecurityProtocol |= Security.GetProtocol();
_configuration = clientConfiguration ?? throw new ArgumentNullException("clientConfiguration");

_restClient = new RestClient(clientConfiguration.ApiBase);
_restClient.Timeout = ConnectTimeoutMilliseconds;

Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo info = FileVersionInfo.GetVersionInfo(assembly.Location);
_libraryVersion = info.FileVersion;

_dotNetVersion = Environment.Version.ToString();
}

/// <summary>
/// Execute a request against the EasyPost API.
/// </summary>
/// <param name="request">EasyPost.Request object instance to execute.</param>
/// <returns>RestSharp.IRestResponse instance.</returns>
internal IRestResponse Execute(Request request) => _restClient.Execute(PrepareRequest(request));

/// <summary>
/// Execute a request against the EasyPost API.
/// </summary>
/// <param name="request">EasyPost.Request object instance to execute.</param>
/// <typeparam name="T">Type of object to deserialize response data into.</typeparam>
/// <returns>An instance of a T type object.</returns>
/// <exception cref="HttpException">An error occurred during the API request.</exception>
internal T Execute<T>(Request request) where T : new()
{
RestResponse<T> response = (RestResponse<T>)_restClient.Execute<T>(PrepareRequest(request));
int statusCode = Convert.ToInt32(response.StatusCode);

if (statusCode < 400)
{
return response.Data;
}

Dictionary<string, Dictionary<string, object>> body;
List<Error> errors;

try
{
body = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, object>>>(response.Content);
errors = JsonConvert.DeserializeObject<List<Error>>(
JsonConvert.SerializeObject(body["error"]["errors"]));
}
catch
{
throw new HttpException(statusCode, "RESPONSE.PARSE_ERROR", response.Content, new List<Error>());
}

throw new HttpException(
statusCode,
(string)body["error"]["code"],
(string)body["error"]["message"],
errors
);
}

/// <summary>
/// Prepare a request for execution by attaching required headers.
/// </summary>
/// <param name="request">EasyPost.Request object instance to prepare.</param>
/// <returns>RestSharp.RestRequest object instance to execute.</returns>
private RestRequest PrepareRequest(Request request)
{
RestRequest restRequest = (RestRequest)request;
restRequest.Timeout = RequestTimeoutMilliseconds;
restRequest.AddHeader("user_agent", UserAgent);
restRequest.AddHeader("authorization", "Bearer " + _configuration.ApiKey);
restRequest.AddHeader("content_type", "application/json");

return restRequest;
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
51 changes: 51 additions & 0 deletions EasyPost.Net/EasyPost.Net.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<OutputType>Library</OutputType>
<RootNamespace>EasyPost</RootNamespace>
<AssemblyName>EasyPost.Net</AssemblyName>
<Configurations>Release;Debug;Test</Configurations>
<Platforms>AnyCPU</Platforms>
<PackageId>EasyPost.Net</PackageId>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>TRACE;DEBUG;NET45</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\lib\net</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>..\lib\net\EasyPost.Net.XML</DocumentationFile>
<NoWarn>1701;1702;1591;1570</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\EasyPost.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<None Include="..\EasyPost.pfx">
<Link>EasyPost.pfx</Link>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2"/>
<PackageReference Include="RestSharp" Version="106.15.0"/>
</ItemGroup>
</Project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 32 additions & 0 deletions EasyPost.Net/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("EasyPost.Net")]
[assembly: AssemblyDescription("EasyPost .NET/.NET Core Shipping API Client Library for .NET https://easypost.com/docs")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("EasyPost.Net")]
[assembly: AssemblyProduct("EasyPost.Net")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Make "private" methods testable.
#if DEBUG
[assembly: InternalsVisibleTo("EasyPost.Net.Tests")]
#else
[assembly:
InternalsVisibleTo(
"EasyPost.Net.Tests, PublicKey=0024000004800000140200000602000000240000525341310010000001000100e9eec615ced88b424c633434317f6afb7728f05dcc0bf40ce65a5b9b3e8e79a43081c4fe9e9a1eec595713637855ad7867d0c3b2aa9fe0756a0ecda9c9428d92fb862d3dd08400fe6ba9d2c54f2eb0aa59beaf581c32564b31288f521780e8f42747434a09f8748e72043eb41dd8ef23454eb1663ba85d24320ae51547a3a31fae62d3df7e6e5d334360acfbc7f296ca0183624b0e938b3c26d6fb38c5c61c3e1c8e907c80a5ee504d8f8999b678a4f67ed1856a865296e52e6849c8ffa86d9080b33a42be88973213b8f837bfcaadf2a09001ad1582f5a3824f594174bc3da410df3e98954320eaba69e81e5357cac146b9678c02ab0c2cc3362593e29845609fff6dbd32624fbe81b74a57dcbd8ca332e3ce0f3de75fe654bf78f33e2b79be6433448bccb202c4cd3f3a24bf35197fbcc64541fa7d1dfcb626f133671ef379c9a4da794d7e37393f30efa8a836bbccb50aa3034bcca18e436e2538fda49e0ab4eaa058a3e11bcb7a359ba89a6dfc0313a7fa04443ec2286f2bd3b57fa0e4616cd27ad116249fd0ccb0182482737b7879b3f5961029ac911f9c9c490dbaa6093546f0653d2bfe08adc029846d260e475fbad7c70aefb6fea5f84c93eb55cb9c285e75a74541447f6c53f79d969d41620da952526d2c6fa787fc19d13ae606c7d4638bba6d64874f000c25dbd600925851bb23838d5b82bfa59d7ce5d6068bb8")]
#endif

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("754ac1cb-281c-4d4c-b242-5fb436ee1832")]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit df4a22c

Please sign in to comment.