Skip to content

easykeys/EasyKeys.Shipping

Repository files navigation

EasyKeys.Shipping

Build status NuGet Nuget feedz.io

EasyKeys.com production ready shipment library for FedEx, Stamps and USPS shipping providers.

Give a Star! ⭐

If you like or are using this project please give it a star. Thanks!

Reusable Abstractions

FedEx Shipping

Stamps Shipping

USPS Shipping

Poly policy Sample

    var request = new ValidateAddress(model?.RequestId ?? Guid.NewGuid().ToString(), originalAddress, originalAddress);

    var response = await _policy.ExecuteAsync(
        async (ctx, ct) => await _validationProvider.ValidateAddressAsync(request, ct),
        new Context
        {
            [_policyContextMethod] = "ValidateAsync"
        },
        cancellationToken);

    private IAsyncPolicy GetRetryWithTimeOutPolicy()
    {
        // each call is limited to 30 seconds in case when fedex is non-reponsive and it is 1min timeout, it is way to long
        // The request channel timed out attempting to send after 00:01:00.
        // Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout.
        var timeoutPolicy = Policy.TimeoutAsync(30, TimeoutStrategy.Pessimistic);

        var jitterer = new Random();

        return Policy
            .Handle<TimeoutRejectedException>()
            .WaitAndRetryAsync(
                  retryCount: 3,    // exponential back-off plus some jitter
                  sleepDurationProvider: (retryAttempt, context) =>
                  {
                      return TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))
                            + TimeSpan.FromMilliseconds(jitterer.Next(0, 200));
                  },
                  onRetry: (ex, span, context) =>
                  {
                      var methodName = context[_policyContextMethod] ?? "MethodNotSpecified";

                      _logger.LogWarning(
                        "{Method} wait {Seconds} to execute with exception: {Message} for named policy: {Policy}",
                        methodName,
                        span.TotalSeconds,
                        ex.Message,
                        context.PolicyKey);
                  })
             .WithPolicyKey($"{nameof(FedExAddressValidationProvider)}WaitAndRetryAsync")
             .WrapAsync(timeoutPolicy);
    }

References