| | | 1 | | using Newtonsoft.Json.Linq; |
| | | 2 | | |
| | | 3 | | namespace Services.ApiServices.MlAutoApi; |
| | | 4 | | |
| | | 5 | | /// <summary>Разрешённые read-only методы российского контура ML-Auto.</summary> |
| | | 6 | | public sealed class MlAutoRuDataManager |
| | | 7 | | { |
| | | 8 | | private readonly MlAutoRuHttpClient _client; |
| | | 9 | | |
| | 146 | 10 | | public MlAutoRuDataManager(MlAutoRuHttpClient client) => _client = client; |
| | | 11 | | |
| | 0 | 12 | | public Task<JToken> PingAsync(CancellationToken cancellationToken = default) => _client.PingAsync(cancellationToken) |
| | | 13 | | |
| | | 14 | | public Task<JToken> ShowInfoAsync(CancellationToken cancellationToken = default) => |
| | 0 | 15 | | _client.PostAsync("/ShowInfo/", null, cancellationToken); |
| | | 16 | | |
| | | 17 | | public Task<JToken> GetStoragesAsync(CancellationToken cancellationToken = default) => |
| | 0 | 18 | | _client.PostAsync("/ImportersSearch/", null, cancellationToken); |
| | | 19 | | |
| | | 20 | | public Task<JToken> GetDeliveryAddressesAsync(CancellationToken cancellationToken = default) => |
| | 0 | 21 | | _client.PostAsync("/MyDeliveryAddress/", null, cancellationToken); |
| | | 22 | | |
| | | 23 | | public Task<JToken> PostAsync(string path, IReadOnlyDictionary<string, string?>? parameters, CancellationToken cance |
| | 24 | 24 | | _client.PostAsync(path, parameters, cancellationToken); |
| | | 25 | | |
| | | 26 | | public Task<MlAutoApiResponse> CreateOrderTestDetailedAsync(IReadOnlyDictionary<string, string?> parameters, Cancell |
| | 19 | 27 | | _client.PostDetailedAsync("/CreateOrderTest/", parameters, cancellationToken); |
| | | 28 | | |
| | | 29 | | public Task<JToken> CreateOrderAsync(IReadOnlyDictionary<string, string?> parameters, CancellationToken cancellation |
| | 15 | 30 | | _client.PostAsync("/CreateOrder/", parameters, cancellationToken); |
| | | 31 | | |
| | | 32 | | public Task<JToken> GetOrderByNumberAsync(string orderNumber, CancellationToken cancellationToken = default) => |
| | 19 | 33 | | _client.PostAsync("/GetOrderByNumber/", new Dictionary<string, string?> { ["ORDER-NUMBER"] = orderNumber }, canc |
| | | 34 | | |
| | 1 | 35 | | public Task<JToken> GetMyOrdersAsync(CancellationToken cancellationToken = default) => _client.PostAsync("/GetMyOrde |
| | | 36 | | |
| | | 37 | | public async Task<IReadOnlyList<MlAutoOrderSummary>> GetOrderSummariesAsync(CancellationToken cancellationToken = de |
| | | 38 | | { |
| | 1 | 39 | | var response = await GetMyOrdersAsync(cancellationToken); |
| | 4 | 40 | | return Objects(response, "ORDERS").Select(x => new MlAutoOrderSummary(Value(x, "ORDER-NUMBER", "ORDERNUMBER", "N |
| | 9 | 41 | | .Where(x => !string.IsNullOrWhiteSpace(x.Number)).DistinctBy(x => x.Number).OrderByDescending(x => x.Created |
| | 1 | 42 | | } |
| | | 43 | | |
| | | 44 | | public async Task<IReadOnlyList<MlAutoOrderItemInfo>> GetOrderItemsAsync(string orderNumber, CancellationToken cance |
| | | 45 | | { |
| | 3 | 46 | | var response = await GetOrderByNumberAsync(orderNumber, cancellationToken); |
| | 6 | 47 | | return Objects(response, "ITEMS").Select(x => new MlAutoOrderItemInfo(Value(x, "PIN") ?? string.Empty, Value(x, |
| | 2 | 48 | | } |
| | | 49 | | |
| | | 50 | | public Task<JToken> SearchAsync(string article, string brand, CancellationToken cancellationToken = default) => |
| | 0 | 51 | | _client.PostAsync("/Search/", new Dictionary<string, string?> |
| | 0 | 52 | | { |
| | 0 | 53 | | ["ARTICLE"] = article.Trim(), ["BRAND"] = brand.Trim(), ["SEARCH_TYPE"] = "0" |
| | 0 | 54 | | }, cancellationToken); |
| | | 55 | | |
| | 3 | 56 | | private static IEnumerable<JObject> Objects(JToken token, string arrayName) => token switch |
| | 3 | 57 | | { |
| | 0 | 58 | | JArray array => array.OfType<JObject>(), |
| | 6 | 59 | | JObject obj when obj.GetValue(arrayName, StringComparison.OrdinalIgnoreCase) is JArray nested => nested.OfType<J |
| | 0 | 60 | | JObject obj => [obj], |
| | 0 | 61 | | _ => [] |
| | 3 | 62 | | }; |
| | | 63 | | |
| | 96 | 64 | | private static string? Value(JObject source, params string[] names) => names.Select(name => source.GetValue(name, St |
| | | 65 | | } |