Esempio n. 1
0
        private async Task <ServicePointInformation> GetServicePointLiveAsync(ClientInfo clientInfo, string pickupPointId)
        {
            var parameters = new Dictionary <string, string>
            {
                { "apikey", clientInfo.ApiKey },
                { "countryCode", clientInfo.Country.ToString() },
                { "servicePointId", pickupPointId },
            };

            string url = $"businesslocation/v1/servicepoint/findByServicePointId.json?{BuildQueryString(parameters)}";

            HttpResponseMessage responseMessage = await CallAsync(() => Client.GetAsync(url), true);

            if (responseMessage == null)
            {
                _log.Error(new { message = "Service point fetch failed. Service response was NULL", pickupPointId });
                return(null);
            }

            if (!responseMessage.IsSuccessStatusCode)
            {
                _log.Error(new { message = "Service point fetch failed. Service response status was not OK", responseMessage.StatusCode });
                return(null);
            }

            ServicePointInformationRootDto dto = await ParseJsonAsync <ServicePointInformationRootDto>(responseMessage);

            if (dto.HasError || dto.ServicePointInformationResponse?.ServicePoints == null)
            {
                _log.Error(new { message = "Service point fetch failed.", pickupPointId, dto.ErrorMessage });
                return(null);
            }

            return(_mapper.Map <ServicePointInformation>(dto.ServicePointInformationResponse.ServicePoints.FirstOrDefault()));
        }
Esempio n. 2
0
        public async Task <ServicePointInformation[]> GetAllServicePointsAsync(ClientInfo clientInfo, bool forceCacheRefresh = false)
        {
            string cacheKey = $"ServicePointList_{clientInfo.ApiKey}";

            _log.Debug(new { message = "Get all service points", clientInfo, forceCacheRefresh });

            ServicePointInformation[] result;

            if (!forceCacheRefresh)
            {
                result = _cacheHelper.Get <ServicePointInformation[]>(cacheKey);
                if (result != null && result.Any())
                {
                    _log.Debug($"Found {result.Length} service points in cache");
                    return(result);
                }
            }

            var parameters = new Dictionary <string, string>
            {
                { "apikey", clientInfo.ApiKey },
                { "countryCode", clientInfo.Country.ToString() }
            };

            string url = $"businesslocation/v1/servicepoint/getServicePointInformation.json?{BuildQueryString(parameters)}";
            HttpResponseMessage responseMessage = await CallAsync(() => Client.GetAsync(url), true);

            if (responseMessage == null)
            {
                _log.Error("Get all service points failed. Service response was NULL");
                return(new ServicePointInformation[0]);
            }

            if (!responseMessage.IsSuccessStatusCode)
            {
                _log.Error(new { message = "Get all service points failed. Service response status was not OK", responseMessage.StatusCode });
                return(new ServicePointInformation[0]);
            }

            ServicePointInformationRootDto dto = await ParseJsonAsync <ServicePointInformationRootDto>(responseMessage);

            if (dto.HasError || dto.ServicePointInformationResponse?.ServicePoints == null)
            {
                _log.Error(new { message = "Get all service points failed. Service response was NULL" });
                return(new ServicePointInformation[0]);
            }

            result = _mapper.Map <ServicePointInformation[]>(dto.ServicePointInformationResponse.ServicePoints);
            _cacheHelper.Insert(cacheKey, result, clientInfo.CacheTimeout);
            _log.Debug($"Found {result.Length} service points by API call");
            return(result);
        }