/*-------------------------------------------------------------------------------------------------------
         * // ** MergeDocument **
         * // This method implements the "v1/document/merge" Web API call
         * //
         * // Parameters:
         * //  - MergeBody MergeBody
         * //  - string TemplateName (default = null)
         * //  - ReturnFormat ReturnFormat (default = PDF)
         * //  - bool Append (default = true)
         * //
         * // Return value: A List of byte[]
         *-----------------------------------------------------------------------------------------------------*/
        /// <summary>
        /// This method merges a template with data.
        /// </summary>
        /// <param name="mergeBody">The MergeBody object contains the data, optionally a template and merge settings.</param>
        /// <param name="templateName">The name of the template in the template storage.</param>
        /// <param name="returnFormat">The document format of the resulting document.</param>
        /// <param name="append">Specifies whether the resulting documents should be appended or not.</param>
        /// <param name="test">Specifies whether it is a test run or not. A test run is not counted against the quota and created documents contain a watermark.</param>
        public List <byte[]> MergeDocument(MergeBody mergeBody,
                                           string templateName       = null,
                                           ReturnFormat returnFormat = ReturnFormat.PDF,
                                           bool append = true,
                                           bool test   = false)
        {
            // create a new HttpClient using the Factory method CreateHttpClient
            using (HttpClient client = CreateHttpClient())
            {
                // set the endpoint and pass the query paramaters
                // MergeBody is posted as a JSON object
                HttpResponseMessage response = client.PostAsync("v1/document/merge?templateName=" + templateName +
                                                                "&returnFormat=" + returnFormat.ToString() +
                                                                "&append=" + append.ToString() + "&test=" + test.ToString(), mergeBody, formatter).Result;

                // if sucessful, return the image list
                if (response.IsSuccessStatusCode)
                {
                    List <byte[]> bResults = new List <byte[]>();

                    foreach (string sResult in response.Content.ReadAsAsync <List <string> >().Result)
                    {
                        bResults.Add(Convert.FromBase64String(sResult));
                    }

                    return(bResults);
                    //return response.Content.ReadAsAsync<List<string>>().Result;
                }
                else
                {
                    // throw exception with the message from the endpoint
                    throw new ArgumentException(response.Content.ReadAsStringAsync().Result);
                }
            }
        }
        /*-------------------------------------------------------------------------------------------------------
         * // ** MergeDocument **
         * // This method implements the "v1/document/merge" Web API call
         * //
         * // Parameters:
         * //  - MergeBody MergeBody
         * //  - string TemplateName (default = null)
         * //  - ReturnFormat ReturnFormat (default = PDF)
         * //  - bool Append (default = true)
         * //
         * // Return value: A List of byte[]
         *-----------------------------------------------------------------------------------------------------*/
        /// <summary>
        /// This method merges a template with data.
        /// </summary>
        /// <param name="mergeBody">The MergeBody object contains the data, optionally a template and merge settings.</param>
        /// <param name="templateName">The name of the template in the template storage.</param>
        /// <param name="returnFormat">The document format of the resulting document.</param>
        /// <param name="append">Specifies whether the resulting documents should be appended or not.</param>
        /// <param name="test">Specifies whether it is a test run or not. A test run is not counted against the quota and created documents contain a watermark.</param>
        public List <byte[]> MergeDocument(MergeBody mergeBody,
                                           string templateName       = null,
                                           ReturnFormat returnFormat = ReturnFormat.PDF,
                                           bool append = true,
                                           bool test   = false)
        {
            // create a new HttpClient using the Factory method CreateHttpClient
            using (HttpClient client = CreateHttpClient())
            {
                var sMergeBody = JsonConvert.SerializeObject(mergeBody, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                });

                // set the endpoint and pass the query paramaters
                // MergeBody is posted as a JSON object
                HttpResponseMessage response = client.PostAsync("v1/document/merge?templateName=" + templateName +
                                                                "&returnFormat=" + returnFormat.ToString() +
                                                                "&append=" + append.ToString() + "&test=" + test.ToString(),
                                                                new StringContent(sMergeBody, Encoding.UTF8, "application/json")).Result;

                // if sucessful, return the image list
                if (response.IsSuccessStatusCode)
                {
                    List <byte[]> bResults = new List <byte[]>();

                    foreach (string sResult in (List <string>)JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result, typeof(List <string>)))
                    {
                        bResults.Add(Convert.FromBase64String(sResult));
                    }

                    return(bResults);
                    //return response.Content.ReadAsAsync<List<string>>().Result;
                }
                else
                {
                    // throw exception with the message from the endpoint
                    throw new ArgumentException(response.Content.ReadAsStringAsync().Result);
                }
            }
        }