private async Task <CFDIWrapperResult> CallServiceAsync(Format restType,
                                                                RestMethod restConstant, string token, Uri requestUri,
                                                                string serializedParameters)
        {
            CFDIWrapperResult cFDIWrapperResult = new CFDIWrapperResult()
            {
                status = "400", detail = "Error no controlado"
            };
            HttpClient client = new HttpClient();

            client.Timeout = TimeSpan.FromMinutes(40);
            string result = string.Empty;

            ConfigureEndPoints(requestUri);
            HttpRequestMessage reqmsg = new HttpRequestMessage();

            reqmsg.RequestUri = requestUri;
            reqmsg.Method     = new HttpMethod(restConstant.ToString("g"));
            reqmsg.Headers.TryAddWithoutValidation("token", token);

            if (reqmsg.Method == HttpMethod.Post || reqmsg.Method == HttpMethod.Put)
            {
                reqmsg.Content = new StringContent(serializedParameters, Encoding.UTF8, resolveContentType(restType));
            }
            var response = await client.SendAsync(reqmsg, HttpCompletionOption.ResponseContentRead);

            result = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                if (!String.IsNullOrEmpty(result))
                {
                    cFDIWrapperResult = JsonConvert.DeserializeObject <CFDIWrapperResult>(result);
                }
                else
                {
                    throw new CotorraException(1000, "1000", "No se pudo conectar al servicio.", null);
                }
            }
            else
            {
                cFDIWrapperResult.detail = await response.Content.ReadAsStringAsync();
            }

            return(cFDIWrapperResult);
        }
        public async Task <CancelDocumentResult <ICFDINomProvider> > CancelStampingDocumentAsync(
            CancelDocumentResult <ICFDINomProvider> cancelDocumentResult)
        {
            //call service async
            CFDIWrapperResult result = null;
            var xmlSerialized        = JsonConvert.SerializeObject(cancelDocumentResult.CancelationXML);

            await CallServiceAsync(Format.JSON, RestMethod.POST, _cotorraiWrapperCFDIToken,
                                   new Uri($"{_cotorraiWrapperPACUrl}/fiscal/stamping/cancelrequest"), xmlSerialized)
            .ContinueWith((i) =>
            {
                if (i.Exception != null)
                {
                    throw i.Exception;
                }

                result = i.Result;
            });

            if (result.status == "400" && result.errors == null)
            {
                //TFD Fix string input :(
                var xmlAcknowledgement = result.detail.Remove(0, 1);
                xmlAcknowledgement = xmlAcknowledgement.Remove(xmlAcknowledgement.Length - 1, 1);
                xmlAcknowledgement = xmlAcknowledgement.Replace("\\\"", "\"");
                xmlAcknowledgement = xmlAcknowledgement.Replace("\\r\\n", "");
                cancelDocumentResult.WithErrors = false;
                cancelDocumentResult.CancelationAcknowledgmentReceipt = xmlAcknowledgement;
            }
            else if (result.status == "400" && result.errors != null)
            {
                cancelDocumentResult.WithErrors = true;
                cancelDocumentResult.Details    = result.errors.FirstOrDefault();
            }
            else
            {
                cancelDocumentResult.WithErrors = true;
                cancelDocumentResult.Details    = $"OcurriĆ³ un error no esperado en el timbrado: {result.detail}";
            }

            return(cancelDocumentResult);
        }
        public async Task <SignDocumentResult <ICFDINomProvider> > StampDocumetAsync(
            SignDocumentResult <ICFDINomProvider> signDocumentResult, FiscalStampingVersion fiscalStampingVersion, string xml)
        {
            //call service async
            CFDIWrapperResult result = null;
            var xmlSerialized        = JsonConvert.SerializeObject(xml);

            await CallServiceAsync(Format.JSON, RestMethod.POST, _cotorraiWrapperCFDIToken,
                                   new Uri($"{_cotorraiWrapperPACUrl}/fiscal/stamping/stamp"), xmlSerialized)
            .ContinueWith((i) =>
            {
                if (i.Exception != null)
                {
                    throw i.Exception;
                }

                result = i.Result;
            });

            if (result.status == "400" && result.errors == null)
            {
                //TFD Fix string input :(
                var xmlTFD = result.detail.Remove(0, 1);
                xmlTFD = xmlTFD.Remove(xmlTFD.Length - 1, 1);
                xmlTFD = xmlTFD.Replace("\\\"", "\"");
                signDocumentResult.WithErrors = false;
                signDocumentResult.TFD        = xmlTFD;
            }
            else if (result.status == "400" && result.errors != null)
            {
                signDocumentResult.WithErrors = true;
                signDocumentResult.Details    = result.errors.FirstOrDefault();
            }
            else
            {
                signDocumentResult.WithErrors = true;
                signDocumentResult.Details    = $"OcurriĆ³ un error no esperado en el timbrado: {result.detail}";
            }

            return(signDocumentResult);
        }