Request handling
TWiRLClientResource
Once TWiRLClient and TWiRLClientApplication have been initialized, it is possible to make the ReST calls. Also in this case it is possible to use the fluent syntax and, since the implementation is done through interfaces, it is not necessary to destroy the WiRL objects.
function TMainDataModule.GetPerson(Id: Integer): TPerson;
begin
Result := WiRLClientApplication1
// Create the invokation object for the resource
.Resource('person')
// Set the accept header (necessary for the server
// to decide the format of the response)
.Accept('application/json')
// Set the *Id* parameter
.QueryParam('Id', Id.ToString)
// Make the call deserializing the response
// into an object of type TPerson
.Get<TPerson>;
end;
With this example, WiRLClientApplication1 is asked to generate a new TWiRLInvocation
on the indicated resource. Through the Accept
and QueryParam
methods, the headers and parameters required by the server are set. The Get
method will start the actual request and the body of the message returned by the server will be used to create an object of type TPerson
as indicated.
User-created objects
Another way of use TWiRLInvocation
is to give to the Get
method (the same thing also applies to Post
, Put
, Delete
, etc.) an object created by us that will be "filled" with the data provided by the server.
function TMainDataModule.GetPersonName(Id: Integer): string;
var
LPerson: TPerson;
begin
LPerson := TPerson.Create;
try
WiRLClientApplication1
.Resource('person')
.Accept('application/json')
.QueryParam('Id', Id.ToString)
.Get(LPerson);
Result := LPerson.Name;
finally
LPerson.Free;
end;
end;
This approach is very useful when the server returns an array of objects. For examples if the properties of the objects are equivalent to the fields of a dataset, WiRL is able to populate the fields of the DataSet (for example a memory table) automatically.
procedure TMainDataModule.GetPeople(ADataSet: TDataSet);
begin
WiRLClientApplication1
.Resource('person')
.Accept('application/json')
.Get(ADataSet);
end;
Read the response at a low level
In some cases it may be useful to read the server response in more detail. For example, the server could return different JSONs depending on certain headers or the status code. In this case we need to access IWiRLResponse
which contains all the information of the response.
function TMainDataModule.GetPerson(Id: Integer): TPerson;
var
LResponse: IWiRLResponse;
begin
LResponse := WiRLClientApplication1
.Resource('person')
.Accept('application/json')
.QueryParam('Id', Id.ToString)
.Get<IWiRLResponse>;
// Do something with the response
if LResponse.Headers.ContentType <> 'application/json' then
Abort;
Result := LResponse.Content.AsType<TPerson>();
end;
More
There are many other useful features of WiRL Client such as:
- Filter management
- Authentication and authorization
- Customization of message body readers and writers
- Creation of new "vendors" for TWiRLClient
- Asynchronous calls