Search code examples
c#staticstatic-librariesstatic-variableseasypost

Static Fields - EasyPost - ClientManager Init


I am currently having a problem when it comes to static fields in C#, the problem is in the way EasyPost instantiates their ClientManger however, I think someone with a better understanding regarding static fields might be able to help me.

I am creating a plugin that allows for multiple users to access EasyPost for tracking off parcels.

I have written a Unit test to test the scenario where multiple people use it at the same time.

Unit Test:

[TestMethod()]
public void TestMultiInit()
    {
        var Client2 = new cpEasyPost("123");
        var Client = new cpEasyPost("123456qq785412");

        var Shipment = Client.CreateShipment(
             new EasyPost.Address
             {
                 street1 = "417 MONTGOMERY ST",
                 street2 = "FLOOR 5",
                 city = "SAN FRANCISCO",
                 state = "CA",
                 zip = "94104",
                 country = "US",
                 company = "EasyPost"
             },
             new EasyPost.Address
             {
                 street1 = "417 MONTGOMERY ST",
                 street2 = "FLOOR 5",
                 city = "SAN FRANCISCO",
                 state = "CA",
                 zip = "94104",
                 country = "US",
                 company = "EasyPost"
             },
             new EasyPost.Parcel
             {
                 length = 20.2,
                 width = 10.9,
                 height = 5,
                 weight = 65.9
             });

        var Shipment2 = Client2.CreateShipment(
             new EasyPost.Address
             {
                 street1 = "417 MONTGOMERY ST",
                 street2 = "FLOOR 5",
                 city = "SAN FRANCISCO",
                 state = "CA",
                 zip = "94104",
                 country = "US",
                 company = "EasyPost"
             },
             new EasyPost.Address
             {
                 street1 = "417 MONTGOMERY ST",
                 street2 = "FLOOR 5",
                 city = "SAN FRANCISCO",
                 state = "CA",
                 zip = "94104",
                 country = "US",
                 company = "EasyPost"
             },
             new EasyPost.Parcel
             {
                 length = 20.2,
                 width = 10.9,
                 height = 5,
                 weight = 65.9
             });


    }

The issue is Client2 has incorrect key so if I try and create a shipment with it should fail however because of ClinetManager being static is uses Client init of it because if was later.

Here is my ctor:

public cpEasyPost(string secretKey)
    {            
        SecretKey = secretKey;
        //Original way of init Client
        //ClientManager.SetCurrent(SecretKey);

        //Create ClientManager
        ClientManager.SetCurrent(() => new Client(new ClientConfiguration(SecretKey)));
    }

And here is my method:

 public Shipment CreateShipment(Address AddressFrom, Address AddressTo, Parcel Parcel, CustomsInfo customs = null, string CustomReference = "", double? InsauranceAmount = null)
    {
        //Validate Adress
        var isValidFrom = ValidateAddress(AddressFrom);
        var isValidTo = ValidateAddress(AddressFrom);

        if (!isValidFrom.isSuccess)
            throw new Exception("Address From is not Valid");

        if (!isValidTo.isSuccess)
            throw new Exception("Address To is not Valid");

        //Validate Pacrcel
        var isValidParcel = ValidateParcel(Parcel);

        if (!isValidFrom.isSuccess)
            throw new Exception("Parcel is not Valid");

        //Create Shipment
        Shipment shipment = new Shipment()
        {
            reference = CustomReference,
            to_address = AddressTo,
            from_address = AddressFrom,
            parcel = Parcel,
            customs_info = customs            
        };

        //ClientManager.SetCurrent(SecretKey); **
        shipment.Create();

        //Add Insurance
        if (InsauranceAmount != null)
            shipment.Insure(InsauranceAmount.Value);

        return shipment;

    }

No the issue I have is that the ClinetManager is static, this is a locked DDL so I can't modify this. In the method, I considered ssetting the manager before every call however this does not seem like the best solution as it could still theoretically lead to issues I marked this with **.

Any help would be greatly appreciated. Thank you in adnvacne.


Solution

  • At the end, I just rebuild the code from their SDK into something that fitted my needs better. There is no other way around this.