/*-------------------------------------------------------------------------------------------------------
         * // ** FindAndReplaceDocument **
         * // This method implements the "v1/document/findandreplace" Web API call
         * //
         * // Parameters:
         * //  - FindAndReplaceBody FindAndReplaceBody
         * //  - string TemplateName (default = null)
         * //  - ReturnFormat ReturnFormat (default = PDF)
         * //
         * // Return value: A List of byte[]
         *-----------------------------------------------------------------------------------------------------*/
        /// <summary>
        /// This method replaces strings in a document.
        /// </summary>
        /// <param name="findAndReplaceBody">The FindAndReplaceBody object contains the replace 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="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 byte[] FindAndReplaceDocument(FindAndReplaceBody findAndReplaceBody,
                                             string templateName       = null,
                                             ReturnFormat returnFormat = ReturnFormat.PDF,
                                             bool test = false)
        {
            // create a new HttpClient using the Factory method CreateHttpClient
            using (HttpClient client = CreateHttpClient())
            {
                var sFindAndReplaceBody = JsonConvert.SerializeObject(findAndReplaceBody, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });

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

                // if successful, return the document list
                if (response.IsSuccessStatusCode)
                {
                    return((byte[])JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result, typeof(byte[])));
                }
                else
                {
                    // throw exception with the message from the endpoint
                    throw new ArgumentException(response.Content.ReadAsStringAsync().Result);
                }
            }
        }
        /*-------------------------------------------------------------------------------------------------------
         * // ** FindAndReplaceDocument **
         * // This method implements the "v1/document/findandreplace" Web API call
         * //
         * // Parameters:
         * //  - FindAndReplaceBody FindAndReplaceBody
         * //  - string TemplateName (default = null)
         * //  - ReturnFormat ReturnFormat (default = PDF)
         * //
         * // Return value: A List of byte[]
         *-----------------------------------------------------------------------------------------------------*/
        /// <summary>
        /// This method replaces strings in a document.
        /// </summary>
        /// <param name="findAndReplaceBody">The FindAndReplaceBody object contains the replace 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="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 byte[] FindAndReplaceDocument(FindAndReplaceBody findAndReplaceBody,
                                             string templateName       = null,
                                             ReturnFormat returnFormat = ReturnFormat.PDF,
                                             bool test = false)
        {
            // create a new HttpClient using the Factory method CreateHttpClient
            using (HttpClient client = CreateHttpClient())
            {
                // set the endpoint and pass the query paramaters
                // FindAndReplaceBody is posted as a JSON object
                HttpResponseMessage response = client.PostAsync("v1/document/findandreplace?templateName=" + templateName +
                                                                "&returnFormat=" + returnFormat.ToString() + "&test=" + test.ToString(),
                                                                findAndReplaceBody, formatter).Result;

                // if successful, return the document list
                if (response.IsSuccessStatusCode)
                {
                    return(Convert.FromBase64String(response.Content.ReadAsAsync <string>().Result));
                }
                else
                {
                    // throw exception with the message from the endpoint
                    throw new ArgumentException(response.Content.ReadAsStringAsync().Result);
                }
            }
        }