public static ContactResponse ExportContact(NameValueCollection myNameValueCollection, string remoteHost)
        {
            using (WebClient myWebClient = new WebClient())
            {
                try
                {
                    RepairShoprUtils.LogWriteLineinHTML("Sending new Contact Parameter to RepairShopr ", MessageSource.Contact, "", messageType.Information);
                    string urls          = string.Format("https://{0}.{1}/api/v1/contacts.json?api_key={2}", LoginResponse.Subdomain.Trim(), remoteHost, LoginResponse.UserToken.Trim());
                    var    responseArray = myWebClient.UploadValues(urls, "POST", myNameValueCollection);
                    string jsonResult    = Encoding.ASCII.GetString(responseArray);
                    RepairShoprUtils.LogWriteLineinHTML(string.Format("Server Response for Contact : {0} ", jsonResult), MessageSource.Customer, "", messageType.Information);
                    var contactData = JsonConvert.DeserializeObject <ContactResponse>(jsonResult);

                    if (string.IsNullOrEmpty(contactData.Errors))
                    {
                        RepairShoprUtils.LogWriteLineinHTML("Successfully Created new Contact to RepairShopr ", MessageSource.Contact, "", messageType.Information);
                        return(contactData);
                    }
                    else
                    {
                        RepairShoprUtils.LogWriteLineinHTML("Failed to Create New Customer in RepairShopr. " + contactData.Errors, MessageSource.Contact, contactData.Record, messageType.Error);
                    }
                }
                catch (WebException ex)
                {
                    if (ex.Response != null)
                    {
                        if ((int)((HttpWebResponse)ex.Response).StatusCode == 429)
                        {
                            Task.Delay(TimeSpan.FromSeconds(5)).Wait();
                            return(ExportContact(myNameValueCollection, remoteHost));
                        }

                        using (Stream data = ex.Response.GetResponseStream())
                            using (var reader = new StreamReader(data))
                            {
                                string bodyText = reader.ReadToEnd();
                                RepairShoprUtils.LogWriteLineinHTML("Failed to Create New Contact in RepairShopr . " + bodyText, MessageSource.Contact, ex.Message, messageType.Error);
                            }
                    }
                }
                catch (Exception ex)
                {
                    RepairShoprUtils.LogWriteLineinHTML("Failed to Create New Contact in RepairShopr . " + ex.Message, MessageSource.Contact, ex.Message, messageType.Error);
                }
            }
            return(null);
        }
 public static LoginResponse GetLoginResquest(string username, string password, string remoteHost)
 {
     using (WebClient myWebClient = new WebClient())
     {
         try
         {
             NameValueCollection myNameValueCollection = new NameValueCollection();
             myNameValueCollection.Add("email", username);
             myNameValueCollection.Add("password", password);
             string url           = string.Format("https://admin.{0}/api/v1/sign_in", remoteHost);
             var    responseArray = myWebClient.UploadValues(url, "POST", myNameValueCollection);
             string jsonResult    = Encoding.ASCII.GetString(responseArray);
             LoginResponse = JsonConvert.DeserializeObject <LoginResponse>(jsonResult);
             if (LoginResponse != null)
             {
                 RepairShoprUtils.LogWriteLineinHTML("Successfully Login in RepairShopr", MessageSource.Login, "", messageType.Information);
                 return(LoginResponse);
             }
         }
         catch (WebException ex)
         {
             if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
             {
                 if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.Unauthorized)
                 {
                     RepairShoprUtils.LogWriteLineinHTML("Failed to Authenticate given Username and Password", MessageSource.Login, ex.Message, messageType.Error);
                 }
             }
             else
             {
                 RepairShoprUtils.LogWriteLineinHTML("Unable to Login in RepairShopr", MessageSource.Login, ex.Message, messageType.Error);
             }
         }
     }
     return(null);
 }
        public static Customer ExportCustomer(NameValueCollection myNameValueCollection, string remoteHost)
        {
            using (WebClient myWebClient = new WebClient())
            {
                try
                {
                    RepairShoprUtils.LogWriteLineinHTML("Sending new Customer Parameter to RepairShopr ", MessageSource.Customer, "", messageType.Information);
                    //string urls = "http://example.repairshopr.com/api/v1/customers.json?api_key=123123";
                    string urls          = string.Format("https://{0}.{1}/api/v1/customers.json?api_key={2}", LoginResponse.Subdomain.Trim(), remoteHost, LoginResponse.UserToken.Trim());
                    var    responseArray = myWebClient.UploadValues(urls, "POST", myNameValueCollection);

                    var jsonResult = Encoding.ASCII.GetString(responseArray);
                    RepairShoprUtils.LogWriteLineinHTML(string.Format("Server Response for Customer : {0} ", jsonResult), MessageSource.Customer, "", messageType.Information);

                    var res = JsonConvert.DeserializeObject <CustomerRoot>(jsonResult);
                    if (res.success == false && res.status != "unprocessable_entity" && res.customer == null)
                    {
                        var obj   = JObject.Parse(jsonResult);
                        var email = (string)obj["params"]["email"];
                        if (string.IsNullOrEmpty(email))
                        {
                            return(null);
                        }
                        string url          = string.Format("https://{0}.{1}/api/v1/customers/autocomplete?api_key={2}&query={3}", LoginResponse.Subdomain.Trim(), remoteHost, LoginResponse.UserToken.Trim(), email);
                        var    result       = myWebClient.DownloadString(url);
                        var    rootCustomer = JsonConvert.DeserializeObject <CustomerListRoot>(result);
                        if (rootCustomer != null)
                        {
                            if (rootCustomer.customers != null && rootCustomer.customers.Count > 0)
                            {
                                return(rootCustomer.customers[0]);
                            }
                        }
                        else
                        {
                            string bodyText = string.Join(" ", res.message);
                            RepairShoprUtils.LogWriteLineinHTML("Failed to Create New Customer in RepairShopr. " + bodyText, MessageSource.Customer, res.params_.email, messageType.Error);
                        }
                    }
                    else
                    {
                        var customerData = JsonConvert.DeserializeObject <CustomerRoot>(jsonResult);
                        if (customerData != null && customerData.customer != null)
                        {
                            return(customerData.customer);
                        }
                    }
                }
                catch (WebException ex)
                {
                    if (ex.Response != null)
                    {
                        if ((int)((HttpWebResponse)ex.Response).StatusCode == 429)
                        {
                            Task.Delay(TimeSpan.FromSeconds(5)).Wait();
                            return(ExportCustomer(myNameValueCollection, remoteHost));
                        }

                        using (Stream data = ex.Response.GetResponseStream())
                            using (var reader = new StreamReader(data))
                            {
                                string bodyText = reader.ReadToEnd();
                                RepairShoprUtils.LogWriteLineinHTML("Failed to Create New Customer in RepairShopr. " + bodyText, MessageSource.Customer, ex.Message, messageType.Error);
                            }
                    }
                }
                catch (Exception ex)
                {
                    RepairShoprUtils.LogWriteLineinHTML("Failed to Create New Customer in RepairShopr. " + ex.Message, MessageSource.Customer, ex.Message, messageType.Error);
                }
            }
            return(null);
        }