Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save viniciusmelocodes/c837caa52cb1ce416ec856054ad604c3 to your computer and use it in GitHub Desktop.
Save viniciusmelocodes/c837caa52cb1ce416ec856054ad604c3 to your computer and use it in GitHub Desktop.
function TServicoIntegradorEComPlus.pesquisarCliente: TStringList;
var
documentosClientes: TStringList;
qry: TFDQuery;
i: Integer;
dadosClientes, arrayCliente: ISuperObject;
arrayClientes: TSuperArray;
mensagem401: string;
begin
mensagem401 := '';
qry := TFDQuery.Create(nil);
documentosClientes := TStringList.Create;
try
try
// Configuração conector
configurarConector;
rstrqstProcurarClientes.ResetToDefaults;
rstclntProcurarClientes.ResetToDefaults;
rstrspnsProcurarClientes.ResetToDefaults;
qry.Connection := conFirebird;
qry.Close;
qry.SQL.Text := SQL_CONFIGURACOES_SISTEMAS;
qry.ParamByName('CFST_E_COM_PLUS_STORE_ID').AsInteger := storeId;
qry.Open;
if (qry.FieldByName('CFST_E_COM_PLUS_EXPIRACAO_TOKEN').AsDateTime = 0) or (qry.FieldByName('CFST_E_COM_PLUS_EXPIRACAO_TOKEN').AsDateTime < Now) then
begin
gerarTokenAcesso;
qry.Close;
qry.ParamByName('CFST_E_COM_PLUS_STORE_ID').AsInteger := storeId;
qry.Open;
end;
myId := qry.FieldByName('CFST_E_COM_PLUS_ID').AsString;
accessToken := qry.FieldByName('CFST_E_COM_PLUS_ACCESS_TOKEN').AsString;
rstclntProcurarClientes.BaseURL := CUSTOMERS_MAIN_EMAIL;
rstclntProcurarClientes.BaseURL := StringReplace(rstclntProcurarClientes.BaseURL, '{main_email}', mainEmail, [rfReplaceAll]);
rstrqstProcurarClientes.Client := rstclntProcurarClientes;
rstrqstProcurarClientes.Method := rmGET;
rstrqstProcurarClientes.Response := rstrspnsProcurarClientes;
rstrqstProcurarClientes.Params.Clear;
rstrqstProcurarClientes.Params.AddHeader('Content-Type', 'application/json');
rstrqstProcurarClientes.Params.AddHeader('X-Store-ID', storeId.ToString);
rstrqstProcurarClientes.Params.AddHeader('X-Access-Token', accessToken);
rstrqstProcurarClientes.Params.AddHeader('X-My-ID', myId);
rstrqstProcurarClientes.Execute;
if (rstrspnsProcurarClientes.StatusCode <> 401) then
begin
dadosClientes := SO(TJson.JsonEncode(rstrspnsProcurarClientes.JSONValue));
arrayClientes := dadosClientes['result'].AsArray;
documentosClientes.Clear;
for i := 0 to arrayClientes.Length - 1 do
begin
arrayCliente := arrayClientes.O[i];
documentosClientes.Add(mainEmail + ' => ' + arrayCliente.S['_id']);
documentosClientes.Add(mainEmail + ' => ' + arrayCliente.S['doc_number']);
end;
end
else
mensagem401 := rstrspnsProcurarClientes.Content;
// salvarLog('Pesquisar Clientes', 'Cliente encontrado. Dados: ' + documentosClientes[0] + '; ' + documentosClientes[1], conFirebird);
except
on E: Exception do
begin
salvarLog('Erro ao procurar clientes. Detalhes: ' + E.Message, conFirebird);
conFirebird.Close;
FreeAndNil(qry);
end;
end;
finally
conFirebird.Close;
Result := TStringList.Create;
if mensagem401 <> '' then
Result.Text := mensagem401
else
Result.Text := documentosClientes.Text;
FreeAndNil(qry);
FreeAndNil(documentosClientes);
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment