Skip to content

Commit 8f4563b

Browse files
authored
Merge pull request #431 from EasyPost/metadata
feat: Add carrier metadata endpoint (beta)
2 parents 27f948f + b38e280 commit 8f4563b

File tree

13 files changed

+613
-5
lines changed

13 files changed

+613
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using EasyPost.Models.API.Beta;
4+
using EasyPost.Tests._Utilities;
5+
using EasyPost.Tests._Utilities.Attributes;
6+
using EasyPost.Utilities.Internal.Attributes;
7+
using Xunit;
8+
using CustomAssertions = EasyPost.Tests._Utilities.Assertions.Assert;
9+
10+
namespace EasyPost.Tests.ServicesTests.Beta
11+
{
12+
public class CarrierMetadataServiceTests : UnitTest
13+
{
14+
public CarrierMetadataServiceTests() : base("beta_carrier_metadata")
15+
{
16+
}
17+
18+
#region Tests
19+
20+
#region Test CRUD Operations
21+
22+
/// <summary>
23+
/// Test that we can retrieve all carriers and all metadata from the API when no params are provided.
24+
/// </summary>
25+
[Fact]
26+
[CrudOperations.Read]
27+
[Testing.Function]
28+
public async Task TestRetrieve()
29+
{
30+
UseVCR("retrieve");
31+
32+
List<Carrier> carriers = await Client.Beta.CarrierMetadata.RetrieveCarrierMetadata();
33+
34+
Assert.IsType<List<Carrier>>(carriers);
35+
36+
// Assert we get multiple carriers
37+
CustomAssertions.Any(carriers, carrier => Assert.Equal("usps", carrier.Name));
38+
CustomAssertions.Any(carriers, carrier => Assert.Equal("fedex", carrier.Name));
39+
}
40+
41+
/// <summary>
42+
/// Test that we can retrieve all carriers and all metadata from the API when no params are provided.
43+
/// </summary>
44+
[Fact]
45+
[CrudOperations.Read]
46+
[Testing.Parameters]
47+
public async Task TestRetrieveWithFilters()
48+
{
49+
UseVCR("retrieve_with_filters");
50+
51+
const string carrierName = "usps";
52+
53+
BetaFeatures.Parameters.Beta.CarrierMetadata.Retrieve parameters = new()
54+
{
55+
Carriers = new List<string> { carrierName },
56+
Types = new List<CarrierMetadataType> { CarrierMetadataType.ServiceLevels, CarrierMetadataType.PredefinedPackages },
57+
};
58+
59+
List<Carrier> carriers = await Client.Beta.CarrierMetadata.RetrieveCarrierMetadata(parameters);
60+
61+
// Assert we get the single carrier we asked for
62+
Assert.True(carriers.Count == 1);
63+
Assert.All(carriers, carrier => Assert.Equal(carrierName, carrier.Name));
64+
65+
// Assert we get service levels and predefined packages as we asked for, but not supported features or shipment options
66+
Assert.All(carriers, carrier => Assert.True(carrier.ServiceLevels != null));
67+
Assert.All(carriers, carrier => Assert.True(carrier.PredefinedPackages != null));
68+
Assert.All(carriers, carrier => Assert.True(carrier.SupportedFeatures == null));
69+
Assert.All(carriers, carrier => Assert.True(carrier.ShipmentOptions == null));
70+
}
71+
72+
#endregion
73+
74+
#endregion
75+
}
76+
}

Diff for: EasyPost.Tests/_Utilities/Assertions/AnyException.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Xunit.Sdk;
2+
3+
namespace EasyPost.Tests._Utilities.Assertions
4+
{
5+
/// <summary>
6+
/// Exception thrown when an Any assertion has one or more items fail an assertion.
7+
/// </summary>
8+
public class AnyException : XunitException
9+
{
10+
/// <summary>
11+
/// Creates a new instance of the <see cref="AnyException"/> class.
12+
/// </summary>
13+
public AnyException()
14+
: base("Assert.Any() Failure")
15+
{
16+
}
17+
}
18+
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace EasyPost.Tests._Utilities.Assertions
5+
{
6+
public abstract partial class Assert
7+
{
8+
private static void GuardArgumentNotNull(string argName, object argValue)
9+
{
10+
if (argValue == null)
11+
throw new ArgumentNullException(argName);
12+
}
13+
14+
/// <summary>
15+
/// Verifies that any items in the collection pass when executed against
16+
/// action.
17+
/// </summary>
18+
/// <typeparam name="T">The type of the object to be verified</typeparam>
19+
/// <param name="collection">The collection</param>
20+
/// <param name="action">The action to test each item against</param>
21+
/// <exception cref="AnyException">Thrown when the collection contains no matching element</exception>
22+
public static void Any<T>(IEnumerable<T> collection, Action<T> action)
23+
{
24+
GuardArgumentNotNull(nameof(collection), collection);
25+
GuardArgumentNotNull(nameof(action), action);
26+
27+
Any(collection, (item, index) => action(item));
28+
}
29+
30+
/// <summary>
31+
/// Verifies that any items in the collection pass when executed against
32+
/// action. The item index is provided to the action, in addition to the item.
33+
/// </summary>
34+
/// <typeparam name="T">The type of the object to be verified</typeparam>
35+
/// <param name="collection">The collection</param>
36+
/// <param name="action">The action to test each item against</param>
37+
/// <exception cref="AnyException">Thrown when the collection contains no matching element</exception>
38+
public static void Any<T>(IEnumerable<T> collection, Action<T, int> action)
39+
{
40+
GuardArgumentNotNull(nameof(collection), collection);
41+
GuardArgumentNotNull(nameof(action), action);
42+
43+
int idx = 0;
44+
45+
bool passed = false;
46+
47+
foreach (var item in collection)
48+
{
49+
try
50+
{
51+
action(item, idx);
52+
passed = true;
53+
break; // if we get here, we passed, so we can stop iterating
54+
}
55+
catch (Exception ex)
56+
{
57+
// we don't care about the exception, we just want to keep iterating
58+
}
59+
60+
++idx;
61+
}
62+
63+
if (!passed)
64+
throw new AnyException();
65+
}
66+
}
67+
}

Diff for: EasyPost.Tests/cassettes/net/beta_carrier_metadata/retrieve.json

+52
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: EasyPost.Tests/cassettes/net/beta_carrier_metadata/retrieve_with_filters.json

+52
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: EasyPost.Tests/cassettes/netstandard/beta_carrier_metadata/retrieve.json

+52
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: EasyPost.Tests/cassettes/netstandard/beta_carrier_metadata/retrieve_with_filters.json

+52
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: EasyPost/BetaClient.cs

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class BetaClient : EasyPostClient
2626

2727
public RateService Rate { get; }
2828

29+
public CarrierMetadataService CarrierMetadata { get; }
30+
2931
/// <summary>
3032
/// Initializes a new instance of the <see cref="BetaClient"/> class.
3133
/// Constructor for the EasyPost beta client.
@@ -38,6 +40,7 @@ internal BetaClient(string apiKey, string? baseUrl = null, HttpClient? customHtt
3840
{
3941
Referral = new ReferralService(this);
4042
Rate = new RateService(this);
43+
CarrierMetadata = new CarrierMetadataService(this);
4144
}
4245
}
4346
}

0 commit comments

Comments
 (0)