How to consume doing client-side addressing of an API that I want to consume?

2

I have an API in PHP and I'm developing a Mobile App with Delphi Firemonkey, and I need to consume the JSON data that my API returns.

I would like to know how I do the addressing on the client side to access this API, since I'm using (for tests) a string that contains the path I want to test. Something like URL: = ' link '.

So far, I've been able to get the JSON that my API returns, and I'm doing it in the following way:

1 - I created a UNIT to handle the REST request, and the code looks like this:

unit uRestConnect;

interface
uses
    REST.Client, REST.Json, JSON;

type
    TRest = class
    private
      FClient   : TRESTClient;
      FRequest  : TRESTRequest;
      FResponse : TRESTResponse;
      FURL      : String;
    public
      constructor Create;
      destructor Destroy;
      function Result(Recurso: String; Parametros: array of string): String;
    end;

implementation

{ TRest }

constructor TRest.Create;
begin
     Self.FURL := 'http://192.168.1.3/api/v1/';

     Self.FClient   := TRESTClient.Create(nil);
     Self.FRequest  := TRESTRequest.Create(nil);
     Self.FResponse := TRESTResponse.Create(nil);

     Self.FClient.BaseURL := Self.FURL;

     Self.FClient.AutoCreateParams  := True;
     Self.FRequest.AutoCreateParams := True;
     Self.FRequest.HandleRedirects  := True;
     Self.FRequest.Client           := Self.FClient;
     Self.FRequest.Response         := Self.FResponse;
end;

destructor TRest.Destroy;
begin
    Self.FClient.Free;
    Self.FRequest.Free;
    Self.FResponse.Free;
end;

function TRest.Result(Recurso: string; Parametros: array of string): String;
var
     I: Integer;
begin
     for I := 0 to Length(Parametros)-1 do
     begin
          Recurso := Recurso + '/' + Parametros[i];
     end;

     Self.FRequest.Resource := Recurso;
     Self.FRequest.Execute;

     Result := Self.FResponse.JSONText;
end;
end.

And I have a basic form with a button and a memo where the content of the JSON that I retrieve from the API is being displayed. The button event looks like this:

procedure TForm1.btnBuscarClick(Sender: TObject);
var
     Rest     : TRest;
     Cliente  : TCliente;
     Response : String;
begin
     Rest         := TRest.Create;
     Cliente      := TCliente.Create;
     Response := Rest.Result('atendimentoscliente', EdIdCliente.Text);
     MemoAtendimentos.Text := Response;
end;

Now, I would like to know how to make this JSON converted into an object of a class in my model.

    
asked by anonymous 03.04.2017 / 13:44

0 answers