Quantcast
Channel: Planet Object Pascal
Viewing all articles
Browse latest Browse all 1725

Jamie's Blog: Simple REST Client for Delphi

$
0
0

While writing a Delphi Rich client for a personal project talking to RESTful service I recently became frustrated with the available easy REST client interfaces for Delphi.

As a result, I wrote a small wrapper around the Indy TIdHttp component to provide a fluent interface upon which you can use to write REST clients. It’s lightweight wrapper, which currently only has the functionality that I required for this project but I decided to make it available in case anyone else wanted to use it.

You can view a simple example were we post a fictional example of PUT’ing a todo item below:

program SimpleRestRequestSample;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  Classes,
  RestRequest in 'RestRequest.pas';

var RestReq: TRestRequest;
    RestResp: THttpResponse;
    putParams: TStringList;
begin
  try
    try
      putParams := TStringList.Create();
      putParams.Add('title=Buy milk');
      putParams.Add('due-date=01/01/2013 00:00:00');
      RestReq := TRestRequest.Create().Domain('localhost').Path('todo').WithCredentials('test', 'test');
      RestResp := RestReq.Put(putParams);
      if RestResp.ResponseCode = 200 then WriteLn('Your todo was added!');
    finally
      RestReq.Free;
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

As you can see, the interface is designed to be fluent to make it easy to see the components of the request in your code.

The TRestResponse record that is returned contains the ResponseCode and the ResponseBody but does nothing with the returned body as the type of response will be determined by your Client and Server. For example, in my own client project, I set the Accept header to prefer ‘application/json’ and then parse this in the response after receiving a 200 OK response from the server with the application/json header,. This looks something like the below:

      request := TRestRequest.Create.Domain(Self.FAPIEndPoint).WithCredentials(Self.FUsername, Self.FPassword).Path('todo').Path(IntToStr(id));
      response := request.Get;
      if response.ResponseCode = 200 then
      begin
        result := jsonToObj(response.ResponseStr);
        Self.doOnTodoReturned(result);
      end else Self.doOnErrorStatus(response.ResponseCode, response.ResponseStr);

There are a few limitations: for example, it doesn’t currently support the PATCH method as TIdHttp doesn’t currently support this method and I didn’t have the time or motivation to mock out TIdHttp so the unit test coverage doesn’t currently extend that far. Aside from that, I’ve found it pretty nice to work with in my own project so any thoughts would be appreciated.

I’m completely open to help or contributions and you can view the SimpleRestClient project on Github.


Viewing all articles
Browse latest Browse all 1725

Trending Articles