(* Collection of code snippets by Arne Vajhøj *)
(* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) *)
program ClientManual;

uses
  Classes, SysUtils,
  IdHTTP,
  DOM, XMLRead, XMLWrite,
  fpjson, jsonparser;

type
  T1 = class(TObject)
    private
      m_f1 : integer;
      m_f2 : string;
    public
      constructor Create;
      constructor Create(f1 : integer; f2 : string);
      property F1 : integer read m_f1;
      property F2 : string read m_f2;
      function ToString() : string; override;
      procedure Free;
  end;
  ListT1 = class(TObject)
    private
      m_list : TList;
    public
      constructor Create;
      procedure Add(o : T1);
      procedure Add(f1 : integer; f2 : string);
      function Count : integer;
      function GetElement(ix : integer) : T1;
      procedure Free;
  end;

constructor T1.Create;

begin
  m_f1 := 0;
  m_f2 := '';
end;

constructor T1.Create(f1 : integer; f2 : string);

begin
  m_f1 := f1;
  m_f2 := f2;
end;

function T1.ToString() : string;

begin
  ToString := '{' + IntToStr(m_f1) + ',' + m_f2 + '}';
end;

procedure T1.Free;

begin
end;

constructor ListT1.Create;

begin
  m_list := TList.Create;
end;

procedure ListT1.Add(o : T1);

begin
  m_list.Add(o);
end;

procedure ListT1.Add(f1 : integer; f2 : string);

begin
  m_list.Add(T1.Create(f1, f2));
end;

function ListT1.Count : integer;

begin
  Count := m_list.Count;
end;

function ListT1.GetElement(ix : integer) : T1;

begin
  GetElement := T1(m_list[ix]);
end;

procedure ListT1.Free;

var
   i : integer;

begin
  for i := 0 to m_list.Count - 1 do begin
      T1(m_list[i]).Free;
  end;
  m_list.Free;
end;

(* start primitive serialization framework *)

function Object2XML(clz : string; obj : TObject) : string;

var
   o : T1;
   a : ListT1;
   res : string;
   i : integer;

begin
  if clz = 'T1' then begin
    o := T1(obj);
    Object2XML := '<t1><f1>' + IntToStr(o.F1) + '</f1><f2>' + o.F2 + '</f2></t1>';
  end else if clz = 'ListT1' then begin
    a := ListT1(obj);
    res := '<t1array>';
    for i := 0 to a.Count - 1 do begin
        res := res + Object2XML('T1', a.GetElement(i));
    end;
    res := res + '</t1array>';
    Object2XML := res;
  end else begin
    raise Exception.Create('Unknown class: ' + clz);
  end;
end;

function XML2Object(clz, s : string) : TObject;

function CreateT1(node : TDOMNode) : T1;

var
 childs : TDOMNodeList;
 i, f1 : integer;
 f2 : string;

begin
  childs := node.GetChildNodes;
  for i := 0 to childs.Count - 1 do begin
      if childs.Item[i].NodeName = 'f1' then begin
         f1 := StrToInt(string(childs.Item[i].FirstChild.NodeValue));
      end;
      if childs.Item[i].NodeName = 'f2' then begin
         f2 := string(childs.Item[i].FirstChild.NodeValue);
      end;
  end;
  CreateT1 := T1.Create(f1, f2);
end;

var
 parser : TDOMParser;
 doc : TXMLDocument;
 childs : TDOMNodeList;
 res : ListT1;
 i : integer;

begin
  parser := TDOMparser.Create;
  parser.Parse(TXMLInputSource.Create(s), doc);
  parser.Free;
  if clz = 'T1' then begin
    XML2Object := CreateT1(doc.DocumentElement);
  end else if clz = 'ListT1' then begin
    res := ListT1.Create;
    childs := doc.DocumentElement.GetChildNodes;
    for i := 0 to childs.Count - 1 do begin
        res.Add(CreateT1(childs.Item[i]));
    end;
    XML2Object := res;
  end else begin
    raise Exception.Create('Unknown class: ' + clz);
  end;
end;

function Object2JSON(clz : string; obj : TObject) : string;

var
   o : T1;
   a : ListT1;
   res : string;
   i : integer;

begin
  if clz = 'T1' then begin
    o := T1(obj);
    Object2JSON := '{"f1":' + IntToStr(o.F1) + ',"f2":"' + o.F2 + '"}';
  end else if clz = 'ListT1' then begin
    a := ListT1(obj);
    res := '[';
    for i := 0 to a.Count - 1 do begin
        res := res + Object2JSON('T1', a.GetElement(i));
    end;
    res := res + ']';
    Object2JSON := res;
  end else begin
    raise Exception.Create('Unknown class: ' + clz);
  end;
end;

function JSON2Object(clz, s : string) : TObject;

function CreateT1(obj : TJSONObject) : T1;

begin
  CreateT1 := T1.Create(obj.Integers['f1'], obj.Strings['f2']);
end;

var
 o : TJSONObject;
 a : TJSONArray;
 res : ListT1;
 i : integer;

begin
  if clz = 'T1' then begin
    o := TJSONObject(GetJSON(s));
    JSON2Object := CreateT1(o);
  end else if clz = 'ListT1' then begin
    a := TJSONArray(GetJSON(s));
    res := ListT1.Create;
    for i := 0 to a.Count - 1 do begin
      res.Add(CreateT1(TJSONObject(a.Items[i])));;
    end;
    JSON2Object := res;
  end else begin
    raise Exception.Create('Unknown class: ' + clz);
  end;
end;

function Object2String(clz : string; obj : TObject; typ : string) : string;

begin
  if typ = 'application/xml' then begin
    Object2String := Object2XML(clz, obj);
  end else if typ = 'application/json' then begin
    Object2String := Object2JSON(clz, obj);
  end else begin
    raise Exception.Create('Unknown type: ' + typ);
  end;
end;

function String2Object(clz, s, typ : string) : TObject;

begin
  if typ = 'application/xml' then begin
    String2Object := XML2Object(clz, s);
  end else if typ = 'application/json' then begin
    String2Object := JSON2Object(clz, s);
  end else begin
    raise Exception.Create('Unknown type: ' + typ);
  end;
end;

(* end primitive serialization framework *)

procedure TestGetOne(urlprefix : string; f1 : integer; acctyp : string);

var
  con : TIdHTTP;
  respbody, contyp : string;

begin
  con := TIdHTTP.Create;
  con.Request.Accept := acctyp;
  respbody := con.Get(urlprefix + '/t1/' + IntToStr(f1));
  writeln((con.Response.ResponseCode div 100) = 2);
  contyp := con.Response.ContentType;
  writeln(String2Object('T1', respbody, contyp).ToString());
  con.Free;
end;

procedure TestGetAll(urlprefix : string; acctyp : string);

var
  con : TIdHTTP;
  respbody, contyp : string;
  a : ListT1;
  i : integer;

begin
  con := TIdHTTP.Create;
  con.Request.Accept := acctyp;
  respbody := con.Get(urlprefix + '/t1');
  writeln((con.Response.ResponseCode div 100) = 2);
  contyp := con.Response.ContentType;
  a := ListT1(String2Object('ListT1', respbody, contyp));
  for i := 0 to a.Count - 1 do begin
      writeln(a.GetElement(i).ToString());
  end;
  con.Free;
end;

procedure TestGetSome(urlprefix : string; start, finish : integer; acctyp : string);

var
  con : TIdHTTP;
  respbody, contyp : string;
  a : ListT1;
  i : integer;

begin
  con := TIdHTTP.Create;
  con.Request.Accept := acctyp;
  respbody := con.Get(urlprefix + '/t1?start=' + IntToStr(start) + '&finish=' + IntToStr(finish));
  writeln((con.Response.ResponseCode div 100) = 2);
  contyp := con.Response.ContentType;
  a := ListT1(String2Object('ListT1', respbody, contyp));
  for i := 0 to a.Count - 1 do begin
      writeln(a.GetElement(i).ToString());
  end;
  con.Free;
end;

procedure TestPost(urlprefix, contyp : string; o : T1; acctyp : string);

var
  con : TIdHTTP;
  respbody, contyp2 : string;

begin
  con := TIdHTTP.Create;
  con.Request.Accept := acctyp;
  con.Request.ContentType := contyp;
  respbody := con.Post(urlprefix + '/t1', TStringStream.Create(Object2String('T1', o, contyp)));
  writeln((con.Response.ResponseCode div 100) = 2);
  contyp2 := con.Response.ContentType;
  writeln(String2Object('T1', respbody, contyp2).ToString());
  con.Free;
end;

procedure TestPut(urlprefix, contyp : string; o : T1);

var
  con : TIdHTTP;

begin
  con := TIdHTTP.Create;
  con.Request.ContentType := contyp;
  con.Put(urlprefix + '/t1/' + IntToStr(o.F1), TStringStream.Create(Object2String('T1', o, contyp)));
  writeln((con.Response.ResponseCode div 100) = 2);
  con.Free;
end;

procedure TestDelete(urlprefix : string; f1 : integer);

var
  con : TIdHTTP;

begin
  con := TIdHTTP.Create;
  con.Delete(urlprefix + '/t1/' + IntToStr(f1));
  writeln((con.Response.ResponseCode div 100) = 2);
  con.Free;
end;

procedure TestAll(urlprefix, typ : string);

begin
  writeln('**** ' + urlprefix + ' (' + typ + ') ****');
  TestGetOne(urlprefix, 123, typ);
  TestGetAll(urlprefix, typ);
  TestGetSome(urlprefix, 10, 12, typ);
  TestPost(urlprefix, typ, T1.Create(123, 'ABC'), typ);
  TestPut(urlprefix, typ, T1.Create(123, 'ABC'));
  TestDelete(urlprefix, 123);
end;

procedure Test(urlprefix : string);

begin
  TestAll(urlprefix, 'application/xml');
  TestAll(urlprefix, 'application/json');
end;

begin
  Test('http://localhost:8080/wsrest/testapi1');
  Test('http://localhost:8080/wsrest/testapi2');
  Test('http://localhost:8080/wsrest/testapi3');
  Test('http://localhost/testapi.ashx');
  Test('http://localhost/testapi.svc');
  Test('http://localhost/testapi');
  Test('http://localhost:81/testapi.php');
end.

