/// <summary> /// Set on the request all the filters from ListReadAllOptions /// </summary> /// <param name="request">Request that will be used to call the API</param> /// <param name="options">Object with the options of filter for mailchimp call</param> private void SetReadAllOptions(ref RestRequest request, BatchReadAllOptions options) { if (options.fields != null && options.fields.Count > 0) { var fieldsString = ""; for (var i = 0; i < options.fields.Count; i++) { if (i < options.fields.Count - 1) { fieldsString += options.fields[i] + ","; } else { fieldsString += options.fields[i]; } } request.AddQueryParameter("fields", fieldsString); } if (options.exclude_fields != null && options.exclude_fields.Count > 0) { var excludeFieldsString = ""; for (var i = 0; i < options.exclude_fields.Count; i++) { if (i < options.fields.Count - 1) { excludeFieldsString += options.exclude_fields[i] + ","; } else { excludeFieldsString += options.exclude_fields[i]; } } request.AddQueryParameter("exclude_fields", excludeFieldsString); } if (options.count != null) { request.AddQueryParameter("count", ((int)options.count).ToString()); } if (options.offset != null) { request.AddQueryParameter("offset", ((int)options.offset).ToString()); } }
/// <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)); } }