2021-07-20 22:51:43 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net.Http;
|
2021-08-05 06:22:19 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2021-08-07 22:20:46 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-07-20 22:51:43 +00:00
|
|
|
|
using Props.Shop.Adafruit.Api;
|
|
|
|
|
using Props.Shop.Framework;
|
|
|
|
|
|
|
|
|
|
namespace Props.Shop.Adafruit
|
|
|
|
|
{
|
|
|
|
|
public class AdafruitShop : IShop
|
|
|
|
|
{
|
2021-08-07 22:20:46 +00:00
|
|
|
|
private ILoggerFactory loggerFactory;
|
|
|
|
|
private ILogger<AdafruitShop> logger;
|
2021-08-05 06:22:19 +00:00
|
|
|
|
private SearchManager searchManager;
|
2021-07-20 22:51:43 +00:00
|
|
|
|
private Configuration configuration;
|
|
|
|
|
private HttpClient http;
|
|
|
|
|
private bool disposedValue;
|
|
|
|
|
|
|
|
|
|
public string ShopName => "Adafruit";
|
|
|
|
|
|
|
|
|
|
public string ShopDescription => "A electronic component online hardware company.";
|
|
|
|
|
|
|
|
|
|
public string ShopModuleAuthor => "Reslate";
|
|
|
|
|
|
|
|
|
|
public SupportedFeatures SupportedFeatures => new SupportedFeatures(
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
2021-08-07 22:20:46 +00:00
|
|
|
|
public void Initialize(string workspaceDir, ILoggerFactory loggerFactory)
|
2021-07-20 22:51:43 +00:00
|
|
|
|
{
|
2021-08-07 22:20:46 +00:00
|
|
|
|
this.loggerFactory = loggerFactory;
|
2021-08-05 06:22:19 +00:00
|
|
|
|
http = new HttpClient();
|
|
|
|
|
http.BaseAddress = new Uri("http://www.adafruit.com/api/");
|
2021-08-07 22:20:46 +00:00
|
|
|
|
configuration = new Configuration();
|
|
|
|
|
// TODO: Implement config persistence.
|
|
|
|
|
// TODO: Implement product listing persisted cache.
|
|
|
|
|
LiveProductListingManager productListingManager = new LiveProductListingManager(http, loggerFactory.CreateLogger<LiveProductListingManager>(), configuration.MinDownloadInterval);
|
|
|
|
|
this.searchManager = new SearchManager(productListingManager, configuration.Similarity);
|
|
|
|
|
productListingManager.StartUpdateTimer(delay: 0);
|
|
|
|
|
|
|
|
|
|
logger = loggerFactory.CreateLogger<AdafruitShop>();
|
2021-07-20 22:51:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-07 22:20:46 +00:00
|
|
|
|
public Task<ProductListing> GetProductListingFromUrl(string url)
|
2021-07-20 22:51:43 +00:00
|
|
|
|
{
|
2021-08-07 22:20:46 +00:00
|
|
|
|
return searchManager.ProductListingManager.GetProductListingFromUrl(url);
|
2021-07-20 22:51:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-07 22:20:46 +00:00
|
|
|
|
public IAsyncEnumerable<ProductListing> Search(string query, Filters filters)
|
2021-07-20 22:51:43 +00:00
|
|
|
|
{
|
2021-08-05 06:22:19 +00:00
|
|
|
|
return searchManager.Search(query);
|
2021-07-20 22:51:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (!disposedValue)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
http.Dispose();
|
2021-08-05 06:22:19 +00:00
|
|
|
|
searchManager.Dispose();
|
2021-07-20 22:51:43 +00:00
|
|
|
|
}
|
|
|
|
|
disposedValue = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(disposing: true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|