Lazarus

Uso de Lazarus (Free Pascal) para obtener datos desde la API de indicadores.

A continuación se muestra el código de ejemplo para consultar a nuestra API desde Lazarus (Free Pascal). El ejemplo utiliza TFPHTTPClient y fpjson, incluidos en Lazarus, y envía la cabecera User-Agent para identificar el cliente.

program FindicExample;

{$mode objfpc}{$H+}

uses
  SysUtils, fphttpclient, fpjson, jsonparser, opensslsockets;

var
  Client: TFPHTTPClient;
  JSONValue: TJSONData;
  Resumen: TJSONObject;
  UF: TJSONObject;
begin
  Client := TFPHTTPClient.Create(nil);
  try
    Client.AddHeader('User-Agent', 'findic-docs-lazarus/1.0');
    Client.AddHeader('Accept', 'application/json');
    Client.ConnectTimeout := 10000;
    Client.IOTimeout := 10000;

    try
      JSONValue := GetJSON(Client.Get('https://findic.cl/api/'));
      if not (JSONValue is TJSONObject) then
      begin
        JSONValue.Free;
        Writeln('Error al decodificar el JSON');
        Exit;
      end;

      Resumen := TJSONObject(JSONValue);
      try
        UF := Resumen.Objects['uf'];
        Writeln('Fecha: ', UF.Strings['fecha']);
        Writeln('Valor UF: $ ', UF.Floats['valor']:0:2);
      finally
        Resumen.Free;
      end;
    except
      on E: Exception do
        Writeln('Error en la solicitud: ', E.Message);
    end;
  finally
    Client.Free;
  end;
end.

Más información del objeto JSON entregado en el resumen aquí.

En macOS con Homebrew, si aparece Could not initialize OpenSSL library, instala openssl@1.1 (brew install openssl@1.1) y ejecuta el programa con DYLD_LIBRARY_PATH="$(brew --prefix openssl@1.1)/lib".