Skip to content

Commit

Permalink
[chore] Replace HTTP method, prep for RestSharp drop (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwithan8 authored Apr 24, 2023
1 parent e5c9ebb commit daf142e
Show file tree
Hide file tree
Showing 22 changed files with 147 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Net;
using EasyPost.Models.API;
using EasyPost.Tests._Utilities;
using RestSharp;

namespace EasyPost.Tests.BetaFeaturesTests.ServicesTests
{
Expand All @@ -19,23 +18,23 @@ protected override IEnumerable<TestUtils.MockRequest> MockRequests
return new List<TestUtils.MockRequest>
{
new(
new TestUtils.MockRequestMatchRules(Method.Post, @"^v2\/bank_accounts\/\S*\/charges$"),
new TestUtils.MockRequestMatchRules(Http.Method.Post, @"^v2\/bank_accounts\/\S*\/charges$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK)
),
new(
new TestUtils.MockRequestMatchRules(Method.Post, @"^v2\/credit_cards\/\S*\/charges$"),
new TestUtils.MockRequestMatchRules(Http.Method.Post, @"^v2\/credit_cards\/\S*\/charges$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK)
),
new(
new TestUtils.MockRequestMatchRules(Method.Delete, @"^v2\/bank_accounts\/\S*$"),
new TestUtils.MockRequestMatchRules(Http.Method.Delete, @"^v2\/bank_accounts\/\S*$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK)
),
new(
new TestUtils.MockRequestMatchRules(Method.Delete, @"^v2\/credit_cards\/\S*$"),
new TestUtils.MockRequestMatchRules(Http.Method.Delete, @"^v2\/credit_cards\/\S*$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK)
),
new(
new TestUtils.MockRequestMatchRules(Method.Get, @"^v2\/payment_methods$"),
new TestUtils.MockRequestMatchRules(Http.Method.Get, @"^v2\/payment_methods$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, data: new PaymentMethodsSummary
{
Id = "summary_123",
Expand Down
17 changes: 8 additions & 9 deletions EasyPost.Tests/ServicesTests/BillingServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using EasyPost.Tests._Utilities;
using EasyPost.Tests._Utilities.Attributes;
using EasyPost.Utilities.Internal.Attributes;
using RestSharp;
using Xunit;

namespace EasyPost.Tests.ServicesTests
Expand All @@ -25,23 +24,23 @@ protected override IEnumerable<TestUtils.MockRequest> MockRequests
return new List<TestUtils.MockRequest>
{
new(
new TestUtils.MockRequestMatchRules(Method.Post, @"^v2\/bank_accounts\/\S*\/charges$"),
new TestUtils.MockRequestMatchRules(Http.Method.Post, @"^v2\/bank_accounts\/\S*\/charges$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK)
),
new(
new TestUtils.MockRequestMatchRules(Method.Post, @"^v2\/credit_cards\/\S*\/charges$"),
new TestUtils.MockRequestMatchRules(Http.Method.Post, @"^v2\/credit_cards\/\S*\/charges$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK)
),
new(
new TestUtils.MockRequestMatchRules(Method.Delete, @"^v2\/bank_accounts\/\S*$"),
new TestUtils.MockRequestMatchRules(Http.Method.Delete, @"^v2\/bank_accounts\/\S*$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK)
),
new(
new TestUtils.MockRequestMatchRules(Method.Delete, @"^v2\/credit_cards\/\S*$"),
new TestUtils.MockRequestMatchRules(Http.Method.Delete, @"^v2\/credit_cards\/\S*$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK)
),
new(
new TestUtils.MockRequestMatchRules(Method.Get, @"^v2\/payment_methods$"),
new TestUtils.MockRequestMatchRules(Http.Method.Get, @"^v2\/payment_methods$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, data: new PaymentMethodsSummary
{
Id = "summary_123",
Expand Down Expand Up @@ -111,7 +110,7 @@ public async Task TestRetrievePaymentMethodsSummaryNoId()
UseMockClient(new List<TestUtils.MockRequest>
{
new(
new TestUtils.MockRequestMatchRules(Method.Get, @"^v2\/payment_methods$"),
new TestUtils.MockRequestMatchRules(Http.Method.Get, @"^v2\/payment_methods$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, data: new PaymentMethodsSummary
{
Id = null, // No ID, will throw an error when we try to interact with this summary
Expand Down Expand Up @@ -161,7 +160,7 @@ public async Task TestGetPaymentMethodByPriorityNoPaymentMethod()
UseMockClient(new List<TestUtils.MockRequest>
{
new(
new TestUtils.MockRequestMatchRules(Method.Get, @"^v2\/payment_methods$"),
new TestUtils.MockRequestMatchRules(Http.Method.Get, @"^v2\/payment_methods$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, data: new PaymentMethodsSummary
{
Id = "summary_123",
Expand All @@ -183,7 +182,7 @@ public async Task TestGetPaymentMethodByPriorityPaymentMethodNoId()
UseMockClient(new List<TestUtils.MockRequest>
{
new(
new TestUtils.MockRequestMatchRules(Method.Get, @"^v2\/payment_methods$"),
new TestUtils.MockRequestMatchRules(Http.Method.Get, @"^v2\/payment_methods$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, data: new PaymentMethodsSummary
{
Id = "summary_123",
Expand Down
35 changes: 17 additions & 18 deletions EasyPost.Tests/ServicesTests/PartnerServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using EasyPost.Tests._Utilities;
using EasyPost.Tests._Utilities.Attributes;
using EasyPost.Utilities.Internal.Attributes;
using RestSharp;
using Xunit;

namespace EasyPost.Tests.ServicesTests
Expand Down Expand Up @@ -90,15 +89,15 @@ public async Task TestAddCreditCardToUser()
UseMockClient(new List<TestUtils.MockRequest>
{
new(
new TestUtils.MockRequestMatchRules(Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestMatchRules(Http.Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, content: "{\"public_key\":\"pk_test_12345\"}")
),
new(
new TestUtils.MockRequestMatchRules(Method.Post, @"^https://api.stripe.com/v1/tokens$"),
new TestUtils.MockRequestMatchRules(Http.Method.Post, @"^https://api.stripe.com/v1/tokens$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, content: "{\"id\":\"tok_12345\"}")
),
new(
new TestUtils.MockRequestMatchRules(Method.Post, @"^v2\/credit_cards$"),
new TestUtils.MockRequestMatchRules(Http.Method.Post, @"^v2\/credit_cards$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, data: new PaymentMethod
{
Id = "summary_123",
Expand Down Expand Up @@ -127,15 +126,15 @@ public async Task TestAddCreditCardToUserDifferentPaymentMethodPriorities()
UseMockClient(new List<TestUtils.MockRequest>
{
new(
new TestUtils.MockRequestMatchRules(Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestMatchRules(Http.Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, content: "{\"public_key\":\"pk_test_12345\"}")
),
new(
new TestUtils.MockRequestMatchRules(Method.Post, @"^https://api.stripe.com/v1/tokens$"),
new TestUtils.MockRequestMatchRules(Http.Method.Post, @"^https://api.stripe.com/v1/tokens$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, content: "{\"id\":\"tok_12345\"}")
),
new(
new TestUtils.MockRequestMatchRules(Method.Post, @"^v2\/credit_cards$"),
new TestUtils.MockRequestMatchRules(Http.Method.Post, @"^v2\/credit_cards$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, data: new PaymentMethod
{
Id = "summary_123",
Expand Down Expand Up @@ -170,7 +169,7 @@ public async Task TestAddCreditCardToUserNoPublicKey()
{
{
new(
new TestUtils.MockRequestMatchRules(Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestMatchRules(Http.Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, content: "{\"public_key\":\"\"}")
)
}
Expand All @@ -188,7 +187,7 @@ public async Task TestAddCreditCardToUserBadPublicKeyResponse()
UseMockClient(new List<TestUtils.MockRequest>
{
new(
new TestUtils.MockRequestMatchRules(Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestMatchRules(Http.Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, content: "{\"not_public_key\":\"random\"}")
),
});
Expand All @@ -205,11 +204,11 @@ public async Task TestAddCreditCardToUserNoStripeToken()
UseMockClient(new List<TestUtils.MockRequest>
{
new(
new TestUtils.MockRequestMatchRules(Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestMatchRules(Http.Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, content: "{\"public_key\":\"pk_test_12345\"}")
),
new(
new TestUtils.MockRequestMatchRules(Method.Post, @"^https://api.stripe.com/v1/tokens$"),
new TestUtils.MockRequestMatchRules(Http.Method.Post, @"^https://api.stripe.com/v1/tokens$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, content: "{\"id\":\"\"}")
)
});
Expand All @@ -226,11 +225,11 @@ public async Task TestAddCreditCardToUserStripeCantConnect()
UseMockClient(new List<TestUtils.MockRequest>
{
new(
new TestUtils.MockRequestMatchRules(Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestMatchRules(Http.Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, content: "{\"public_key\":\"pk_test_12345\"}")
),
new(
new TestUtils.MockRequestMatchRules(Method.Post, @"^https://api.stripe.com/v1/tokens$"),
new TestUtils.MockRequestMatchRules(Http.Method.Post, @"^https://api.stripe.com/v1/tokens$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.NotFound, content: "{}")
)
});
Expand All @@ -247,11 +246,11 @@ public async Task TestAddCreditCardToUserStripeBadResponse()
UseMockClient(new List<TestUtils.MockRequest>
{
new(
new TestUtils.MockRequestMatchRules(Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestMatchRules(Http.Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, content: "{\"public_key\":\"pk_test_12345\"}")
),
new(
new TestUtils.MockRequestMatchRules(Method.Post, @"^https://api.stripe.com/v1/tokens$"),
new TestUtils.MockRequestMatchRules(Http.Method.Post, @"^https://api.stripe.com/v1/tokens$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, content: "{\"not_id\":\"random\"}")
)
});
Expand All @@ -268,15 +267,15 @@ public async Task TestAddCreditCardToUserEasyPostApiError()
UseMockClient(new List<TestUtils.MockRequest>
{
new(
new TestUtils.MockRequestMatchRules(Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestMatchRules(Http.Method.Get, @"^v2\/partners\/stripe_public_key$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, content: "{\"public_key\":\"pk_test_12345\"}")
),
new(
new TestUtils.MockRequestMatchRules(Method.Post, @"^https://api.stripe.com/v1/tokens$"),
new TestUtils.MockRequestMatchRules(Http.Method.Post, @"^https://api.stripe.com/v1/tokens$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, content: "{\"id\":\"tok_12345\"}")
),
new(
new TestUtils.MockRequestMatchRules(Method.Post, @"^v2\/credit_cards$"),
new TestUtils.MockRequestMatchRules(Http.Method.Post, @"^v2\/credit_cards$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.NotFound, content: "{}")
)
});
Expand Down
3 changes: 1 addition & 2 deletions EasyPost.Tests/ServicesTests/ServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using EasyPost.Tests._Utilities;
using EasyPost.Tests._Utilities.Attributes;
using EasyPost.Utilities.Internal.Attributes;
using RestSharp;
using Xunit;

namespace EasyPost.Tests.ServicesTests
Expand Down Expand Up @@ -70,7 +69,7 @@ public async Task TestGetNextPageReachEnd()
{
// API call to get the page of addresses will return an empty list with HasMore = false
new(
new TestUtils.MockRequestMatchRules(Method.Get, @"^v2\/addresses$"),
new TestUtils.MockRequestMatchRules(Http.Method.Get, @"^v2\/addresses$"),
new TestUtils.MockRequestResponseInfo(HttpStatusCode.OK, data: new AddressCollection
{
Addresses = new List<Address>(),
Expand Down
8 changes: 4 additions & 4 deletions EasyPost.Tests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ internal Client SetUpTest(string cassetteName, string? overrideApiKey = null)

public class MockRequestMatchRules
{
internal Method Method { get; set; }
internal Http.Method Method { get; set; }

internal string ResourceRegex { get; set; }

public MockRequestMatchRules(Method method, string resourceRegex)
public MockRequestMatchRules(Http.Method method, string resourceRegex)
{
Method = method;
ResourceRegex = resourceRegex;
Expand Down Expand Up @@ -243,7 +243,7 @@ internal override async Task<RestResponse> ExecuteRequest(RestRequest request)
return new RestResponse
{
Content = mockRequest.ResponseInfo.Content,
StatusCode = mockRequest.ResponseInfo.StatusCode
StatusCode = mockRequest.ResponseInfo.StatusCode,
};
}

Expand All @@ -255,7 +255,7 @@ internal MockClient(EasyPostClient client) : base(client.Configuration.ApiKey, c

internal void AddMockRequests(IEnumerable<MockRequest> mockRequests) => _mockRequests.AddRange(mockRequests);

private MockRequest? FindMatchingMockRequest(RestRequest request) => _mockRequests.FirstOrDefault(mock => mock.MatchRules.Method == request.Method && EndpointMatches(request.Resource, mock.MatchRules.ResourceRegex));
private MockRequest? FindMatchingMockRequest(RestRequest request) => _mockRequests.FirstOrDefault(mock => mock.MatchRules.Method.RestSharpMethod == request.Method && EndpointMatches(request.Resource, mock.MatchRules.ResourceRegex));

private static bool EndpointMatches(string endpoint, string pattern)
{
Expand Down
41 changes: 41 additions & 0 deletions EasyPost/Http/Method.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Net.Http;

namespace EasyPost.Http
{
/// <summary>
/// Enum representing the available HTTP methods.
/// </summary>
public class Method
{
internal HttpMethod HttpMethod { get; }

internal RestSharp.Method RestSharpMethod { get; }

/// <summary>
/// HTTP GET method.
/// </summary>
public static readonly Method Get = new Method(HttpMethod.Get, RestSharp.Method.Get);
/// <summary>
/// HTTP POST method.
/// </summary>
public static readonly Method Post = new Method(HttpMethod.Post, RestSharp.Method.Post);
/// <summary>
/// HTTP PUT method.
/// </summary>
public static readonly Method Put = new Method(HttpMethod.Put, RestSharp.Method.Put);
/// <summary>
/// HTTP DELETE method.
/// </summary>
public static readonly Method Delete = new Method(HttpMethod.Delete, RestSharp.Method.Delete);
/// <summary>
/// HTTP PATCH method.
/// </summary>
public static readonly Method Patch = new Method(new HttpMethod("PATCH"), RestSharp.Method.Patch);

private Method(HttpMethod httpMethod, RestSharp.Method restSharpMethod)
{
HttpMethod = httpMethod;
RestSharpMethod = restSharpMethod;
}
}
}
33 changes: 14 additions & 19 deletions EasyPost/Http/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ internal sealed class Request
private readonly Dictionary<string, object> _parameters;
private readonly RestRequest _restRequest;

private readonly Http.Method _method;

// ReSharper disable once SuggestBaseTypeForParameterInConstructor
public Request(string endpoint, Method method, ApiVersion apiVersion, Dictionary<string, object>? parameters = null, string? rootElement = null)
{
endpoint = $"{apiVersion.Value}/{endpoint}";
_method = method;

_restRequest = new RestRequest(endpoint, method);
_restRequest = new RestRequest(endpoint, method.RestSharpMethod);

_parameters = parameters ?? new Dictionary<string, object>();

Expand All @@ -35,25 +38,17 @@ internal void BuildParameters()
return;
}

switch (_restRequest.Method)
var @switch = new SwitchCase
{
case Method.Get:
case Method.Delete:
BuildQueryParameters();
break;
case Method.Post:
case Method.Put:
case Method.Patch:
BuildBodyParameters();
break;
case Method.Head:
case Method.Options:
case Method.Merge:
case Method.Copy:
case Method.Search:
default:
break;
}
// equality of two HttpMethod objects falls back to their inner strings, so we can compare these objects directly
{ Http.Method.Get.HttpMethod, BuildQueryParameters },
{ Http.Method.Delete.HttpMethod, BuildQueryParameters },
{ Http.Method.Post.HttpMethod, BuildBodyParameters },
{ Http.Method.Put.HttpMethod, BuildBodyParameters },
{ Http.Method.Patch.HttpMethod, BuildBodyParameters },
};

@switch.MatchFirst(_method.HttpMethod);
}

/// <summary>
Expand Down
Loading

0 comments on commit daf142e

Please sign in to comment.