public List <CabecalhoHttpRest> PegaTokenNeo(string userName, string password, string storeId, string urlBase)
        {
            var ret = new List <CabecalhoHttpRest>();

            HttpUtil httpRest = null;

            IAutenticacaoRest autenticacaoRest = AutenticacaoRestAbstractFactory.ObterInstancia((Model.Interface.Rest.TipoAutenticacao)Enum.Parse(typeof(Model.Interface.Rest.TipoAutenticacao), "1"));

            string url = urlBase + "auth";

            httpRest = new HttpUtil(url, HttpVerbos.POST, autenticacaoRest);

            var obj = new ReqBody();

            obj.userName = userName;
            obj.password = password;
            obj.storeID  = storeId;

            httpRest.Data = Json.Serialize(obj);

            httpRest.ContentytType = Interface.Rest.ContentyType.Json.GetAtributoContentType();

            var resultadoHttpRest = httpRest.FazerRequisicao(RetornoRequisicao);

            var result = Json.Deserialize <ReqRetorno>(resultadoHttpRest.Conteudo);

            ret.Add(new CabecalhoHttpRest {
                Nome = "Authorization", Valor = "Bearer " + result.access_token
            });

            return(ret);
        }
Пример #2
0
        public Resultado Executar(Config entConfig, XmlDocument xmlFila = null)
        {
            Resultado ret      = new Resultado();
            HttpUtil  httpRest = null;

            try
            {
                if (xmlFila != null)
                {
                    ObterValorNodeXML(entConfig.Mapeamentos, xmlFila, false);
                }

                var url       = ConfigurarURL(entConfig.Rest.Url, entConfig.MapeamentosQueryString, xmlFila);
                var httpVerbo = (HttpVerbos)Enum.Parse(typeof(HttpVerbos), entConfig.Rest.VerboHttp.ToString());

                IAutenticacaoRest autenticacaoRest = AutenticacaoRestAbstractFactory.ObterInstancia(entConfig.Rest.Autenticacao);

                httpRest = new HttpUtil(url, httpVerbo, autenticacaoRest);


                if (!string.IsNullOrEmpty(entConfig.AcaoInicial))
                {
                    //List<object> lst = new List<object>();
                    //lst.Add("bWRpYXNfYWRtcHJvZHV0bw==");
                    //lst.Add("YWRtcHJvbWRpYXMyQDE3");
                    //lst.Add("MTAwMDAxNQ==");
                    //httpRest.Data = new Integrador.Utils.Generic.Invoke().Exec(entConfig.AcaoInicial.Split(';')[0], entConfig.AcaoInicial.Split(';')[1], lst).ToString();
                }
                else
                {
                    if (httpVerbo == HttpVerbos.POST || httpVerbo == HttpVerbos.PUT)
                    {
                        httpRest.Data = ObterConteudo(entConfig.Mapeamentos, entConfig.Rest.ContentType, xmlFila);
                    }
                }

                httpRest.ContentytType = entConfig.Rest.ContentType.GetAtributoContentType();
                httpRest.Login         = entConfig.Rest.Login;
                httpRest.Senha         = entConfig.Rest.Senha;

                if (httpVerbo == HttpVerbos.POST || httpVerbo == HttpVerbos.PUT)
                {
                    ret.ParametrosEnvio = httpRest.Data;
                }

                if (!string.IsNullOrEmpty(entConfig.AcaoCabecalho))
                {
                    List <object> lst = new List <object>();
                    entConfig.Rest.CabecalhosHttpRest = (List <Interface.Rest.CabecalhoHttpRest>) new Integrador.Utils.Generic.Invoke().Exec(entConfig.AcaoCabecalho.Split(';')[0], entConfig.AcaoCabecalho.Split(';')[1], lst);
                }

                foreach (var cabecalho in entConfig.Rest.CabecalhosHttpRest)
                {
                    httpRest.InserirCabecalho(cabecalho.Nome, cabecalho.Valor);
                }

                var resultadoHttpRest = httpRest.FazerRequisicao(RetornoRequisicao);

                ret.RespostaXmlCompleta = ConverterParaXML(resultadoHttpRest.ConteudoJson, resultadoHttpRest.Conteudo, entConfig.Rest.ReturnRootElementPai, entConfig.Rest.ReturnRootElementLista);

                if (!string.IsNullOrEmpty(entConfig.AcaoFinal))
                {
                    List <object> lst = new List <object>();
                    lst.Add(ret.RespostaXmlCompleta);
                    lst.Add(xmlFila);
                    lst.Add(entConfig);
                    try
                    {
                        ret.RespostaXmlCompleta = (XmlDocument) new Integrador.Utils.Generic.Invoke().Exec(entConfig.AcaoFinal.Split(';')[0], entConfig.AcaoFinal.Split(';')[1], lst);
                    }
                    catch (Exception e)
                    {
                        throw new Selia.Integrador.Utils.Exceptions.HttpRestRetornoException(
                                  message: e.Message,
                                  inner: e,
                                  conteudoFila: xmlFila.InnerXml,
                                  retornoNaoTratado: "Connection: " + Monitor.Instance.GetStatus() + "  **  " + "STATUS: ** " + resultadoHttpRest.HttpStatusCodeSucess + " ** CONTENT: ** " + resultadoHttpRest.Conteudo + " **",
                                  retornoTratado: e.Message + "  <br /><br />\r\n\r\n  " + e.StackTrace
                                  );
                    }
                }

                ret = new Resultado
                {
                    Mensagem              = "Rest executado com sucesso.",
                    ElementoSeparador     = entConfig.ElementoSeparador,
                    RespostaXmlCompleta   = ret.RespostaXmlCompleta,
                    TipoMensagem          = Resultado.Tipo.Sucesso,
                    ParametrosEnvio       = httpRest.Data,
                    RespostaSemTratamento = resultadoHttpRest.Conteudo
                };
            }
            catch (Selia.Integrador.Utils.Exceptions.HttpRestRetornoException exIntegradoRetorno)
            {
                if (httpRest != null)
                {
                    exIntegradoRetorno.InformacaoAdicional = httpRest.Data;
                    exIntegradoRetorno.ParamentroEnvio     = httpRest.Data;
                }

                if (xmlFila != null)
                {
                    exIntegradoRetorno.ConteudoFila = xmlFila.InnerXml;
                }

                throw exIntegradoRetorno;
            }
            catch (Selia.Integrador.Utils.Exceptions.HttpRestException exIntegrador)
            {
                if (httpRest != null)
                {
                    exIntegrador.InformacaoAdicional = httpRest.Data;
                    exIntegrador.ParamentroEnvio     = httpRest.Data;
                }

                if (xmlFila != null)
                {
                    exIntegrador.ConteudoFila = xmlFila.InnerXml;
                }

                throw exIntegrador;
            }
            catch (Exception ex)
            {
                ret = new Resultado
                {
                    Mensagem          = ex.Message + " <br><br>  " + ex.StackTrace,
                    TipoMensagem      = Resultado.Tipo.Erro,
                    ElementoSeparador = entConfig.ElementoSeparador,

                    ParametrosEnvio       = (httpRest != null) ? httpRest.Data : "(empty)",
                    RespostaSemTratamento = ex.Message + " <br><br>\r\n\r\n  " + ex.StackTrace,
                };
                //ServiceLog.LogError(String.Format("Erro: Message: {0} - StackTrace: {1}", ex.Message, ex.ToString()));
            }
            return(ret);
        }