Recent

Author Topic: TRESTClient availability in FPC/Lazarus  (Read 19764 times)

kveroneau

  • Full Member
  • ***
  • Posts: 122
TRESTClient availability in FPC/Lazarus
« on: March 30, 2016, 08:40:58 pm »
I noticed that Delphi has this pretty awesome REST library:  http://docwiki.embarcadero.com/RADStudio/Seattle/en/REST_Client_Library

Has anybody tries to use this in a Lazarus project, or has it been ported over to Lazarus yet?  Is there an alternative REST client library available?


kveroneau

  • Full Member
  • ***
  • Posts: 122
Re: TRESTClient availability in FPC/Lazarus
« Reply #2 on: March 30, 2016, 09:49:10 pm »
mORMot appears to be more of a client-server framework, which seems more like a web framework.  TRestClient is strictly a client-side REST API client, so that we can talk to remote REST services such as Twitter, Facebook, among others from our application.  Basically, my end goal is to be-able to talk with the Twilio REST API:  https://www.twilio.com/docs/api/rest

I'd like to be able to say, connect to their REST API from my Lazarus desktop application to display call logs, SMS messages, and grab other stats from my Twilio account.  TRestClient appears to be the perfect tool for this job, but I am unsure if it's available in Lazarus or not.

JD

  • Hero Member
  • *****
  • Posts: 1913
Re: TRESTClient availability in FPC/Lazarus
« Reply #3 on: March 30, 2016, 11:40:25 pm »
Brook Framework has REST capabilities

http://silvioprog.github.io/brookframework/

JD
Linux Mint - Lazarus 4.6/FPC 3.2.2,
Windows - Lazarus 4.6/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

ttomas

  • Sr. Member
  • ****
  • Posts: 250
Re: TRESTClient availability in FPC/Lazarus
« Reply #4 on: March 31, 2016, 12:20:12 am »

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: TRESTClient availability in FPC/Lazarus
« Reply #5 on: March 31, 2016, 06:46:20 am »
hello,
you can use also RestRequest unit from here which uses  indy components. 
Success on Windows 7 Lazarus 1.6   :
1 - Install Indy ( see lazarus wiki page)
2 - Use my modified version of RestRequest.pas

in attachment application console project to read data from a fake rest server (for testing). May be need to change the path to Indy files. 

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$MODE Delphi}
  4. uses
  5.   SysUtils,
  6.   Classes,
  7.   RestRequest in 'RestRequest.pas';
  8.  
  9. var RestReq: TRestRequest;
  10.     RestResp: THttpResponse;
  11. begin
  12.   try
  13.     RestReq := nil;
  14.     try
  15.    //   RestReq := TRestRequest.Create().Domain('jsonplaceholder.typicode.com').Path('todos').Path('1');
  16.            RestReq := TRestRequest.Create().Domain('jsonplaceholder.typicode.com').Path('posts');
  17.       RestResp := RestReq.Get();
  18.       if RestResp.ResponseCode = 200 then WriteLn('Your todo was added!') else WriteLn('Failed to add your todo.');
  19.       WriteLn(RestResp.ResponseStr);
  20.       Readln;
  21.     finally
  22.       RestReq.Free;
  23.     end;
  24.   except
  25.     on E: Exception do
  26.       Writeln(E.ClassName, ': ', E.Message);
  27.   end;
  28. end.
  29.  

May be someone has time  to replace indy components with fcl or synapse components ( if possible)    ;)

Friendly, J.P
« Last Edit: March 31, 2016, 07:13:50 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TRESTClient availability in FPC/Lazarus
« Reply #6 on: March 31, 2016, 08:05:51 am »
Most REST implementation is just plain HTTP with JSON response (well, some requires authentication/authorization using certain protocols but that's also on top of plain HTTP). fphttpclient + fpjson unit should already cover the needs.

kveroneau

  • Full Member
  • ***
  • Posts: 122
Re: TRESTClient availability in FPC/Lazarus
« Reply #7 on: March 31, 2016, 07:11:31 pm »
Thank you for the helpful replies here.  I am looking to be as cross-plat compatible as possible, and my dev box is actually a Debian box, so components which strictly depend on Windows libraries are a no-go.  The delphi-rest-client-api appears to depend on WinHTTP, and WinINET, or perhaps the README.md file wasn't updated after the Lazarus fork...

I will first give the RestRequest unit a shot, the one provided by Jurassic Pork, although I'd wish for as little external deps as possible, I'll check it out and see how it works.

In the end, I may end up rolling my own class using Leledumbo's suggestion of using fphttpclient+fpjson units, as this would make it more cross-plat, and would be more de-coupled from LCL components, so it has the benefit of working with both pure FPC(non-Lazarus) programs, and also working with Lazarus as well.  I am also thinking of taking a look at the INet library, as I believe they also have a HTTPClient class which may work to communicate with REST as well.

As Leledumbo mentions, the only issue I may run into with rolling my own REST API client is dealing with authentication, but most endpoints just use a simple header, so that shouldn't be a big deal.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TRESTClient availability in FPC/Lazarus
« Reply #8 on: March 31, 2016, 08:06:05 pm »
Take a look on how I implement one for Instagram: https://bitbucket.org/leledumbo/instapas
fpctwit (https://bitbucket.org/reiniero/fpctwit) also has Twitter and Plurk implementation, which uses OAuth v1.0a protocol.

ttomas

  • Sr. Member
  • ****
  • Posts: 250
Re: TRESTClient availability in FPC/Lazarus
« Reply #9 on: March 31, 2016, 10:51:32 pm »
The delphi-rest-client-api appears to depend on WinHTTP, and WinINET, or perhaps the README.md file wasn't updated after the Lazarus fork...
Old readme.md
You can look at files HttpConnectionXXX.pas
delphi-rest-cient-api have interface implementation for FpHTTP and Indy (cross platform) and WinHTTP, WinINET (windows only).

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: TRESTClient availability in FPC/Lazarus
« Reply #10 on: April 01, 2016, 01:00:52 am »
hello,
here is an example (for post) to dump json data returned from a REST server using free pascal libraries :
Code: Pascal  [Select][+][-]
  1. // uses fphttpclient, fpjson, jsonparser;
  2. Procedure TForm1.DumpJSONData(J : TJSonData; DOEOLN : Boolean = True);
  3.  
  4. Var
  5.   I : Integer;
  6.   DataString : String;
  7.  
  8. begin
  9.   // JSONType property determines kind of value.
  10.   Case J.jsontype of
  11.     jtNull   : Memo1.Append('Null');
  12.     jtBoolean : If J.AsBoolean then
  13.                   Memo1.Append('True')
  14.                 else
  15.                   Memo1.Append('False');
  16.     jtNumber : {JSONNumber has extra NumberType property
  17.                 which determines kind of value (int/float).}
  18.                Case TJSONNumber(J).NumberType of
  19.                  ntInteger : Memo1.Append(inttostr(J.AsInteger));
  20.                  ntFloat   : Memo1.Append(floattostr(J.AsFloat));
  21.                end;
  22.     jtString : Memo1.Append('"' + J.AsString +'"');
  23.     jtArray  : begin
  24.                Memo1.Append('======================');
  25.                For I:=0 to J.Count-1 do
  26.                  begin
  27.                  DumpJSONData(J.Items[I],False);
  28.                  If I<J.Count-1 then
  29.                    Memo1.Append('+++++++++++++++++++++++');
  30.                  end;
  31.                Memo1.Append('=======================');
  32.                end;
  33.     jtObject : begin
  34.                Memo1.Append('========================');
  35.                For I:=0 to J.Count-1 do
  36.                  begin
  37.                  Memo1.Append('"' + TJSONObject(J).Names[i] + '" : ');
  38.                  DumpJSONData(J.Items[I],False);
  39.                  If I<J.Count-1 then
  40.                    Memo1.Append('+++++++++++++++++++++++++')
  41.                  end;
  42.                Memo1.Append('=========================');
  43.                end;
  44.    end;
  45.    If DOEOLN then
  46.      Memo1.Append('');
  47. end;
  48.  
  49. procedure TForm1.bt_GetDataClick(Sender: TObject);
  50. Var
  51.   S : String;
  52.   J: TJSONData;
  53.   httpClient : TFPHTTPClient;
  54.   user,pwd,id : String;
  55. begin
  56.   Memo1.Clear;
  57.   user := 'user';
  58.   pwd  := 'mdp';
  59.   id := '5';
  60.   httpClient := TFPHTTPClient.Create(nil);
  61.   httpClient.AddHeader('Accept','application/json');
  62.   httpClient.AddHeader('Content-Type','application/json');
  63.   httpClient.RequestBody := TStringStream.Create('{"id": "'+id+'"}');
  64.       try
  65. //      S:= httpClient.Get(Ed_RestSvr.Text);
  66.       S := httpClient.Post('https://httpbin.org/post');
  67.       memo1.Append('Request Response : ' + httpClient.ResponseStatusText);
  68.       except
  69.       on e: Exception do
  70.           begin
  71.           ShowMessage('Exception : ' + e.Message);
  72.           httpClient.Free;
  73.           Exit;
  74.           end;
  75.       end;
  76.     httpClient.Free;
  77.     J:= GetJSON(S);
  78.     DumpJsonData(J);
  79. end;

Response :
Code: Pascal  [Select][+][-]
  1. Request Response : OK
  2. ========================
  3. "args" :
  4. ========================
  5. =========================
  6. +++++++++++++++++++++++++
  7. "data" :
  8. "{"id": "5"}"
  9. +++++++++++++++++++++++++
  10. "files" :
  11. ========================
  12. =========================
  13. +++++++++++++++++++++++++
  14. "form" :
  15. ========================
  16. =========================
  17. +++++++++++++++++++++++++
  18. "headers" :
  19. ========================
  20. "Accept" :
  21. "application/json"
  22. +++++++++++++++++++++++++
  23. "Content-Length" :
  24. "11"
  25. +++++++++++++++++++++++++
  26. "Content-Type" :
  27. "application/json"
  28. +++++++++++++++++++++++++
  29. "Host" :
  30. "httpbin.org"
  31. =========================
  32. +++++++++++++++++++++++++
  33. "json" :
  34. ========================
  35. "id" :
  36. "5"
  37. =========================
  38. +++++++++++++++++++++++++
  39. "origin" :
  40. "99.98.97.96"
  41. +++++++++++++++++++++++++
  42. "url" :
  43. "https://httpbin.org/post"
  44. =========================
  45.  

for a get request : 
Code: Pascal  [Select][+][-]
  1. S:= httpClient.Get('http://jsonplaceholder.typicode.com/posts');
  2.  // S := httpClient.Post(Ed_RestSvr.Text);  

Friendly, J.P
« Last Edit: April 01, 2016, 01:17:12 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

LuizAmérico

  • Sr. Member
  • ****
  • Posts: 457
Re: TRESTClient availability in FPC/Lazarus
« Reply #11 on: April 01, 2016, 02:41:29 am »
Here you have client and server components that can be used together or separated.
https://github.com/blikblum/luipack/tree/master/luirest

Examples at:
Server:
https://github.com/blikblum/luipack/tree/master/luirest/demos/addressbook/sqldbcollection
Client:
app: https://github.com/blikblum/luipack/tree/master/luirest/demos/addressbook/lclclient2
tests: https://github.com/blikblum/luipack/tree/master/luicomponents/tests/restclient See https://github.com/blikblum/luipack/blob/master/luicomponents/tests/sqlite3client/jsonresourcetests.pas for the actual tests

There's also client for sqldb client with the same interface to allow easy migration from direct database access to a rest service


 

TinyPortal © 2005-2018