コード例 #1
0
        /// <summary>
        /// Gets the stories.
        /// </summary>
        /// <param name="storyParametersRequest">The story parameters request.</param>
        /// <returns></returns>
        //public async Task<APIResponse> GetStories(StoryParametersRequest storyParametersRequest)
        //{
        //    try
        //    {
        //        string serializedStories;

        //        List<StoryResponse> stories;

        //        var encodedStories = await distributedCache.GetAsync(BlogServiceOperation.GetStoriesCacheName);

        //        if (encodedStories != null)
        //        {
        //            serializedStories = Encoding.UTF8.GetString(encodedStories);
        //            stories = JsonConvert.DeserializeObject<List<StoryResponse>>(serializedStories);
        //        }
        //        else
        //        {
        //            var client = httpClientFactory.CreateClient(BlogServiceOperation.serviceName);

        //            UriBuilder url = new UriBuilder(servicesConfig.Blog + BlogServiceOperation.GetStories());
        //            url.Query = QueryStringHelper.ConvertToQueryString(storyParametersRequest);

        //            var response = await client.GetAsync(url.ToString());
        //            stories = JsonConvert.DeserializeObject<List<StoryResponse>>(await response.Content.ReadAsStringAsync());

        //            serializedStories = JsonConvert.SerializeObject(stories);
        //            encodedStories = Encoding.UTF8.GetBytes(serializedStories);
        //            var options = new DistributedCacheEntryOptions()
        //                            .SetSlidingExpiration(TimeSpan.FromMinutes(1))
        //                            .SetAbsoluteExpiration(DateTime.Now.AddHours(1));

        //            await distributedCache.SetAsync(BlogServiceOperation.GetStoriesCacheName, encodedStories, options);
        //        }

        //        return new APIResponse(stories, HttpStatusCode.OK);
        //    }
        //    catch (Exception ex)
        //    {
        //        logger.Error(ex, "Exception in method 'GetStories()'");
        //        var exMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
        //        return new APIResponse(exMessage, HttpStatusCode.InternalServerError);
        //    }
        //}
        public async Task <APIResponse> GetStories(StoryParametersRequest storyParametersRequest)
        {
            try
            {
                var client = httpClientFactory.CreateClient(BlogServiceOperation.serviceName);

                UriBuilder url = new UriBuilder(servicesConfig.Blog + BlogServiceOperation.GetStories());
                url.Query = QueryStringHelper.ConvertToQueryString(storyParametersRequest);

                var response = await client.GetAsync(url.ToString());

                if (response.IsSuccessStatusCode)
                {
                    var stories = JsonConvert.DeserializeObject <List <StoryResponse> >(await response.Content.ReadAsStringAsync());
                    foreach (var item1 in stories)
                    {
                        byte[] b = System.IO.File.ReadAllBytes(item1.CoverPhotoOrVideo);
                        item1.CoverPhotoOrVideo = Convert.ToBase64String(b);
                    }
                    return(new APIResponse(stories, HttpStatusCode.OK));
                }

                return(new APIResponse(response.StatusCode));
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Exception in method 'GetStories()'");
                var exMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                return(new APIResponse(exMessage, HttpStatusCode.InternalServerError));
            }
        }
コード例 #2
0
        /// <summary>
        /// Gets the stories.
        /// </summary>
        /// <param name="storyParametersRequest">The story parameters request.</param>
        /// <returns></returns>
        public async Task <APIResponse> GetStories(StoryParametersRequest storyParametersRequest)
        {
            try
            {
                string serializedStories;
                List <StoryResponse> stories;

                var encodedStories = await distributedCache.GetAsync(BlogServiceOperation.GetStoriesCacheName);

                if (encodedStories != null)
                {
                    serializedStories = Encoding.UTF8.GetString(encodedStories);
                    stories           = JsonConvert.DeserializeObject <List <StoryResponse> >(serializedStories);
                }
                else
                {
                    var client = httpClientFactory.CreateClient(BlogServiceOperation.serviceName);

                    UriBuilder url = new UriBuilder(servicesConfig.Blog + BlogServiceOperation.GetStories());
                    url.Query = QueryStringHelper.ConvertToQueryString(storyParametersRequest);

                    var response = await client.GetAsync(url.ToString());

                    stories = JsonConvert.DeserializeObject <List <StoryResponse> >(await response.Content.ReadAsStringAsync());

                    serializedStories = JsonConvert.SerializeObject(stories);
                    encodedStories    = Encoding.UTF8.GetBytes(serializedStories);
                    var options = new DistributedCacheEntryOptions()
                                  .SetSlidingExpiration(TimeSpan.FromMinutes(1))
                                  .SetAbsoluteExpiration(DateTime.Now.AddHours(1));

                    await distributedCache.SetAsync(BlogServiceOperation.GetStoriesCacheName, encodedStories, options);
                }

                return(new APIResponse(stories, HttpStatusCode.OK));
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Exception in method 'GetStories()'");
                var exMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                return(new APIResponse(exMessage, HttpStatusCode.InternalServerError));
            }
        }