unit ContaEndpoints;

interface

uses
  Classes, Conexoes, IdHTTP, XSuperObject, IdGlobal, Vcl.Dialogs, System.SysUtils,
  MainCode, ModalParametrosBody, Forms;

Procedure SGetV2GnConfig;
Procedure SGetV2GnSaldo;
Procedure SPutV2GnConfig;

implementation

threadvar
  HttpClient: TIdHTTP;
  AccessToken, ObjResult : iSuperObject;
  JsonToSend: TMemoryStream;

var
  ModalParametrosBody : TModalParametrosBody;

procedure SGetV2GnConfig;
var
  sResponse: string;
begin
  try
    AccessToken := Conexoes.OauthToken;
    HttpClient := Conexoes.SetupClient;
    HttpClient.Request.CustomHeaders.Values['Authorization'] := ('Bearer '+ AccessToken.S['access_token']);
    sResponse := HttpClient.Get(MainCode.MainFrm.Enviroment + '/v2/gn/config');

    MainCode.MainFrm.MemoResponse.text := sresponse;

  except
    on E: Exception do
    begin
      ShowMessage('Erro na Requisição GET /v2/gn/Config.'#13#10#13#10 + e.Message);
    end;
  end;
end;

procedure SGetV2GnSaldo;
var
  sResponse: string;
begin
  try
    AccessToken := Conexoes.OauthToken;
    HttpClient := Conexoes.SetupClient;

    HttpClient.Request.CustomHeaders.Values['Authorization'] := ('Bearer '+ AccessToken.S['access_token']);
    sResponse := HttpClient.Get(MainCode.MainFrm.Enviroment + '/v2/gn/saldo');

    MainCode.MainFrm.MemoResponse.text := sresponse;

  except
    on E: Exception do
    begin
      ShowMessage('Erro na Requisição GET /v2/gn/Saldo '#13#10#13#10 + e.Message);
    end;
  end;
end;

procedure SPutV2GnConfig;
var
  sResponse: string;
begin
  try
    try

      if not Assigned(ModalParametrosBody) then
        Application.CreateForm(TModalParametrosBody, ModalParametrosBody);

      if ModalParametrosBody.ShowModal = 1 then
      begin
        AccessToken := Conexoes.OauthToken;
        HttpClient := Conexoes.SetupClient;

        HttpClient.Request.CustomHeaders.Values['Authorization'] := ('Bearer '+ AccessToken.S['access_token']);

        JsonToSend := TMemoryStream.Create;
        WriteStringToStream(JsonToSend, ModalParametrosBody.Body, IndyTextEncoding_UTF8);
        JsonToSend.Position := 0;


        sResponse := HttpClient.Put( MainCode.MainFrm.Enviroment +'/v2/gn/config', JsonToSend);

        MainCode.MainFrm.MemoResponse.text := sresponse;
      end;

    except
      on E: Exception do
      begin
        ShowMessage('Erro na Requisição PUT /v2/gn/config '#13#10#13#10 + e.Message);
      end;
    end;
  finally
    HttpClient.Free;
  end;
end;

end.
