public BulkImportResults Import(List <SubscriberDetail> subscribers, bool resubscribe)
        {
            List <object> reworkedSusbcribers = new List <object>();
            string        json = "";

            foreach (SubscriberDetail subscriber in subscribers)
            {
                Dictionary <string, object> subscriberWithoutDate = new Dictionary <string, object>()
                {
                    { "EmailAddress", subscriber.EmailAddress }, { "Name", subscriber.Name }, { "CustomFields", subscriber.CustomFields }
                };
                reworkedSusbcribers.Add(subscriberWithoutDate);
            }

            try
            {
                json = HttpHelper.Post(string.Format("/subscribers/{0}/import.json", _listID), null, JavaScriptConvert.SerializeObject(
                                           new Dictionary <string, object>()
                {
                    { "Subscribers", reworkedSusbcribers }, { "Resubscribe", resubscribe }
                }
                                           ));
            }
            catch (CreatesendException ex)
            {
                if (!ex.Data.Contains("ErrorResult") && ex.Data.Contains("ErrorResponse"))
                {
                    ErrorResult <BulkImportResults> result = JavaScriptConvert.DeserializeObject <ErrorResult <BulkImportResults> >(ex.Data["ErrorResponse"].ToString());
                    ex.Data.Add("ErrorResult", result);
                }
                else if (ex.Data.Contains("ErrorResult"))
                {
                    ErrorResult <BulkImportResults> result = new ErrorResult <BulkImportResults>((ErrorResult)ex.Data["ErrorResult"]);
                    ex.Data["ErrorResult"] = result;
                }

                throw ex;
            }
            catch (Exception ex) { throw ex; }

            return(JavaScriptConvert.DeserializeObject <BulkImportResults>(json));
        }
Exemple #2
0
        public bool TestWebhook(string webhookID)
        {
            try
            {
                HttpHelper.Get(string.Format("/lists/{0}/webhooks/{1}/test.json", _listID, System.Web.HttpUtility.UrlEncode(webhookID)), null);
            }
            catch (CreatesendException ex)
            {
                if (!ex.Data.Contains("ErrorResult") && ex.Data.Contains("ErrorResponse"))
                {
                    ErrorResult <WebhookTestErrorResult> result = JavaScriptConvert.DeserializeObject <ErrorResult <WebhookTestErrorResult> >(ex.Data["ErrorResponse"].ToString());
                    ex.Data.Add("ErrorResult", result);
                }

                throw ex;
            }
            catch (Exception ex) { throw ex; }

            return(true); //an exception will be thrown if there is a problem
        }
Exemple #3
0
        private static Exception ThrowReworkedCustomException <EX>(HttpResponseMessage messageResponse) where EX : ErrorResult
        {
            using (System.IO.StreamReader sr = new System.IO.StreamReader(messageResponse.Content.ReadAsStreamAsync().Result))
            {
                string      response = sr.ReadToEnd().Trim();
                ErrorResult result   = JsonConvert.DeserializeObject <EX>(response);
                string      message;

                if (result is OAuthErrorResult)
                {
                    message = string.Format(
                        "The CreateSend OAuth receiver responded with the following error - {0}: {1}",
                        (result as OAuthErrorResult).error,
                        (result as OAuthErrorResult).error_description);
                }
                else // Regular ErrorResult format.
                {
                    message = string.Format(
                        "The CreateSend API responded with the following error - {0}: {1}",
                        result.Code, result.Message);
                }

                CreatesendException exception;
                if (result.Code == "121")
                {
                    exception = new ExpiredOAuthTokenException(message);
                }
                else
                {
                    exception = new CreatesendException(message);
                }

                exception.Data.Add("ErrorResponse", response);
                exception.Data.Add("ErrorResult", result);
                return(exception);
            }
        }
Exemple #4
0
        private static Exception ThrowReworkedCustomException(WebException we)
        {
            using (System.IO.StreamReader sr = new System.IO.StreamReader(((HttpWebResponse)we.Response).GetResponseStream()))
            {
                string response = sr.ReadToEnd().Trim();
                try
                {
                    ErrorResult apiExceptionResult = JavaScriptConvert.DeserializeObject <ErrorResult>(response);

                    CreatesendException exception = new CreatesendException(string.Format("The CreateSend API responded with the following error - {0}: {1}", apiExceptionResult.Code, apiExceptionResult.Message));
                    exception.Data.Add("ErrorResponse", response);
                    exception.Data.Add("ErrorResult", apiExceptionResult);

                    return(exception);
                }
                catch (Newtonsoft.Json.JsonSerializationException)
                {
                    CreatesendException exception = new CreatesendException("The CreateSend API returned an error with addtional data");
                    exception.Data.Add("ErrorResponse", response);

                    return(exception);
                }
            }
        }
Exemple #5
0
 public ErrorResult(ErrorResult errorResult)
 {
     Message = errorResult.Message;
     Code    = errorResult.Code;
 }