/// <summary> /// Delete a merge-field based on listId and mergeFieldId. /// </summary> /// <param name="listId">The Id of the list to delete the merge-field.</param> /// <param name="mergeFieldId">The Id of the merge-field to be deleted</param> /// <returns></returns> public MailChimpWrapperResponse <CommonDeleteResponseModel> Delete(string listId, int mergeFieldId) { try { // Adds the resource var request = new RestRequest(ResourcesEndpoints.MergeFieldDelete(listId, mergeFieldId), Method.DELETE); // Execute the request var response = restClient.Execute(request); // If the request return ok, then return MailChimpWrapperResponse with deserialized object if (response.StatusCode == System.Net.HttpStatusCode.NoContent) { return new MailChimpWrapperResponse <CommonDeleteResponseModel> { objectRespose = new CommonDeleteResponseModel() } } ; // If an error occurs, encapsulates the error in MailChimpWrapperResponse var errorContent = JsonConvert.DeserializeObject <ErrorModel>(response.Content); return(new MailChimpWrapperResponse <CommonDeleteResponseModel> { hasErrors = true, error = errorContent }); } catch (Exception ex) { return(ErrorResponse <CommonDeleteResponseModel>(ex)); } }
/// <summary> /// Read all members from a list on mailchimp. /// </summary> /// <param name="options">The options to filter the lists result.</param> /// <returns></returns> public MailChimpWrapperResponse <BatchReadAllResponseModel> ReadAll(BatchReadAllOptions options = null) { try { // Adds the resource var request = new RestRequest(ResourcesEndpoints.BatchReadAll(), Method.GET); request.JsonSerializer = new CustomNewtonsoftSerializer(new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore, }); // Check if options was passed if (options != null) { SetReadAllOptions(ref request, options); } // Execute the request var response = restClient.Execute(request); // If the request return ok, then return MailChimpWrapperResponse with deserialized object if (response.StatusCode == System.Net.HttpStatusCode.OK) { return new MailChimpWrapperResponse <BatchReadAllResponseModel> { objectRespose = JsonConvert.DeserializeObject <BatchReadAllResponseModel>(response.Content) } } ; // If an error occurs, encapsulates the error in MailChimpWrapperResponse var errorContent = JsonConvert.DeserializeObject <ErrorModel>(response.Content); return(new MailChimpWrapperResponse <BatchReadAllResponseModel> { hasErrors = true, error = errorContent }); } catch (Exception ex) { return(ErrorResponse <BatchReadAllResponseModel>(ex)); } }
/// <summary> /// Updates the member by its email_address /// </summary> /// <param name="listId">The Id of the list to be updated</param> /// <param name="member">The data to update</param> /// <returns></returns> public MailChimpWrapperResponse <MembersCreateResponseModel> Update(string listId, MembersPatchModel member) { try { // Initialize the RestRequest from RestSharp.Newtonsoft so the serializer will user Newtonsoft defaults. var request = new RestRequest(ResourcesEndpoints.MembersUpdate(listId, member.email_address), Method.PATCH); // Adds the resource request.AddHeader("content-type", "application/json"); request.JsonSerializer = new CustomNewtonsoftSerializer(new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore, }); request.AddJsonBody(member); // Execute the request var response = restClient.Execute(request); // If the request return ok, then return MailChimpWrapperResponse with deserialized object if (response.StatusCode == System.Net.HttpStatusCode.OK) { return new MailChimpWrapperResponse <MembersCreateResponseModel> { objectRespose = JsonConvert.DeserializeObject <MembersCreateResponseModel>(response.Content) } } ; // If an error occurs, encapsulates the error in MailChimpWrapperResponse var errorContent = JsonConvert.DeserializeObject <ErrorModel>(response.Content); return(new MailChimpWrapperResponse <MembersCreateResponseModel> { hasErrors = true, error = errorContent }); } catch (Exception ex) { return(ErrorResponse <MembersCreateResponseModel>(ex)); } }
/// <summary> /// Reads the list info /// </summary> /// <param name="listId">The id of the list that will be read</param> /// <param name="options">Options to filter the list that will be returned</param> public MailChimpWrapperResponse <ListReadResponseModel> Read(string listId, ListReadOptions options = null) { try { // Adds the resource var request = new RestRequest(ResourcesEndpoints.ListRead(listId), Method.GET); // Check if options was passed if (options != null) { SetReadOptions(ref request, options); } // Execute the request var response = restClient.Execute(request); // If the request return ok, then return MailChimpWrapperResponse with deserialized object if (response.StatusCode == System.Net.HttpStatusCode.OK) { return new MailChimpWrapperResponse <ListReadResponseModel> { objectRespose = JsonConvert.DeserializeObject <ListReadResponseModel>(response.Content) } } ; // If an error occurs, encapsulates the error in MailChimpWrapperResponse var errorContent = JsonConvert.DeserializeObject <ErrorModel>(response.Content); return(new MailChimpWrapperResponse <ListReadResponseModel> { hasErrors = true, error = errorContent }); } catch (Exception ex) { return(ErrorResponse <ListReadResponseModel>(ex)); } }
/// <summary> /// Read the merge field info /// </summary> /// <param name="listId">The id of the list that will be read</param> /// <param name="mergeFieldId">Merge Field Id of the merge-field on mailchimp</param> public MailChimpWrapperResponse <MergeFieldReadResponseModel> Read(string listId, int mergeFieldId) { try { // Adds the resource var request = new RestRequest(ResourcesEndpoints.MergeFieldRead(listId, mergeFieldId), Method.GET); request.JsonSerializer = new CustomNewtonsoftSerializer(new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore, }); // Execute the request var response = restClient.Execute(request); // If the request return ok, then return MailChimpWrapperResponse with deserialized object if (response.StatusCode == System.Net.HttpStatusCode.OK) { return new MailChimpWrapperResponse <MergeFieldReadResponseModel> { objectRespose = JsonConvert.DeserializeObject <MergeFieldReadResponseModel>(response.Content) } } ; // If an error occurs, encapsulates the error in MailChimpWrapperResponse var errorContent = JsonConvert.DeserializeObject <ErrorModel>(response.Content); return(new MailChimpWrapperResponse <MergeFieldReadResponseModel> { hasErrors = true, error = errorContent }); } catch (Exception ex) { return(ErrorResponse <MergeFieldReadResponseModel>(ex)); } }
/// <summary> /// Create a batch on mailchimp /// </summary> /// <param name="member"></param> /// <returns></returns> public MailChimpWrapperResponse <BatchCreateResponseModel> Create(BatchCreateModel member) { try { // Initialize the RestRequest from RestSharp.Newtonsoft so the serializer will user Newtonsoft defaults. var request = new RestRequest(ResourcesEndpoints.BatchCreate(), Method.POST); // Adds the resource request.AddHeader("content-type", "application/json"); request.JsonSerializer = new CustomNewtonsoftSerializer(new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore, }); // Validates the object var validationContext = new ValidationContext(member, serviceProvider: null, items: null); var validationResults = new List <ValidationResult>(); var isValid = Validator.TryValidateObject(member, validationContext, validationResults, false); if (isValid) { request.AddJsonBody(member); // Execute the request var response = restClient.Execute(request); // If the request return ok, then return MailChimpWrapperResponse with deserialized object if (response.StatusCode == System.Net.HttpStatusCode.OK) { return new MailChimpWrapperResponse <BatchCreateResponseModel> { objectRespose = JsonConvert.DeserializeObject <BatchCreateResponseModel>(response.Content) } } ; // If an error occurs, encapsulates the error in MailChimpWrapperResponse var errorContent = JsonConvert.DeserializeObject <ErrorModel>(response.Content); return(new MailChimpWrapperResponse <BatchCreateResponseModel> { hasErrors = true, error = errorContent }); } // If the object was not valid then creates and ErrorModel to send back var error = new ErrorModel { type = "Internal Method Error.", title = "One or more fields were not validated. Look in detail for more information", status = 0, detail = Util.GetValidationsErrors(validationResults) }; return(new MailChimpWrapperResponse <BatchCreateResponseModel> { hasErrors = true, error = error }); } catch (Exception ex) { return(ErrorResponse <BatchCreateResponseModel>(ex)); } }
/// <summary> /// Updates the merge-field by its id based on MergeFieldCreateModel object /// </summary> /// <param name="listId">The Id of the list to be updated</param> /// <param name="mergeFieldId">The Id of the merge field to be updated</param> /// <param name="mergeField">The data to update</param> /// <returns></returns> public MailChimpWrapperResponse <MergeFieldCreateResponseModel> Update(string listId, int mergeFieldId, MergeFieldCreateModel mergeField) { try { // Initialize the RestRequest from RestSharp.Newtonsoft so the serializer will user Newtonsoft defaults. var request = new RestRequest(ResourcesEndpoints.MergeFieldEdit(listId, mergeFieldId), Method.PATCH); // Adds the resource request.AddHeader("content-type", "application/json"); request.JsonSerializer = new CustomNewtonsoftSerializer(new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore, }); // Validates the object var validationContext = new ValidationContext(mergeField, serviceProvider: null, items: null); var validationResults = new List <ValidationResult>(); var isValid = Validator.TryValidateObject(mergeField, validationContext, validationResults, false); if (isValid) { request.AddJsonBody(mergeField); // Execute the request var response = restClient.Execute(request); // If the request return ok, then return MailChimpWrapperResponse with deserialized object if (response.StatusCode == System.Net.HttpStatusCode.OK) { return new MailChimpWrapperResponse <MergeFieldCreateResponseModel> { objectRespose = JsonConvert.DeserializeObject <MergeFieldCreateResponseModel>(response.Content) } } ; // If an error occurs, encapsulates the error in MailChimpWrapperResponse var errorContent = JsonConvert.DeserializeObject <ErrorModel>(response.Content); return(new MailChimpWrapperResponse <MergeFieldCreateResponseModel> { hasErrors = true, error = errorContent }); } // If the object was not valid then creates and ErrorModel to send back var error = new ErrorModel { type = "Internal Method Error.", title = string.Format("Field {0} missing", validationResults[0]?.MemberNames.FirstOrDefault()), status = 0, detail = validationResults[0].ErrorMessage }; return(new MailChimpWrapperResponse <MergeFieldCreateResponseModel> { hasErrors = true, error = error }); } catch (Exception ex) { return(ErrorResponse <MergeFieldCreateResponseModel>(ex)); } }