Skip to content

Commit 3b2c80b

Browse files
committed
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)
1 parent 706451f commit 3b2c80b

File tree

2 files changed

+71
-29
lines changed

2 files changed

+71
-29
lines changed

Binner/Library/Binner.Common/Services/IntegrationService.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using System.Threading.Tasks;
9+
using Binner.Model.Configuration.Integrations;
910

1011
namespace Binner.Common.Services
1112
{
@@ -143,10 +144,23 @@ public async Task<TestApiResponse> TestApiAsync(TestApiRequest request)
143144
return new TestApiResponse(nameof(Integrations.MouserApi), "Api is not enabled.");
144145
try
145146
{
146-
var result = await api.SearchAsync("LM555", 1);
147-
if (result.Errors.Any())
148-
return new TestApiResponse(nameof(Integrations.MouserApi), string.Join(". ", result.Errors));
149-
return new TestApiResponse(nameof(Integrations.MouserApi), true);
147+
if (((MouserConfiguration)api.Configuration).IsConfigured)
148+
{
149+
var result = await api.SearchAsync("LM555", 1);
150+
if (result.Errors.Any())
151+
return new TestApiResponse(nameof(Integrations.MouserApi), string.Join(". ", result.Errors));
152+
return new TestApiResponse(nameof(Integrations.MouserApi), true);
153+
}
154+
155+
if (((MouserConfiguration)api.Configuration).IsOrdersConfigured)
156+
{
157+
var result = await api.GetOrderAsync("1111111");
158+
if (result.Errors.Any() && !result.Errors.First().EndsWith("Not Found"))
159+
return new TestApiResponse(nameof(Integrations.MouserApi), string.Join(". ", result.Errors));
160+
return new TestApiResponse(nameof(Integrations.MouserApi), true);
161+
}
162+
163+
return new TestApiResponse(nameof(Integrations.MouserApi), false);
150164
}
151165
catch (MouserErrorsException ex)
152166
{

Binner/Library/Binner.Common/Services/PartService.cs

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -349,36 +349,64 @@ public async Task<bool> DeletePartSupplierAsync(PartSupplier partSupplier)
349349
// get details on this mouser part
350350
if (string.IsNullOrEmpty(lineItem.MouserPartNumber))
351351
continue;
352-
var partResponse = await mouserApi.GetProductDetailsAsync(lineItem.MouserPartNumber);
353-
if (!partResponse.RequiresAuthentication && partResponse?.Errors.Any() == false)
352+
if (((MouserConfiguration)mouserApi.Configuration).IsConfigured)
354353
{
355-
if (partResponse.Response != null)
354+
// request additional information for the part as orders doesn't return much
355+
var partResponse = await mouserApi.GetProductDetailsAsync(lineItem.MouserPartNumber);
356+
if (!partResponse.RequiresAuthentication && partResponse?.Errors.Any() == false)
356357
{
357-
var searchResults = (ICollection<MouserPart>)partResponse.Response;
358-
// convert the part to a common part
359-
var part = searchResults.First();
360-
commonParts.Add(new CommonPart
358+
if (partResponse.Response != null)
361359
{
362-
SupplierPartNumber = part.MouserPartNumber,
363-
Supplier = "Mouser",
364-
ManufacturerPartNumber = part.ManufacturerPartNumber,
365-
Manufacturer = part.Manufacturer,
366-
Description = part.Description,
367-
ImageUrl = part.ImagePath,
368-
DatasheetUrls = new List<string> { part.DataSheetUrl ?? string.Empty },
369-
ProductUrl = part.ProductDetailUrl,
370-
Status = part.LifecycleStatus,
371-
Currency = mouserOrderResponse.CurrencyCode,
372-
AdditionalPartNumbers = new List<string>(),
373-
BasePartNumber = part.ManufacturerPartNumber,
374-
MountingTypeId = 0,
375-
PackageType = "",
376-
Cost = lineItem.UnitPrice,
377-
QuantityAvailable = lineItem.Quantity,
378-
Reference = lineItem.CartItemCustPartNumber,
379-
});
360+
var searchResults = (ICollection<MouserPart>)partResponse.Response;
361+
// convert the part to a common part
362+
var part = searchResults.First();
363+
commonParts.Add(new CommonPart
364+
{
365+
SupplierPartNumber = part.MouserPartNumber,
366+
Supplier = "Mouser",
367+
ManufacturerPartNumber = part.ManufacturerPartNumber,
368+
Manufacturer = part.Manufacturer,
369+
Description = part.Description,
370+
ImageUrl = part.ImagePath,
371+
DatasheetUrls = new List<string> { part.DataSheetUrl ?? string.Empty },
372+
ProductUrl = part.ProductDetailUrl,
373+
Status = part.LifecycleStatus,
374+
Currency = mouserOrderResponse.CurrencyCode,
375+
AdditionalPartNumbers = new List<string>(),
376+
BasePartNumber = part.ManufacturerPartNumber,
377+
MountingTypeId = 0,
378+
PackageType = "",
379+
Cost = lineItem.UnitPrice,
380+
QuantityAvailable = lineItem.Quantity,
381+
Reference = lineItem.CartItemCustPartNumber,
382+
});
383+
}
380384
}
381385
}
386+
else
387+
{
388+
// use the more minimal information provided by the order import call
389+
commonParts.Add(new CommonPart
390+
{
391+
SupplierPartNumber = lineItem.MouserPartNumber,
392+
Supplier = "Mouser",
393+
ManufacturerPartNumber = lineItem.MfrPartNumber,
394+
Manufacturer = lineItem.Manufacturer,
395+
Description = lineItem.Description,
396+
//ImageUrl = part.ImagePath,
397+
//DatasheetUrls = new List<string> { part.DataSheetUrl ?? string.Empty },
398+
//ProductUrl = lineItem.ProductDetailUrl,
399+
//Status = part.LifecycleStatus,
400+
Currency = mouserOrderResponse.CurrencyCode,
401+
AdditionalPartNumbers = new List<string>(),
402+
BasePartNumber = lineItem.MfrPartNumber,
403+
MountingTypeId = 0,
404+
PackageType = "",
405+
Cost = lineItem.UnitPrice,
406+
QuantityAvailable = lineItem.Quantity,
407+
Reference = lineItem.CartItemCustPartNumber,
408+
});
409+
}
382410
}
383411

384412
foreach (var part in commonParts)

0 commit comments

Comments
 (0)