public async Task <ArtistWorkInfo> GetArtistWorkAsync(int id)
        {
            string         localStorageKey = LOCALSTORAGEKEY_ARTISTWORK_INFO + id;
            ArtistWorkInfo result          = await _localStorageService.GetItemAsync <ArtistWorkInfo>(localStorageKey);

            if (result == null)
            {
                ArtistWorkInfosRequest request = new ArtistWorkInfosRequest()
                {
                    ArtistWorkIds = new List <int>()
                    {
                        id
                    }
                };

                HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync(_endpointAddress + "ArtistWorkInfos", request);

                string responseBody = await responseMessage.Content.ReadAsStringAsync();

                ArtistWorkInfoSource temp = JsonConvert.DeserializeObject <List <ArtistWorkInfoSource> >(responseBody)?.FirstOrDefault();
                result = new ArtistWorkInfo()
                {
                    ArtistInfo = await _radiocomArtistRepository.GetArtistAsync(temp.ArtistId),
                    Id         = temp.Id,
                    Name       = temp.Title
                };
                await _localStorageService.SetItemAsync <ArtistWorkInfo>(localStorageKey, result);
            }
            return(result);
        }
Example #2
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            ArtistWorkInfosRequest        data     = JsonConvert.DeserializeObject <ArtistWorkInfosRequest>(requestBody);
            IEnumerable <IArtistWorkInfo> response = await _artistWorkInfosRequestEngine.ProcessArtistWorkInfosRequest(data);



            return(new OkObjectResult(response));
        }
        public async Task <IEnumerable <ArtistWorkInfo> > GetArtistWorksAsync()
        {
            string localStorageKey = LOCALSTORAGEKEY_ARTISTWORK_INFO + "all";
            List <ArtistWorkInfo> result;
            TimeCachedObject <List <ArtistWorkInfo> > cachedObject = await _localStorageService.GetItemAsync <TimeCachedObject <List <ArtistWorkInfo> > >(localStorageKey);

            if (cachedObject == null || cachedObject.NextUpdateHour < DateTimeOffset.UtcNow)
            {
                ArtistWorkInfosRequest request = new ArtistWorkInfosRequest()
                {
                    ArtistWorkIds = Enumerable.Empty <int>()
                };
                HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync(_endpointAddress + "ArtistWorkInfos", request);

                string responseBody = await responseMessage.Content.ReadAsStringAsync();

                List <ArtistWorkInfoSource> temp    = JsonConvert.DeserializeObject <List <ArtistWorkInfoSource> >(responseBody);
                IEnumerable <ArtistInfo>    artists = await _radiocomArtistRepository.GetArtistsAsync();

                result = artists.Join(temp, x => x.Id, y => y.ArtistId, (x, y) => new ArtistWorkInfo()
                {
                    ArtistInfo = x, Id = y.Id, Name = y.Title
                }).ToList();
                DateTimeOffset nextUpdate = TimeCachedObject <object> .CalculateNextUpdateHour();

                cachedObject = new TimeCachedObject <List <ArtistWorkInfo> >()
                {
                    CachedObject   = result,
                    NextUpdateHour = nextUpdate
                };
                await _localStorageService.SetItemAsync(localStorageKey, cachedObject);
            }
            else
            {
                result = cachedObject.CachedObject;
            }
            return(result);
        }