Ejemplo n.º 1
0
        /// <summary>
        /// https://us2.api.mailchimp.com/2.0/helper/ping
        /// </summary>
        /// <returns></returns>
        public ServiceResponse PingMailChimpServer()
        {
            ServiceResponse serviceResponse = new ServiceResponse();

            var urlTemplate = String.Format("{0}{1}/ping.json/", MailChimpServiceConfiguration.Settings.ServiceUrl,
                                            MailChimpServiceConfiguration.Settings.HelperRelatedSection);

            string pingJson = @"{""apikey"": """ + _apiKey + @"""}";

            try
            {
                var responseData = PostHelpers.PostJson(urlTemplate, pingJson);
                _log.DebugFormat("MailChimpService Call : {0}, response json : {1}", pingJson, responseData);
                serviceResponse.ResponseJson = responseData;
                serviceResponse.IsSuccesful  = true;
            }
            catch (WebException exception)
            {
                using (Stream stream = exception.Response.GetResponseStream())
                {
                    StreamReader reader         = new StreamReader(stream, Encoding.UTF8);
                    String       responseString = reader.ReadToEnd();
                    serviceResponse.ResponseJson = responseString;
                    _log.Error(responseString);
                }
                _log.Error(exception);

                serviceResponse.IsSuccesful = false;
            }
            return(serviceResponse);
        }
Ejemplo n.º 2
0
        private ServiceResponse SubscribeWithMergeVars(string email, dynamic mergeVars, bool updateExisting = true)
        {
            ServiceResponse serviceResponse = new ServiceResponse();
            var             urlTemplate     = String.Format("{0}{1}/subscribe.json/", MailChimpServiceConfiguration.Settings.ServiceUrl,
                                                            MailChimpServiceConfiguration.Settings.ListsRelatedSection);

            var subscriber = new Subscriber();

            subscriber.ApiKey = _apiKey;
            subscriber.ListId = MailChimpServiceConfiguration.Settings.SubscriberListId;
            var emailObject = new Email {
                EmailValue = email
            };

            subscriber.Email          = emailObject;
            subscriber.UpdateExisting = updateExisting;

            if (mergeVars != null)
            {
                subscriber.MergeVars = mergeVars;
            }
            try
            {
                var responseData     = PostHelpers.PostJson(urlTemplate, subscriber.ToString());
                var deserializedData = JsonConvert.DeserializeObject <Email>(responseData);
                _log.DebugFormat("MailChimpService Call : {0}, response json : {1}", subscriber, deserializedData);
                serviceResponse.IsSuccesful = deserializedData.EmailValue != null && deserializedData.Euid != null &&
                                              deserializedData.Leid != null;
                serviceResponse.ResponseJson = responseData;
            }
            catch (WebException exception)
            {
                using (Stream stream = exception.Response.GetResponseStream())
                {
                    StreamReader reader         = new StreamReader(stream, Encoding.UTF8);
                    String       responseString = reader.ReadToEnd();
                    serviceResponse.ResponseJson = responseString;
                    _log.Error(responseString);
                }
                _log.Error(exception);
                serviceResponse.IsSuccesful = false;
            }
            return(serviceResponse);
        }