Exemple #1
0
        public async Task <IEnumerable <Employee> > GetAllAsync()
        {
            var requestString = $"all";

            if (CacheEnabled)
            {
                var allEmployess = distributedCache.Get(allEmployessKey);
                if (allEmployess != null)
                {
                    return(JsonMapper.EntityFromJson <IEnumerable <Employee> >(Encoding.UTF8.GetString(allEmployess)));
                }
                else
                {
                    var requestToCache = await this.DoHttpRequest(requestString);

                    if (!string.IsNullOrEmpty(requestToCache))
                    {
                        distributedCache.Set(allEmployessKey, Encoding.UTF8.GetBytes(requestToCache), options);
                    }
                    return(String.IsNullOrEmpty(requestToCache) ? null : JsonMapper.EntityFromJson <IEnumerable <Employee> >(requestToCache));
                }
            }


            var request = await this.DoHttpRequest(requestString);

            return(String.IsNullOrEmpty(request) ? null : JsonMapper.EntityFromJson <IEnumerable <Employee> >(request));
        }
Exemple #2
0
        public async Task <Employee> GetAsync(string username)
        {
            if (String.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException(nameof(username));
            }

/*
 *
 *          var requestString = $"{this.configuration.HRS}/?username={username}";
 *          var request = await this.DoHttpRequest(requestString);
 *          return String.IsNullOrEmpty(request) ? null : JsonMapper.EntityFromJson<IEnumerable<Employee>>(request).FirstOrDefault();
 *      }
 *
 *      public async Task<IEnumerable<Employee>> GetAllAsync()
 *      {
 *          var requestString = $"{this.configuration.Staffing}";
 *          var request = await this.DoHttpRequest(requestString);
 *          return String.IsNullOrEmpty(request) ? null : JsonMapper.EntityFromJson<IEnumerable<Employee>>(request);
 *      }
 *
 *      private async Task<string> DoHttpRequest(string uri)
 *      {
 *          var httpClient = new HttpClient();
 *          var response = await httpClient.GetAsync(uri);
 *          response.EnsureSuccessStatusCode();
 *          return await response.Content.ReadAsStringAsync();
 *      }
 */
            var requestString = $"?username={username}";
            var key           = $"{cacheKey}:{username}";

            if (CacheEnabled)
            {
                var employee = distributedCache.Get(key);
                if (employee != null)
                {
                    return(JsonMapper.EntityFromJson <IEnumerable <Employee> >(Encoding.UTF8.GetString(employee)).FirstOrDefault());
                }
                else
                {
                    var requestToCache = await this.DoHttpRequest(requestString);

                    if (!String.IsNullOrEmpty(requestToCache))
                    {
                        distributedCache.Set(key, Encoding.UTF8.GetBytes(requestToCache), options);
                    }
                    return(String.IsNullOrEmpty(requestToCache) ? null : JsonMapper.EntityFromJson <IEnumerable <Employee> >(requestToCache).FirstOrDefault());
                }
            }

            var request = await this.DoHttpRequest(requestString);

            return(String.IsNullOrEmpty(request) ? null : JsonMapper.EntityFromJson <IEnumerable <Employee> >(request).FirstOrDefault());
        }