Skip to content

Commit

Permalink
#6712 Migrated UPS to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
skoshelev committed Dec 14, 2023
1 parent 31ac886 commit 2b935d6
Show file tree
Hide file tree
Showing 35 changed files with 7,441 additions and 12,544 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Nop.Plugin.Shipping.UPS.API;

public class NullToEmptyStringResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
return type.GetProperties()
.Select(p => {
var jp = base.CreateProperty(p, memberSerialization);
jp.ValueProvider = new NullToEmptyStringValueProvider(p);
return jp;
}).ToList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using Newtonsoft.Json.Serialization;

namespace Nop.Plugin.Shipping.UPS.API
{
public class NullToEmptyStringValueProvider : IValueProvider
{
private readonly PropertyInfo _memberInfo;

public NullToEmptyStringValueProvider(PropertyInfo memberInfo)
{
_memberInfo = memberInfo;
}

public object GetValue(object target)
{
var result = _memberInfo.GetValue(target);

if (_memberInfo.PropertyType != typeof(string))
return result;

var attributes = _memberInfo
.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.RequiredAttribute)).FirstOrDefault() as System.ComponentModel.DataAnnotations.RequiredAttribute;

if ((attributes?.AllowEmptyStrings ?? false) && result == null)
result = "";

return result;
}

public void SetValue(object target, object value)
{
_memberInfo.SetValue(target, value);
}
}
}
33 changes: 33 additions & 0 deletions src/Plugins/Nop.Plugin.Shipping.UPS/API/OAuth/NopOAuthClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Nop.Plugin.Shipping.UPS.Services;

namespace Nop.Plugin.Shipping.UPS.API.OAuth
{
public partial class OAuthClient
{
private UPSSettings _upsSettings;

public OAuthClient(HttpClient httpClient, UPSSettings upsSettings) : this(httpClient)
{
_upsSettings = upsSettings;

if (!_upsSettings.UseSandbox)
BaseUrl = UPSDefaults.ApiUrl.Replace("/api", "");
}

partial void PrepareRequest(HttpClient client, HttpRequestMessage request,
string url)
{
client.PrepareRequest(request, _upsSettings);
}

public virtual Task<GenerateTokenSuccessResponse> GenerateTokenAsync()
{
return GenerateTokenAsync(null, new Body(), CancellationToken.None);
}

partial void ProcessResponse(HttpClient client, HttpResponseMessage response)
{
client.ProcessResponse(response, _upsSettings);
}
}
}
475 changes: 475 additions & 0 deletions src/Plugins/Nop.Plugin.Shipping.UPS/API/OAuth/OAuthClient.cs

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions src/Plugins/Nop.Plugin.Shipping.UPS/API/Rates/NopRateClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Newtonsoft.Json;
using Nop.Plugin.Shipping.UPS.Services;

namespace Nop.Plugin.Shipping.UPS.API.Rates
{
public partial class RateClient
{
private UPSSettings _upsSettings;
private string _accessToken;

public RateClient(HttpClient httpClient, UPSSettings upsSettings, string accessToken) : this(httpClient)
{
_upsSettings = upsSettings;
_accessToken = accessToken;

if (!_upsSettings.UseSandbox)
BaseUrl = UPSDefaults.ApiUrl;
}

partial void PrepareRequest(HttpClient client, HttpRequestMessage request,
string url)
{
client.PrepareRequest(request, _upsSettings, _accessToken);
}

public async Task<RateResponse> ProcessRateAsync(RateRequest request)
{
var response = await RateAsync(Guid.NewGuid().ToString(),
_upsSettings.UseSandbox ? "testing" : UPSDefaults.SystemName, string.Empty, "v1", "Shop", new RATERequestWrapper
{
RateRequest = request
});

return response.RateResponse;
}

partial void UpdateJsonSerializerSettings(JsonSerializerSettings settings)
{
settings.ContractResolver = new NullToEmptyStringResolver();
}
}

public partial class RateResponse_RatedShipment
{
/// <summary>
/// <remarks>
/// For some reason, the description of this field in the API definition
/// does not correspond to reality. More precisely, it does not always correspond to reality,
/// sometimes the answer comes as a single object and sometimes as a collection of objects.
/// since we do not use this data in our code, we decided to change type to object (It might be ICollection<RatedShipment_RatedPackage> or RatedShipment_RatedPackage).
///
/// Do not delete this field unless you have made sure that the description of the API
/// or the response from the server has changed</remarks>
/// </summary>
[JsonProperty("RatedPackage", Required = Required.Always)]
[System.ComponentModel.DataAnnotations.Required]
public object RatedPackage { get; set; }

/// <summary>
/// <remarks>
/// For some reason, the description of this field in the API definition
/// does not correspond to reality. More precisely, it does not always correspond to reality,
/// sometimes the answer comes as a single object and sometimes as a collection of objects.
/// since we do not use this data in our code, we decided to change type to object (It might be ICollection<RatedShipment_RatedShipmentAlert> or RatedShipment_RatedShipmentAlert).
///
/// Do not delete this field unless you have made sure that the description of the API
/// or the response from the server has changed</remarks>
/// </summary>
[JsonProperty("RatedShipmentAlert", Required = Required.DisallowNull, NullValueHandling = NullValueHandling.Ignore)]
public object RatedShipmentAlert { get; set; }
}
}
Loading

0 comments on commit 2b935d6

Please sign in to comment.