protected override async Task <IEnumerable <IDocument> > ExecuteInputAsync(IDocument input, IExecutionContext context) { if (input.ContainsKey(SiteKeys.Location) && !input.ContainsKey(SiteKeys.Lat) && !input.ContainsKey(SiteKeys.Lon)) { string location = input.GetString(SiteKeys.Location); if (!location.IsNullOrWhiteSpace()) { CoordinateAbbreviated coordinates = await _coordinateCache.GetOrAdd(location, async _ => { context.LogInformation($"Geocoding location {location} for {input.ToSafeDisplayString()}"); string subscriptionKey = await _subscriptionKey.GetValueAsync(input, context); if (!subscriptionKey.IsNullOrWhiteSpace()) { using (HttpClient client = context.CreateHttpClient()) { HttpResponseMessage responseMessage = await client.SendWithRetryAsync($"https://atlas.microsoft.com/search/address/json?&subscription-key={subscriptionKey}&api-version=1.0&language=en-US&limit=1&query={HttpUtility.UrlEncode(location)}"); if (responseMessage.IsSuccessStatusCode) { SearchAddressResponse searchAddressResponse; using (Stream responseStream = await responseMessage.Content.ReadAsStreamAsync()) { searchAddressResponse = await JsonSerializer.DeserializeAsync <SearchAddressResponse>(responseStream, DefaultJsonSerializerOptions); } if (searchAddressResponse.Results.Length > 0) { return(searchAddressResponse.Results[0].Position); } else { context.LogWarning($"No results while geocoding location {location} for {input.ToSafeDisplayString()}"); } } else { context.LogWarning($"Error {responseMessage.StatusCode} while geocoding location {location} for {input.ToSafeDisplayString()}"); } } } return(null); }); if (coordinates is object) { return(input .Clone(new MetadataItems { { SiteKeys.Lat, coordinates.Lat }, { SiteKeys.Lon, coordinates.Lon } }) .Yield()); } } } return(input.Yield()); }
public EventService Create(ApplicationContext context) { var myProfile = new AutoMapping(); var configuration = new MapperConfiguration(cfg => cfg.AddProfile(myProfile)); var mapper = new Mapper(configuration); var appSettings = Options.Create(new AppSettings() { Secret = "this is a secret" }); var distributedCacheMock = new Mock <IDistributedCache>(); var analyticsServiceMock = new Mock <IAnalyticsService>(); analyticsServiceMock.Setup(x => x.CaptureSearchAsync(CancellationToken.None, null, null, new Guid(), new DateTime())); analyticsServiceMock.Setup( x => x.CapturePageViewAsync(CancellationToken.None, new Guid(), new Guid(), new DateTime())); var workQueueMock = new Mock <IWorkQueue>(); workQueueMock.Setup(x => x.QueueWork(new Mock <Func <CancellationToken, Task> >().Object)); var recommendationServiceMock = new Mock <IRecommendationService>(); var popularityServiceMock = new Mock <IPopularityService>(); var addressResponse = new SearchAddressResponse() { Results = new SearchAddressResult[] { new() { Position = new CoordinateAbbreviated() { Lat = 0, Lon = 0 } } } }; var mapResponse = new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonSerializer.Serialize(addressResponse)) }; mapResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); httpMessageHandlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()).ReturnsAsync(mapResponse); var httpClientMock = new HttpClient(httpMessageHandlerMock.Object); return(new EventService( context, new Mock <ILogger <EventService> >().Object, mapper, httpClientMock, appSettings, analyticsServiceMock.Object, recommendationServiceMock.Object, distributedCacheMock.Object, workQueueMock.Object, popularityServiceMock.Object)); }