Example #1
0
        public async Task <HttpResponseMessage> PostCustomer(Customer customer)
        {
            HttpResponseMessage response;
            HttpClient          httpClient = new HttpClient();
            string url          = "";
            string authToken    = "";
            string eventbaseurl = "";

            if (ConfigurationManager.AppSettings["IsTestingEnv"].ToString() == "true")
            {
                url          = ConfigurationManager.AppSettings["CutomerAddUrl"].ToString();
                authToken    = ConfigurationManager.AppSettings["authToken"].ToString();
                eventbaseurl = ConfigurationManager.AppSettings["EventApiUrl"].ToString();
            }
            else
            {
                url          = Environment.GetEnvironmentVariable("CutomerAddUrl").ToString();
                authToken    = Environment.GetEnvironmentVariable("authToken");
                eventbaseurl = Environment.GetEnvironmentVariable("EventApiUrl");
            }
            try
            {
                if (Request.Headers.Contains("transactionID"))
                {
                    customer.TransactionID = Request.Headers.GetValues("transactionID").FirstOrDefault();
                }
                if (Request.Headers.Contains("agentID"))
                {
                    customer.AgentId = Request.Headers.GetValues("agentID").FirstOrDefault();
                }
                customer.id = GenerateCustomerId.Generate(9);
                var content       = JsonConvert.SerializeObject(customer);
                var stringContent = new StringContent(content, UnicodeEncoding.UTF8, "application/json");
                var result        = httpClient.PostAsync(url, stringContent);
                response = result.Result;
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    //HttpClient eventHttpClient = new HttpClient();
                    //var eventurl= eventbaseurl+"?customerId=" +customer.id;
                    //eventHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
                    //var eventresponse=eventHttpClient.GetAsync(eventurl);
                    //eventresponse.Wait();
                    //response = Request.CreateResponse(HttpStatusCode.OK, "Customer details entered succesfully");
                    await ServiceBusHandler.Initialize(JsonConvert.SerializeObject(customer));

                    await CustomerEventRaiser.RaiseNewCustomerEvent(customer.id);
                }
                return(response);
            }
            catch (Exception ex)
            {
                response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
                return(response);
            }
        }
        public HttpResponseMessage PostCustomer(Customer customer)
        {
            HttpResponseMessage response;
            HttpClient          httpClient = new HttpClient();
            var url = Environment.GetEnvironmentVariable("CutomerAddUrl").ToString();

            try
            {
                if (Request.Headers.Contains("transactionID"))
                {
                    customer.TransactionID = Request.Headers.GetValues("transactionID").FirstOrDefault();
                }
                if (Request.Headers.Contains("agentID"))
                {
                    customer.AgentID = Request.Headers.GetValues("agentID").FirstOrDefault();
                }
                customer.id = GenerateCustomerId.Generate(9);
                var content       = JsonConvert.SerializeObject(customer);
                var stringContent = new StringContent(content, UnicodeEncoding.UTF8, "application/json");
                var result        = httpClient.PostAsync(url, stringContent);
                response = result.Result;
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    HttpClient eventHttpClient = new HttpClient();
                    var        eventurl        = Environment.GetEnvironmentVariable("EventApiUrl") + "?customerId=" + customer.id;
                    eventHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("authToken"));
                    var eventresponse = eventHttpClient.GetAsync(eventurl);
                    eventresponse.Wait();
                    response = Request.CreateResponse(HttpStatusCode.OK, "Customer details entered succesfully");
                }
                return(response);
            }
            catch (Exception ex)
            {
                response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
                return(response);
            }
        }