private static async Task <IActionResult> Run_Impl( API.ScrapSellerRequest request, ILogger log, ExecutionContext context ) { ConfigurationUtils.Initialize(context, log); if (!(request?.IsValid ?? false)) { return(new BadRequestObjectResult($"Please pass a valid {nameof(API.ScrapSellerRequest)} object")); } try { var response = await API.ScrapSeller.RunAsync(request); log.LogDebug(JsonConvert.SerializeObject(response)); return((ActionResult) new OkObjectResult(response)); } catch (Exception e) { log.LogError(e.ToString()); return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } }
public static async Task <ScrapSellerResponse> RunAsync(ScrapSellerRequest request) { return(await RunAsync( request, GetLocalCacheKey, GetLocalCacheAsync, SetLocalCacheAsync )); }
public static async Task <ScrapSellerResponse> RunAsync( ScrapSellerRequest request, GetCacheKey getCacheKey, GetCacheAsync getCache, SetCacheAsync setCache ) { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (!request.IsValid) { throw new ArgumentException(nameof(request)); } var scrapper = ScrapSellerFactory.CreateInstance(request.MarketPlaceID); if (scrapper == null) { throw new NotSupportedException(request.MarketPlaceID); } string content = await getCache?.Invoke(getCacheKey?.Invoke(request)); if (string.IsNullOrEmpty(content)) { content = await WebUtils.GetWebPageAsync(scrapper.GetPageUri(request.SellerID)); await setCache?.Invoke(getCacheKey?.Invoke(request), content); } var properties = scrapper.GetProperties(content); return(new ScrapSellerResponse(request.SellerID, properties)); }
private static string GetLocalCacheKey(ScrapSellerRequest request) => $"seller_{HashUtils.ToMD5(request.SellerID)}.html";