| | | 1 | | using Newtonsoft.Json.Linq; |
| | | 2 | | |
| | | 3 | | namespace Services.ApiServices.NikeiApi; |
| | | 4 | | |
| | | 5 | | /// <summary>Read-only методы Nikei, доступные для диагностики и последующего подключения проценки.</summary> |
| | | 6 | | public sealed class NikeiDataManager |
| | | 7 | | { |
| | | 8 | | private readonly NikeiHttpClient _client; |
| | | 9 | | |
| | 116 | 10 | | public NikeiDataManager(NikeiHttpClient client) => _client = client; |
| | | 11 | | |
| | | 12 | | public Task<JToken> SearchBrandsAsync(string article, CancellationToken cancellationToken = default) => |
| | 0 | 13 | | _client.GetAsync($"parts/search/{Uri.EscapeDataString(article.Trim())}", cancellationToken); |
| | | 14 | | |
| | | 15 | | /// <summary>Точная проценка выбранного бренда. filterByArticle исключает аналоги уже на стороне Nikei.</summary> |
| | | 16 | | public Task<JToken> SearchPartsAsync(string article, string brand, CancellationToken cancellationToken = default) => |
| | 0 | 17 | | _client.GetAsync($"parts/search/{Uri.EscapeDataString(article.Trim())}/{Uri.EscapeDataString(brand.Trim())}?filt |
| | | 18 | | |
| | | 19 | | public Task<JToken> GetOfficesAsync(CancellationToken cancellationToken = default) => |
| | 1 | 20 | | _client.GetAsync("office/getOffices", cancellationToken); |
| | | 21 | | |
| | | 22 | | public Task<JToken> GetClientAddressesAsync(CancellationToken cancellationToken = default) => |
| | 1 | 23 | | _client.GetAsync("office/getClientAddresses", cancellationToken); |
| | | 24 | | |
| | | 25 | | public Task<JToken> GetCartAsync(CancellationToken cancellationToken = default) => |
| | 1 | 26 | | _client.GetAsync("account/cart", cancellationToken); |
| | | 27 | | |
| | | 28 | | public Task<JToken> GetOrdersAsync(CancellationToken cancellationToken = default) => |
| | 1 | 29 | | _client.GetAsync("account/order", cancellationToken); |
| | | 30 | | |
| | | 31 | | public Task<JToken> GetOrderStatusesAsync(CancellationToken cancellationToken = default) => |
| | 0 | 32 | | _client.GetAsync("misc/order_statuses", cancellationToken); |
| | | 33 | | |
| | | 34 | | public async Task<NikeiCartAddResult> AddToCartAsync(string product, int quantity, string comment, |
| | | 35 | | CancellationToken cancellationToken = default) |
| | | 36 | | { |
| | 15 | 37 | | var response = await _client.PostFormAsync("account/cart/add", new Dictionary<string, string> |
| | 15 | 38 | | { |
| | 15 | 39 | | ["product"] = product, |
| | 15 | 40 | | ["quantity"] = quantity.ToString(System.Globalization.CultureInfo.InvariantCulture), |
| | 15 | 41 | | ["comment"] = comment |
| | 15 | 42 | | }, cancellationToken); |
| | 14 | 43 | | return NikeiCartAddResult.From(response); |
| | 14 | 44 | | } |
| | | 45 | | |
| | | 46 | | public Task<JToken> RemoveFromCartAsync(IReadOnlyCollection<string> products, CancellationToken cancellationToken = |
| | 6 | 47 | | _client.PostFormAsync("account/cart/remove", products.Select((value, index) => |
| | 27 | 48 | | new KeyValuePair<string, string>($"products[{index}]", value)).ToDictionary(x => x.Key, x => x.Value), cance |
| | | 49 | | |
| | | 50 | | public Task<JToken> CreateOrderAsync(IReadOnlyCollection<string> products, int shippingType, int shippingAddress, |
| | | 51 | | string comment, CancellationToken cancellationToken = default) |
| | | 52 | | { |
| | 19 | 53 | | var values = products.Select((value, index) => new KeyValuePair<string, string>($"products[{index}]", value)) |
| | 30 | 54 | | .ToDictionary(x => x.Key, x => x.Value); |
| | 8 | 55 | | values["shipping_type"] = shippingType.ToString(System.Globalization.CultureInfo.InvariantCulture); |
| | 8 | 56 | | values["shipping_address"] = shippingAddress.ToString(System.Globalization.CultureInfo.InvariantCulture); |
| | 8 | 57 | | values["order_comment"] = comment; |
| | 8 | 58 | | return _client.PostFormAsync("account/order/create", values, cancellationToken); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public async Task PingAsync(CancellationToken cancellationToken = default) |
| | | 62 | | { |
| | 0 | 63 | | var response = await SearchBrandsAsync("9054", cancellationToken); |
| | 0 | 64 | | NikeiHttpClient.ThrowIfError(response, "/parts/search/9054"); |
| | 0 | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | public sealed record NikeiCartAddResult(bool IsSuccess, string? ToOrder, string Message) |
| | | 69 | | { |
| | | 70 | | public static NikeiCartAddResult From(JToken response) |
| | | 71 | | { |
| | | 72 | | var success = string.Equals(response.Value<string>("status"), "ok", StringComparison.OrdinalIgnoreCase); |
| | | 73 | | return new NikeiCartAddResult(success, response.Value<string>("data"), |
| | | 74 | | response.Value<string>("message") ?? (success ? "Позиция добавлена." : "Nikei отклонил добавление в корзину. |
| | | 75 | | } |
| | | 76 | | } |