Skip to content

Commit

Permalink
Fixed mouser api so Test functionality works if only orders api is en…
Browse files Browse the repository at this point in the history
…abled.

Fixed mouser order import to allow import even if search api key is not enabled (more minimal data is returned)
  • Loading branch information
replaysMike committed May 22, 2023
1 parent 706451f commit 3b2c80b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 29 deletions.
22 changes: 18 additions & 4 deletions Binner/Library/Binner.Common/Services/IntegrationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Binner.Model.Configuration.Integrations;

namespace Binner.Common.Services
{
Expand Down Expand Up @@ -143,10 +144,23 @@ public async Task<TestApiResponse> 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)
{
Expand Down
78 changes: 53 additions & 25 deletions Binner/Library/Binner.Common/Services/PartService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,36 +349,64 @@ public async Task<bool> 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<MouserPart>)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<string> { part.DataSheetUrl ?? string.Empty },
ProductUrl = part.ProductDetailUrl,
Status = part.LifecycleStatus,
Currency = mouserOrderResponse.CurrencyCode,
AdditionalPartNumbers = new List<string>(),
BasePartNumber = part.ManufacturerPartNumber,
MountingTypeId = 0,
PackageType = "",
Cost = lineItem.UnitPrice,
QuantityAvailable = lineItem.Quantity,
Reference = lineItem.CartItemCustPartNumber,
});
var searchResults = (ICollection<MouserPart>)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<string> { part.DataSheetUrl ?? string.Empty },
ProductUrl = part.ProductDetailUrl,
Status = part.LifecycleStatus,
Currency = mouserOrderResponse.CurrencyCode,
AdditionalPartNumbers = new List<string>(),
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<string> { part.DataSheetUrl ?? string.Empty },
//ProductUrl = lineItem.ProductDetailUrl,
//Status = part.LifecycleStatus,
Currency = mouserOrderResponse.CurrencyCode,
AdditionalPartNumbers = new List<string>(),
BasePartNumber = lineItem.MfrPartNumber,
MountingTypeId = 0,
PackageType = "",
Cost = lineItem.UnitPrice,
QuantityAvailable = lineItem.Quantity,
Reference = lineItem.CartItemCustPartNumber,
});
}
}

foreach (var part in commonParts)
Expand Down

0 comments on commit 3b2c80b

Please sign in to comment.