Beispiel #1
0
        /// <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 SetReadOptions(ref RestRequest request, BatchReadOptions 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);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Read the member info
        /// </summary>
        /// <param name="batchId">The id of the batch to be read</param>
        /// <param name="options">Options to filter the response</param>
        public MailChimpWrapperResponse <BatchReadResponseModel> Read(string batchId, BatchReadOptions options = null)
        {
            try
            {
                // Adds the resource
                var request = new RestRequest(ResourcesEndpoints.BatchRead(batchId), Method.GET);
                request.JsonSerializer = new CustomNewtonsoftSerializer(new JsonSerializer()
                {
                    NullValueHandling = NullValueHandling.Ignore,
                });
                // 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 <BatchReadResponseModel>
                           {
                               objectRespose = JsonConvert.DeserializeObject <BatchReadResponseModel>(response.Content)
                           }
                }
                ;

                // If an error occurs, encapsulates the error in MailChimpWrapperResponse
                var errorContent = JsonConvert.DeserializeObject <ErrorModel>(response.Content);
                return(new MailChimpWrapperResponse <BatchReadResponseModel>
                {
                    hasErrors = true,
                    error = errorContent
                });
            }
            catch (Exception ex)
            {
                return(ErrorResponse <BatchReadResponseModel>(ex));
            }
        }