Skip to content

Commit

Permalink
[chore] Move all instance (model) functions to services (part 4) (#445)
Browse files Browse the repository at this point in the history
- Remove client handoff (objects no longer contain clients)
- Finish removing instance function capabilities
- Remove excessive get, list, update, delete etc. request abstractions
  • Loading branch information
nwithan8 committed Apr 27, 2023
1 parent 9b19d18 commit a31b6a6
Show file tree
Hide file tree
Showing 22 changed files with 113 additions and 257 deletions.
13 changes: 1 addition & 12 deletions EasyPost.Tests/baseTests/EasyPostObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,11 @@ public void TestHashCode()
// hashcode should be the same number each time
Assert.Equal(apiKey.GetHashCode(), apiKey.GetHashCode());

// hashcode should be the same for two objects with the same properties (including Client)
// hashcode should be the same for two objects with the same properties
Assert.Equal(apiKey.GetHashCode(), sameProperties.GetHashCode());

// hashcode should be different for two objects with different properties
Assert.NotEqual(apiKey.GetHashCode(), differentProperties.GetHashCode());

// hashcode should be different for two objects with the same properties but different Clients
sameProperties.Client = new Client("key");
Assert.NotEqual(apiKey.GetHashCode(), sameProperties.GetHashCode());
}

[Fact]
Expand All @@ -73,13 +69,6 @@ public void TestEquals()
Assert.False(apiKey == differentProperties);
Assert.True(apiKey != differentProperties);

// two objects with the same properties but different clients should not be equal
sameProperties.Client = new Client("key");
Assert.NotEqual(apiKey, sameProperties);
Assert.False(apiKey.Equals(sameProperties));
Assert.False(apiKey == sameProperties);
Assert.True(apiKey != sameProperties);

// two objects of different types should not be equal
// ReSharper disable once SuspiciousTypeConversion.Global
Assert.False(apiKey.Equals(new List<string>()));
Expand Down
14 changes: 7 additions & 7 deletions EasyPost/Services/AddressService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task<Address> Create(Dictionary<string, object> parameters)
parameters.Add("verify_strict", true);
}

return await Create<Address>("addresses", parameters);
return await Request<Address>(Method.Post, "addresses", parameters);
}

/// <summary>
Expand All @@ -75,7 +75,7 @@ public async Task<Address> Create(Dictionary<string, object> parameters)
public async Task<Address> Create(BetaFeatures.Parameters.Addresses.Create parameters)
{
// Because the normal Create method does wrapping internally, we can't simply pass the parameters object to it, otherwise it will wrap the parameters twice.
return await Create<Address>("addresses", parameters.ToDictionary());
return await Request<Address>(Method.Post, "addresses", parameters.ToDictionary());
}

/// <summary>
Expand All @@ -97,7 +97,7 @@ public async Task<Address> Create(BetaFeatures.Parameters.Addresses.Create param
/// </param>
/// <returns>An Address object.</returns>
[CrudOperations.Create]
public async Task<Address> CreateAndVerify(Dictionary<string, object> parameters) => await Create<Address>("addresses/create_and_verify", parameters, "address");
public async Task<Address> CreateAndVerify(Dictionary<string, object> parameters) => await Request<Address>(Method.Post, "addresses/create_and_verify", parameters, "address");

/// <summary>
/// Create and verify an <see cref="Address"/>.
Expand All @@ -108,7 +108,7 @@ public async Task<Address> Create(BetaFeatures.Parameters.Addresses.Create param
public async Task<Address> CreateAndVerify(BetaFeatures.Parameters.Addresses.Create parameters)
{
// Because the normal Create method does wrapping internally, we can't simply pass the parameters object to it, otherwise it will wrap the parameters twice.
return await Create<Address>("addresses/create_and_verify", parameters.ToDictionary(), "address");
return await Request<Address>(Method.Post, "addresses/create_and_verify", parameters.ToDictionary(), "address");
}

/// <summary>
Expand All @@ -127,15 +127,15 @@ public async Task<Address> CreateAndVerify(BetaFeatures.Parameters.Addresses.Cre
/// </param>
/// <returns>An EasyPost.AddressCollection instance.</returns>
[CrudOperations.Read]
public async Task<AddressCollection> All(Dictionary<string, object>? parameters = null) => await List<AddressCollection>("addresses", parameters);
public async Task<AddressCollection> All(Dictionary<string, object>? parameters = null) => await Request<AddressCollection>(Method.Get, "addresses", parameters);

/// <summary>
/// List all <see cref="Address"/> objects.
/// </summary>
/// <param name="parameters"><see cref="BetaFeatures.Parameters.Addresses.All"/> parameter set.</param>
/// <returns><see cref="AddressCollection"/> instance.</returns>
[CrudOperations.Read]
public async Task<AddressCollection> All(BetaFeatures.Parameters.Addresses.All parameters) => await List<AddressCollection>("addresses", parameters.ToDictionary());
public async Task<AddressCollection> All(BetaFeatures.Parameters.Addresses.All parameters) => await Request<AddressCollection>(Method.Get, "addresses", parameters.ToDictionary());

/// <summary>
/// Get the next page of a paginated <see cref="AddressCollection"/>.
Expand All @@ -153,7 +153,7 @@ public async Task<Address> CreateAndVerify(BetaFeatures.Parameters.Addresses.Cre
/// <param name="id">String representing an Address. Starts with "adr_".</param>
/// <returns>EasyPost.Address instance.</returns>
[CrudOperations.Read]
public async Task<Address> Retrieve(string id) => await Get<Address>($"addresses/{id}");
public async Task<Address> Retrieve(string id) => await Request<Address>(Method.Get, $"addresses/{id}");

/// <summary>
/// Verify an Address.
Expand Down
3 changes: 2 additions & 1 deletion EasyPost/Services/ApiKeyService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using EasyPost._base;
using EasyPost.Http;
using EasyPost.Models.API;
using EasyPost.Utilities.Internal.Attributes;

Expand All @@ -20,7 +21,7 @@ internal ApiKeyService(EasyPostClient client)
/// </summary>
/// <returns>An EasyPost.ApiKeyCollection instances.</returns>
[CrudOperations.Read]
public async Task<ApiKeyCollection> All() => await List<ApiKeyCollection>("api_keys");
public async Task<ApiKeyCollection> All() => await Request<ApiKeyCollection>(Method.Get, "api_keys");

#endregion
}
Expand Down
12 changes: 6 additions & 6 deletions EasyPost/Services/BatchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal BatchService(EasyPostClient client)
public async Task<Batch> Create(Dictionary<string, object>? parameters = null)
{
parameters = parameters?.Wrap("batch");
return await Create<Batch>("batches", parameters);
return await Request<Batch>(Method.Post, "batches", parameters);
}

/// <summary>
Expand All @@ -46,7 +46,7 @@ public async Task<Batch> Create(Dictionary<string, object>? parameters = null)
public async Task<Batch> Create(BetaFeatures.Parameters.Batches.Create parameters)
{
// Because the normal Create method does wrapping internally, we can't simply pass the parameters object to it, otherwise it will wrap the parameters twice.
return await Create<Batch>("batches", parameters.ToDictionary());
return await Request<Batch>(Method.Post, "batches", parameters.ToDictionary());
}

/// <summary>
Expand All @@ -63,7 +63,7 @@ public async Task<Batch> Create(BetaFeatures.Parameters.Batches.Create parameter
public async Task<Batch> CreateAndBuy(Dictionary<string, object> parameters)
{
parameters = parameters.Wrap("batch");
return await Create<Batch>("batches/create_and_buy", parameters);
return await Request<Batch>(Method.Post, "batches/create_and_buy", parameters);
}

/// <summary>
Expand All @@ -75,7 +75,7 @@ public async Task<Batch> CreateAndBuy(Dictionary<string, object> parameters)
public async Task<Batch> CreateAndBuy(BetaFeatures.Parameters.Batches.Create parameters)
{
// Because the normal Create method does wrapping internally, we can't simply pass the parameters object to it, otherwise it will wrap the parameters twice.
return await Create<Batch>("batches/create_and_buy", parameters.ToDictionary());
return await Request<Batch>(Method.Post, "batches/create_and_buy", parameters.ToDictionary());
}

/// <summary>
Expand All @@ -94,15 +94,15 @@ public async Task<Batch> CreateAndBuy(BetaFeatures.Parameters.Batches.Create par
/// </param>
/// <returns>An EasyPost.BatchCollection instance.</returns>
[CrudOperations.Read]
public async Task<BatchCollection> All(Dictionary<string, object>? parameters = null) => await List<BatchCollection>("batches", parameters);
public async Task<BatchCollection> All(Dictionary<string, object>? parameters = null) => await Request<BatchCollection>(Method.Get, "batches", parameters);

/// <summary>
/// List all <see cref="Batch"/> objects.
/// </summary>
/// <param name="parameters"><see cref="BetaFeatures.Parameters.Batches.All"/> parameter set.</param>
/// <returns><see cref="BatchCollection"/> instance.</returns>
[CrudOperations.Read]
public async Task<BatchCollection> All(BetaFeatures.Parameters.Batches.All parameters) => await List<BatchCollection>("batches", parameters.ToDictionary());
public async Task<BatchCollection> All(BetaFeatures.Parameters.Batches.All parameters) => await Request<BatchCollection>(Method.Get, "batches", parameters.ToDictionary());

// TODO: Add GetNextPage function when Batches are sorted newest to oldest.

Expand Down
3 changes: 2 additions & 1 deletion EasyPost/Services/Beta/CarrierMetadataService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyPost._base;
using EasyPost.Http;
using EasyPost.Models.API.Beta;
using EasyPost.Utilities.Internal.Attributes;

Expand All @@ -26,7 +27,7 @@ public async Task<List<Carrier>> RetrieveCarrierMetadata(BetaFeatures.Parameters
{
Dictionary<string, object> data = parameters?.ToDictionary() ?? new Dictionary<string, object>();

return await Get<List<Carrier>>("metadata", data, "carriers", ApiVersion.Beta);
return await Request<List<Carrier>>(Method.Get, "metadata", data, "carriers", ApiVersion.Beta);
}

#endregion
Expand Down
9 changes: 5 additions & 4 deletions EasyPost/Services/Beta/RateService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyPost._base;
using EasyPost.Http;
using EasyPost.Models.API.Beta;
using EasyPost.Utilities.Internal.Attributes;
using EasyPost.Utilities.Internal.Extensions;
Expand Down Expand Up @@ -39,10 +40,10 @@ internal RateService(EasyPostClient client)
/// </param>
/// <returns>A list of <see cref="StatelessRate"/> objects.</returns>
[CrudOperations.Create]
public async Task<List<EasyPost.Models.API.Beta.StatelessRate>> RetrieveStatelessRates(Dictionary<string, object> parameters)
public async Task<List<StatelessRate>> RetrieveStatelessRates(Dictionary<string, object> parameters)
{
parameters = parameters.Wrap("shipment");
return await Create<List<EasyPost.Models.API.Beta.StatelessRate>>("rates", parameters, "rates", ApiVersion.Beta);
return await Request<List<StatelessRate>>(Method.Post, "rates", parameters, "rates", ApiVersion.Beta);
}

/// <summary>
Expand All @@ -51,9 +52,9 @@ public async Task<List<EasyPost.Models.API.Beta.StatelessRate>> RetrieveStateles
/// <param name="parameters"><see cref="BetaFeatures.Parameters.Beta.Rates.Retrieve"/> parameter set.</param>
/// <returns>A list of <see cref="StatelessRate"/> objects.</returns>
[CrudOperations.Create]
public async Task<List<EasyPost.Models.API.Beta.StatelessRate>> RetrieveStatelessRates(BetaFeatures.Parameters.Beta.Rates.Retrieve parameters)
public async Task<List<StatelessRate>> RetrieveStatelessRates(BetaFeatures.Parameters.Beta.Rates.Retrieve parameters)
{
return await Create<List<EasyPost.Models.API.Beta.StatelessRate>>("rates", parameters.ToDictionary(), "rates", ApiVersion.Beta);
return await Request<List<StatelessRate>>(Method.Post, "rates", parameters.ToDictionary(), "rates", ApiVersion.Beta);
}

#endregion
Expand Down
7 changes: 4 additions & 3 deletions EasyPost/Services/BillingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using EasyPost._base;
using EasyPost.Exceptions.General;
using EasyPost.Http;
using EasyPost.Models.API;
using EasyPost.Utilities.Internal;
using EasyPost.Utilities.Internal.Attributes;
Expand Down Expand Up @@ -34,7 +35,7 @@ public async Task FundWallet(string amount, PaymentMethod.Priority? priority = n

Dictionary<string, object> parameters = new() { { "amount", amount } };

await CreateNoResponse($"{paymentMethod.Endpoint}/{paymentMethod.Id}/charges", parameters);
await Request(Method.Post, $"{paymentMethod.Endpoint}/{paymentMethod.Id}/charges", parameters);
}

/// <summary>
Expand All @@ -44,7 +45,7 @@ public async Task FundWallet(string amount, PaymentMethod.Priority? priority = n
[CrudOperations.Read]
public async Task<PaymentMethodsSummary> RetrievePaymentMethodsSummary()
{
PaymentMethodsSummary paymentMethodsSummary = await Get<PaymentMethodsSummary>("payment_methods");
PaymentMethodsSummary paymentMethodsSummary = await Request<PaymentMethodsSummary>(Method.Get, "payment_methods");

return paymentMethodsSummary.Id == null
? throw new InvalidObjectError(Constants.ErrorMessages.NoPaymentMethods)
Expand All @@ -61,7 +62,7 @@ public async Task DeletePaymentMethod(PaymentMethod.Priority priority)
{
PaymentMethod paymentMethod = await GetPaymentMethodByPriority(priority);

await DeleteNoResponse($"{paymentMethod.Endpoint}/{paymentMethod.Id}");
await Request(Method.Delete, $"{paymentMethod.Endpoint}/{paymentMethod.Id}");
}

#endregion
Expand Down
3 changes: 2 additions & 1 deletion EasyPost/Services/CarrierTypeService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyPost._base;
using EasyPost.Http;
using EasyPost.Models.API;
using EasyPost.Utilities.Internal.Attributes;

Expand All @@ -21,7 +22,7 @@ internal CarrierTypeService(EasyPostClient client)
/// </summary>
/// <returns>A list of EasyPost.CarrierType instances.</returns>
[CrudOperations.Read]
public async Task<List<CarrierType>> All() => await List<List<CarrierType>>("carrier_types");
public async Task<List<CarrierType>> All() => await Request<List<CarrierType>>(Method.Get, "carrier_types");

#endregion
}
Expand Down
7 changes: 4 additions & 3 deletions EasyPost/Services/CustomsInfoService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyPost._base;
using EasyPost.Http;
using EasyPost.Models.API;
using EasyPost.Utilities.Internal.Attributes;
using EasyPost.Utilities.Internal.Extensions;
Expand Down Expand Up @@ -37,7 +38,7 @@ internal CustomsInfoService(EasyPostClient client)
public async Task<CustomsInfo> Create(Dictionary<string, object> parameters)
{
parameters = parameters.Wrap("customs_info");
return await Create<CustomsInfo>("customs_infos", parameters);
return await Request<CustomsInfo>(Method.Post, "customs_infos", parameters);
}

/// <summary>
Expand All @@ -49,7 +50,7 @@ public async Task<CustomsInfo> Create(Dictionary<string, object> parameters)
public async Task<CustomsInfo> Create(BetaFeatures.Parameters.CustomsInfo.Create parameters)
{
// Because the normal Create method does wrapping internally, we can't simply pass the parameters object to it, otherwise it will wrap the parameters twice.
return await Create<CustomsInfo>("customs_infos", parameters.ToDictionary());
return await Request<CustomsInfo>(Method.Post, "customs_infos", parameters.ToDictionary());
}

/// <summary>
Expand All @@ -58,7 +59,7 @@ public async Task<CustomsInfo> Create(BetaFeatures.Parameters.CustomsInfo.Create
/// <param name="id">String representing a CustomsInfo. Starts with "cstinfo_".</param>
/// <returns>EasyPost.CustomsInfo instance.</returns>
[CrudOperations.Read]
public async Task<CustomsInfo> Retrieve(string id) => await Get<CustomsInfo>($"customs_infos/{id}");
public async Task<CustomsInfo> Retrieve(string id) => await Request<CustomsInfo>(Method.Get, $"customs_infos/{id}");

#endregion
}
Expand Down
7 changes: 4 additions & 3 deletions EasyPost/Services/CustomsItemService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyPost._base;
using EasyPost.Http;
using EasyPost.Models.API;
using EasyPost.Utilities.Internal.Attributes;
using EasyPost.Utilities.Internal.Extensions;
Expand Down Expand Up @@ -35,7 +36,7 @@ internal CustomsItemService(EasyPostClient client)
public async Task<CustomsItem> Create(Dictionary<string, object> parameters)
{
parameters = parameters.Wrap("customs_item");
return await Create<CustomsItem>("customs_items", parameters);
return await Request<CustomsItem>(Method.Post, "customs_items", parameters);
}

/// <summary>
Expand All @@ -47,7 +48,7 @@ public async Task<CustomsItem> Create(Dictionary<string, object> parameters)
public async Task<CustomsItem> Create(BetaFeatures.Parameters.CustomsItems.Create parameters)
{
// Because the normal Create method does wrapping internally, we can't simply pass the parameters object to it, otherwise it will wrap the parameters twice.
return await Create<CustomsItem>("customs_items", parameters.ToDictionary());
return await Request<CustomsItem>(Method.Post, "customs_items", parameters.ToDictionary());
}

/// <summary>
Expand All @@ -56,7 +57,7 @@ public async Task<CustomsItem> Create(BetaFeatures.Parameters.CustomsItems.Creat
/// <param name="id">String representing a CustomsItem. Starts with "cstitem_".</param>
/// <returns>EasyPost.CustomsItem instance.</returns>
[CrudOperations.Read]
public async Task<CustomsItem> Retrieve(string id) => await Get<CustomsItem>($"customs_items/{id}");
public async Task<CustomsItem> Retrieve(string id) => await Request<CustomsItem>(Method.Get, $"customs_items/{id}");

#endregion
}
Expand Down
Loading

0 comments on commit a31b6a6

Please sign in to comment.