From 3b2c80bc4c5863729833b79f2daf64ede7a75bdd Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Sun, 21 May 2023 23:34:57 -0700 Subject: [PATCH] Fixed mouser api so Test functionality works if only orders api is enabled. Fixed mouser order import to allow import even if search api key is not enabled (more minimal data is returned) --- .../Services/IntegrationService.cs | 22 +++++- .../Binner.Common/Services/PartService.cs | 78 +++++++++++++------ 2 files changed, 71 insertions(+), 29 deletions(-) diff --git a/Binner/Library/Binner.Common/Services/IntegrationService.cs b/Binner/Library/Binner.Common/Services/IntegrationService.cs index 2f2ca3bd..aaffff1a 100644 --- a/Binner/Library/Binner.Common/Services/IntegrationService.cs +++ b/Binner/Library/Binner.Common/Services/IntegrationService.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Binner.Model.Configuration.Integrations; namespace Binner.Common.Services { @@ -143,10 +144,23 @@ public async Task TestApiAsync(TestApiRequest request) return new TestApiResponse(nameof(Integrations.MouserApi), "Api is not enabled."); try { - var result = await api.SearchAsync("LM555", 1); - if (result.Errors.Any()) - return new TestApiResponse(nameof(Integrations.MouserApi), string.Join(". ", result.Errors)); - return new TestApiResponse(nameof(Integrations.MouserApi), true); + if (((MouserConfiguration)api.Configuration).IsConfigured) + { + var result = await api.SearchAsync("LM555", 1); + if (result.Errors.Any()) + return new TestApiResponse(nameof(Integrations.MouserApi), string.Join(". ", result.Errors)); + return new TestApiResponse(nameof(Integrations.MouserApi), true); + } + + if (((MouserConfiguration)api.Configuration).IsOrdersConfigured) + { + var result = await api.GetOrderAsync("1111111"); + if (result.Errors.Any() && !result.Errors.First().EndsWith("Not Found")) + return new TestApiResponse(nameof(Integrations.MouserApi), string.Join(". ", result.Errors)); + return new TestApiResponse(nameof(Integrations.MouserApi), true); + } + + return new TestApiResponse(nameof(Integrations.MouserApi), false); } catch (MouserErrorsException ex) { diff --git a/Binner/Library/Binner.Common/Services/PartService.cs b/Binner/Library/Binner.Common/Services/PartService.cs index f65b93b4..14f70fab 100644 --- a/Binner/Library/Binner.Common/Services/PartService.cs +++ b/Binner/Library/Binner.Common/Services/PartService.cs @@ -349,36 +349,64 @@ public async Task DeletePartSupplierAsync(PartSupplier partSupplier) // get details on this mouser part if (string.IsNullOrEmpty(lineItem.MouserPartNumber)) continue; - var partResponse = await mouserApi.GetProductDetailsAsync(lineItem.MouserPartNumber); - if (!partResponse.RequiresAuthentication && partResponse?.Errors.Any() == false) + if (((MouserConfiguration)mouserApi.Configuration).IsConfigured) { - if (partResponse.Response != null) + // request additional information for the part as orders doesn't return much + var partResponse = await mouserApi.GetProductDetailsAsync(lineItem.MouserPartNumber); + if (!partResponse.RequiresAuthentication && partResponse?.Errors.Any() == false) { - var searchResults = (ICollection)partResponse.Response; - // convert the part to a common part - var part = searchResults.First(); - commonParts.Add(new CommonPart + if (partResponse.Response != null) { - SupplierPartNumber = part.MouserPartNumber, - Supplier = "Mouser", - ManufacturerPartNumber = part.ManufacturerPartNumber, - Manufacturer = part.Manufacturer, - Description = part.Description, - ImageUrl = part.ImagePath, - DatasheetUrls = new List { part.DataSheetUrl ?? string.Empty }, - ProductUrl = part.ProductDetailUrl, - Status = part.LifecycleStatus, - Currency = mouserOrderResponse.CurrencyCode, - AdditionalPartNumbers = new List(), - BasePartNumber = part.ManufacturerPartNumber, - MountingTypeId = 0, - PackageType = "", - Cost = lineItem.UnitPrice, - QuantityAvailable = lineItem.Quantity, - Reference = lineItem.CartItemCustPartNumber, - }); + var searchResults = (ICollection)partResponse.Response; + // convert the part to a common part + var part = searchResults.First(); + commonParts.Add(new CommonPart + { + SupplierPartNumber = part.MouserPartNumber, + Supplier = "Mouser", + ManufacturerPartNumber = part.ManufacturerPartNumber, + Manufacturer = part.Manufacturer, + Description = part.Description, + ImageUrl = part.ImagePath, + DatasheetUrls = new List { part.DataSheetUrl ?? string.Empty }, + ProductUrl = part.ProductDetailUrl, + Status = part.LifecycleStatus, + Currency = mouserOrderResponse.CurrencyCode, + AdditionalPartNumbers = new List(), + BasePartNumber = part.ManufacturerPartNumber, + MountingTypeId = 0, + PackageType = "", + Cost = lineItem.UnitPrice, + QuantityAvailable = lineItem.Quantity, + Reference = lineItem.CartItemCustPartNumber, + }); + } } } + else + { + // use the more minimal information provided by the order import call + commonParts.Add(new CommonPart + { + SupplierPartNumber = lineItem.MouserPartNumber, + Supplier = "Mouser", + ManufacturerPartNumber = lineItem.MfrPartNumber, + Manufacturer = lineItem.Manufacturer, + Description = lineItem.Description, + //ImageUrl = part.ImagePath, + //DatasheetUrls = new List { part.DataSheetUrl ?? string.Empty }, + //ProductUrl = lineItem.ProductDetailUrl, + //Status = part.LifecycleStatus, + Currency = mouserOrderResponse.CurrencyCode, + AdditionalPartNumbers = new List(), + BasePartNumber = lineItem.MfrPartNumber, + MountingTypeId = 0, + PackageType = "", + Cost = lineItem.UnitPrice, + QuantityAvailable = lineItem.Quantity, + Reference = lineItem.CartItemCustPartNumber, + }); + } } foreach (var part in commonParts)