public Tier CreateTier(Tier tierRequest)
        {
            const PlatformLoyaltyServiceRequestType requestType = PlatformLoyaltyServiceRequestType.CreateTier;
            const string method      = Constants.Post;
            var          createdTier = _platformLoyaltyServiceApi.TierResponse(tierRequest, requestType, method);

            return(createdTier);
        }
        public Reward CreateReward(Reward rewardRequest)
        {
            const PlatformLoyaltyServiceRequestType requestType = PlatformLoyaltyServiceRequestType.CreateReward;
            const string method        = Constants.Post;
            var          createdReward = _platformLoyaltyServiceApi.RewardResponse(rewardRequest, requestType, method);

            return(createdReward);
        }
        public LoyaltyProgram UpdateLoyaltyById(LoyaltyProgram loyaltyRequest)
        {
            const PlatformLoyaltyServiceRequestType requestType = PlatformLoyaltyServiceRequestType.UpdateLoyaltyById;
            const string method = Constants.Put;
            var          updatedLoyaltyResponse = _platformLoyaltyServiceApi.LoyaltyProgramResponse(loyaltyRequest, requestType, method);

            return(updatedLoyaltyResponse);
        }
        public LoyaltyProgram CreateLoyalty(LoyaltyProgram loyaltyRequest)
        {
            const PlatformLoyaltyServiceRequestType requestType = PlatformLoyaltyServiceRequestType.CreateLoyalty;
            const string method       = Constants.Post;
            var          savedLoyalty = _platformLoyaltyServiceApi.LoyaltyProgramResponse(loyaltyRequest, requestType, method);

            return(savedLoyalty);
        }
        public Tier UpdateTierById(Tier tierRequest)
        {
            const PlatformLoyaltyServiceRequestType requestType = PlatformLoyaltyServiceRequestType.UpdateTierById;
            const string method      = Constants.Put;
            var          updatedTier = _platformLoyaltyServiceApi.TierResponse(tierRequest, requestType, method);

            return(updatedTier);
        }
        public Reward UpdateRewardById(Reward rewardRequest)
        {
            const PlatformLoyaltyServiceRequestType requestType = PlatformLoyaltyServiceRequestType.UpdateRewardById;
            const string method        = Constants.Put;
            var          updatedReward = _platformLoyaltyServiceApi.RewardResponse(rewardRequest, requestType, method);

            return(updatedReward);
        }
        public LoyaltyPrograms GetAllLoyalties()
        {
            const PlatformLoyaltyServiceRequestType requestType = PlatformLoyaltyServiceRequestType.AllLoyalties;
            const string method        = Constants.Get;
            var          commonRequest = new PlatformLoyaltyServiceRequest();
            var          allLoyalties  = _platformLoyaltyServiceApi.LoyaltyProgramsResponse(requestType, commonRequest, method);

            return(allLoyalties);
        }
        public Users GetUsers()
        {
            const PlatformLoyaltyServiceRequestType requestType = PlatformLoyaltyServiceRequestType.AllUsers;
            const string method        = Constants.Get;
            var          commonRequest = new PlatformLoyaltyServiceRequest();
            var          allUsers      = _platformLoyaltyServiceApi.UsersResponse(requestType, commonRequest, method);

            return(allUsers);
        }
        public Tier TierResponse(Tier tierRequest, PlatformLoyaltyServiceRequestType requestType, string method)
        {
            var commonRequest = new PlatformLoyaltyServiceRequest {
                Tier = tierRequest
            };
            var tier = TierResponse(requestType, commonRequest, method);

            return(tier);
        }
        public Reward RewardResponse(Reward rewardRequest, PlatformLoyaltyServiceRequestType requestType, string method)
        {
            var commonRequest = new PlatformLoyaltyServiceRequest {
                Reward = rewardRequest
            };
            var reward = RewardResponse(requestType, commonRequest, method);

            return(reward);
        }
        public Tier TierResponse(PlatformLoyaltyServiceRequestType requestType,
                                 PlatformLoyaltyServiceRequest commonRequest, string method)
        {
            var responseMessage = HttpResponseMessage(requestType, commonRequest, method);
            var tier            = responseMessage.IsSuccessStatusCode
                ? GenericResponse <Tier>(requestType, responseMessage)
                : throw new Exception((int)responseMessage.StatusCode + "-" + responseMessage.StatusCode);

            return(tier);
        }
        public LoyaltyProgram LoyaltyProgramResponse(LoyaltyProgram loyaltyRequest,
                                                     PlatformLoyaltyServiceRequestType requestType, string method)
        {
            var commonRequest = new PlatformLoyaltyServiceRequest {
                Loyalty = loyaltyRequest
            };
            var loyaltyProgram = LoyaltyProgramResponse(requestType, commonRequest, method);

            return(loyaltyProgram);
        }
        public LoyaltyProgram LoyaltyProgramResponse(PlatformLoyaltyServiceRequestType requestType,
                                                     PlatformLoyaltyServiceRequest commonRequest, string method)
        {
            var responseMessage = HttpResponseMessage(requestType, commonRequest, method);
            var loyaltyProgram  = responseMessage.IsSuccessStatusCode
                ? GenericResponse <LoyaltyProgram>(requestType, responseMessage)
                : throw new Exception((int)responseMessage.StatusCode + "-" + responseMessage.StatusCode);

            return(loyaltyProgram);
        }
        public Rewards RewardsResponse(PlatformLoyaltyServiceRequestType requestType,
                                       PlatformLoyaltyServiceRequest commonRequest, string method)
        {
            var response = HttpResponseMessage(requestType, commonRequest, method);

            var allRewards = response.IsSuccessStatusCode
                ? GenericResponse <Rewards>(requestType, response)
                : throw new Exception((int)response.StatusCode + "-" + response.StatusCode);

            return(allRewards);
        }
        public LoyaltyProgram GetLoyaltyById(string loyaltyId)
        {
            const PlatformLoyaltyServiceRequestType requestType = PlatformLoyaltyServiceRequestType.GetLoyaltyById;
            const string method        = Constants.Get;
            var          commonRequest = new PlatformLoyaltyServiceRequest {
                Loyalty = new LoyaltyProgram {
                    Id = loyaltyId
                }
            };
            var loyaltyByIdResponse = _platformLoyaltyServiceApi.LoyaltyProgramResponse(requestType, commonRequest, method);

            return(loyaltyByIdResponse);
        }
        public Tier GetTierById(string loyaltyId, string tierId)
        {
            const PlatformLoyaltyServiceRequestType requestType = PlatformLoyaltyServiceRequestType.GetTierById;
            const string method        = Constants.Get;
            var          commonRequest = new PlatformLoyaltyServiceRequest {
                Tier = new Tier {
                    Id = tierId, ProgramId = loyaltyId
                }
            };
            var tier = _platformLoyaltyServiceApi.TierResponse(requestType, commonRequest, method);

            return(tier);
        }
        public Reward GetRewardById(string loyaltyId, string rewardId)
        {
            const PlatformLoyaltyServiceRequestType requestType = PlatformLoyaltyServiceRequestType.GetRewardById;
            const string method        = Constants.Get;
            var          commonRequest = new PlatformLoyaltyServiceRequest {
                Reward = new Reward {
                    Id = rewardId, ProgramId = loyaltyId
                }
            };
            var reward = _platformLoyaltyServiceApi.RewardResponse(requestType, commonRequest, method);

            return(reward);
        }
        public Rewards GetAllRewards(string loyaltyId)
        {
            const PlatformLoyaltyServiceRequestType requestType = PlatformLoyaltyServiceRequestType.AllRewards;
            const string method        = Constants.Get;
            var          commonRequest = new PlatformLoyaltyServiceRequest {
                Loyalty = new LoyaltyProgram {
                    Id = loyaltyId
                }
            };
            var allRewards = _platformLoyaltyServiceApi.RewardsResponse(requestType, commonRequest, method);

            return(allRewards);
        }
        private T GenericResponse <T>(PlatformLoyaltyServiceRequestType requestType,
                                      HttpResponseMessage httpResponseMessage)
        {
            var data = httpResponseMessage.Content.ReadAsStringAsync().Result;
            T   genericType;

            switch (requestType)
            {
            case PlatformLoyaltyServiceRequestType.AllLoyalties:
                genericType = (T)JsonConvert.DeserializeObject(data, typeof(LoyaltyPrograms));
                break;

            case PlatformLoyaltyServiceRequestType.CreateLoyalty:
            case PlatformLoyaltyServiceRequestType.UpdateLoyaltyById:
            case PlatformLoyaltyServiceRequestType.GetLoyaltyById:
                genericType = (T)JsonConvert.DeserializeObject(data, typeof(LoyaltyProgram));
                break;

            case PlatformLoyaltyServiceRequestType.AllTiers:
                genericType = (T)JsonConvert.DeserializeObject(data, typeof(Tiers));
                break;

            case PlatformLoyaltyServiceRequestType.CreateTier:
            case PlatformLoyaltyServiceRequestType.UpdateTierById:
            case PlatformLoyaltyServiceRequestType.GetTierById:
                genericType = (T)JsonConvert.DeserializeObject(data, typeof(Tier));
                break;

            case PlatformLoyaltyServiceRequestType.AllRewards:
                genericType = (T)JsonConvert.DeserializeObject(data, typeof(Rewards));
                break;

            case PlatformLoyaltyServiceRequestType.CreateReward:
            case PlatformLoyaltyServiceRequestType.UpdateRewardById:
            case PlatformLoyaltyServiceRequestType.GetRewardById:
                genericType = (T)JsonConvert.DeserializeObject(data, typeof(Reward));
                break;

            case PlatformLoyaltyServiceRequestType.AllUsers:
                genericType = (T)JsonConvert.DeserializeObject(data, typeof(Users));
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(requestType), requestType, null);
            }

            return(genericType);
        }
        /// <summary>
        ///     Create URL dynamically.
        /// </summary>
        public static UriBuilder GetUrl(PlatformLoyaltyServiceRequestType requestType, string loyaltyId = null, string id = null)
        {
            var uriBuilder = GetBaseUri();

            switch (requestType)
            {
            case PlatformLoyaltyServiceRequestType.AllLoyalties:
            case PlatformLoyaltyServiceRequestType.CreateLoyalty:
                uriBuilder.Path += Constants.Loyalty;
                break;

            case PlatformLoyaltyServiceRequestType.UpdateLoyaltyById:
            case PlatformLoyaltyServiceRequestType.GetLoyaltyById:
                uriBuilder.Path += Constants.Loyalty + loyaltyId;
                break;

            case PlatformLoyaltyServiceRequestType.AllTiers:
            case PlatformLoyaltyServiceRequestType.CreateTier:
                uriBuilder.Path += Constants.Loyalty + loyaltyId + Constants.Tier;
                break;

            case PlatformLoyaltyServiceRequestType.GetTierById:
            case PlatformLoyaltyServiceRequestType.UpdateTierById:
                uriBuilder.Path += Constants.Loyalty + loyaltyId + Constants.Tier + id;
                break;

            case PlatformLoyaltyServiceRequestType.AllRewards:
            case PlatformLoyaltyServiceRequestType.CreateReward:
                uriBuilder.Path += Constants.Loyalty + loyaltyId + Constants.Reward;
                break;

            case PlatformLoyaltyServiceRequestType.GetRewardById:
            case PlatformLoyaltyServiceRequestType.UpdateRewardById:
                uriBuilder.Path += Constants.Loyalty + loyaltyId + Constants.Reward + id;
                break;

            case PlatformLoyaltyServiceRequestType.AllUsers:
                uriBuilder.Path += Constants.Users;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(requestType), requestType, null);
            }

            return(uriBuilder);
        }
        private static HttpResponseMessage HttpResponseMessage(PlatformLoyaltyServiceRequestType requestType,
                                                               PlatformLoyaltyServiceRequest request, string method)
        {
            UriBuilder    uri;
            StringContent content = null;

            switch (requestType)
            {
            case PlatformLoyaltyServiceRequestType.AllLoyalties:
                uri = CreateUrl.GetUrl(requestType);
                break;

            case PlatformLoyaltyServiceRequestType.GetLoyaltyById:
                uri = CreateUrl.GetUrl(requestType, request.Loyalty.Id);
                break;

            case PlatformLoyaltyServiceRequestType.CreateLoyalty:
                uri     = CreateUrl.GetUrl(requestType);
                content = HttpClientFactory.JsonContentFactory.CreateJsonContent(request.Loyalty);
                break;

            case PlatformLoyaltyServiceRequestType.UpdateLoyaltyById:
                uri     = CreateUrl.GetUrl(requestType, request.Loyalty.Id);
                content = HttpClientFactory.JsonContentFactory.CreateJsonContent(request.Loyalty);
                break;

            case PlatformLoyaltyServiceRequestType.AllTiers:
                uri = CreateUrl.GetUrl(requestType, request.Loyalty.Id);
                break;

            case PlatformLoyaltyServiceRequestType.GetTierById:
                uri = CreateUrl.GetUrl(requestType, request.Tier.ProgramId, request.Tier.Id);
                break;

            case PlatformLoyaltyServiceRequestType.CreateTier:
                uri     = CreateUrl.GetUrl(requestType, request.Tier.ProgramId);
                content = HttpClientFactory.JsonContentFactory.CreateJsonContent(request.Tier);
                break;

            case PlatformLoyaltyServiceRequestType.UpdateTierById:
                uri     = CreateUrl.GetUrl(requestType, request.Tier.ProgramId, request.Tier.Id);
                content = HttpClientFactory.JsonContentFactory.CreateJsonContent(request.Tier);
                break;

            case PlatformLoyaltyServiceRequestType.AllRewards:
                uri = CreateUrl.GetUrl(requestType, request.Loyalty.Id);
                break;

            case PlatformLoyaltyServiceRequestType.GetRewardById:
                uri = CreateUrl.GetUrl(requestType, request.Reward.ProgramId, request.Reward.Id);
                break;

            case PlatformLoyaltyServiceRequestType.CreateReward:
                uri     = CreateUrl.GetUrl(requestType, request.Reward.ProgramId);
                content = HttpClientFactory.JsonContentFactory.CreateJsonContent(request.Reward);
                break;

            case PlatformLoyaltyServiceRequestType.UpdateRewardById:
                uri     = CreateUrl.GetUrl(requestType, request.Reward.ProgramId, request.Reward.Id);
                content = HttpClientFactory.JsonContentFactory.CreateJsonContent(request.Reward);
                break;

            case PlatformLoyaltyServiceRequestType.AllUsers:
                uri = CreateUrl.GetUrl(requestType);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(requestType), requestType, null);
            }

            var responseMessage = HttpClientFactory.PerformRequest(method, uri, content, GetHttpClient());

            return(responseMessage);
        }