Recent

Author Topic: Using REST API's  (Read 12457 times)

anon101

  • New Member
  • *
  • Posts: 13
Using REST API's
« on: November 04, 2018, 10:35:28 am »
Hey guys,
I'm new to this forum (and Lazarus) so sorry if I've posted this in the wrong place or done anything else wrong here.
I'm planning to create a program which will use Trello's API to receive information on a specific card (e.g. labels, name/title, etc) and use it for something else. However, I'm confused about how to go about using their API in Lazarus (I've never really attempted to do web-based programming before, and normally just stick to Java (or another language I've learnt in the past) to do everything, however, I was recently intrigued by Lazarus's ability to compile a GUI nearly anywhere).
So any help explaining how to go about using their API to collect this data for a desktop program would be greatly appreciated.
Thanks so much!!!

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Using REST API's
« Reply #1 on: November 04, 2018, 05:58:29 pm »
Welcome to the forum and use the search functionality, limiting the search to this particular subforum. REST API consumption is quite common task and has been asked and answered quite a few times. Comeback when you're stuck with what you have tried so far.

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Using REST API's
« Reply #2 on: November 04, 2018, 06:23:52 pm »
A good example is the google api's. That comes with a rather easy to use reflector/converter for the google rest api's.
You can probably use it for any rest api. (but this is not quite suitable for real beginners)
Specialize a type, not a var.

anon101

  • New Member
  • *
  • Posts: 13
Re: Using REST API's
« Reply #3 on: November 04, 2018, 09:53:37 pm »
Hi Thaddy,
I'm not quite sure about what you mean when you say a reflector/converter.
Would you have a guide or anything you can refer me to?

I have googled this, and all I need is to use a GET request.
But how do I do this with Lazarus/Free Pascal?

I have myself looked this up to try and find what I can, and it appears there are several ways to go about this (I think), I need to use HTTPS connections, and there seem to be several external libraries like Synapse and Indy to do so. But isn't there something already in Lazarus which would easily allow me to use the GET and POST requests (as I want it to be able to run on as many platforms as possible)?

Thanks for helping!!!

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Using REST API's
« Reply #4 on: November 04, 2018, 11:37:46 pm »
Built in libraries, other than the lower-level sockets and fcl-net, start p.e. with FPC's fcl-web (folder {FPC-Src}/packages/fcl-web/)--specifically, for you, fphttpclient--and follow with the various Lazarus components which use it to implement higher level clients (like the mentioned googleapis, etc.)

These may be enough for quick development or to give you an idea, but beware that most serious web development is actually done with Synapse or Indy; you should become used to them if you plan to do it long-term.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Using REST API's
« Reply #5 on: November 05, 2018, 07:47:11 am »
These may be enough for quick development or to give you an idea, but beware that most serious web development is actually done with Synapse or Indy; you should become used to them if you plan to do it long-term.
I beg to differ, both are much used because of legacy.
The fcl-web and fpc-net packages are under serious development with the aim to remove the third party dependencies.
As it stands there is a lot of code and knowledge out there that use synapse or Indy, the expertise on fcl-web and fcl-net is still a bit less, but:
- fcl-web and fcl-net have close to the same feature set as Synapse for the sockets and http(s) side of things and a more modern - although similar - architecture.
- Indy has the advantage (or disadvantage!) that it has a huge set of components, which is ideal for beginners.

The more serious web related development inside the FPC core packages like pas2js  is all based on the newer fcl-web and fcl-net.

To summarize: all development related to web inside the fpc core packages is done with fcl-web/net which in itself is well maintained and with a growing feature set.....
I do almost all new development with it, not least because it supports more platforms, unless I need to maintain Delphi compatibility in which case I prefer synapse over Indy.
« Last Edit: November 05, 2018, 08:12:59 am by Thaddy »
Specialize a type, not a var.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Using REST API's
« Reply #6 on: November 05, 2018, 10:44:43 am »
I'm planning to create a program which will use Trello's API to receive information on a specific card
If you want quick results then there is a commercial 3rd party lib to consider:
https://forum.lazarus.freepascal.org/index.php/topic,30423.msg193639.html#msg193639
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Iwoks

  • Newbie
  • Posts: 2
Re: Using REST API's
« Reply #7 on: November 05, 2018, 11:04:03 am »
Hay guys,

  I'm using Wekan. (Nodejs)
  In 2019, I'm planning to create a program which will use Wekan API to receive information on a specific card

  https://github.com/wekan/wekan/wiki/REST-API


anon101

  • New Member
  • *
  • Posts: 13
Re: Using REST API's
« Reply #8 on: November 06, 2018, 08:30:16 am »
Built in libraries, other than the lower-level sockets and fcl-net, start p.e. with FPC's fcl-web (folder {FPC-Src}/packages/fcl-web/)--specifically, for you, fphttpclient--and follow with the various Lazarus components which use it to implement higher level clients (like the mentioned googleapis, etc.)

These may be enough for quick development or to give you an idea, but beware that most serious web development is actually done with Synapse or Indy; you should become used to them if you plan to do it long-term.

So fphttpclient is built in and thus what I'm looking to use?
Thanks so much and sorry if I've taken a while to reply (I've got VCE exams right now and aim to start up my small project after they're over)

anon101

  • New Member
  • *
  • Posts: 13
Re: Using REST API's
« Reply #9 on: November 06, 2018, 08:33:01 am »
These may be enough for quick development or to give you an idea, but beware that most serious web development is actually done with Synapse or Indy; you should become used to them if you plan to do it long-term.
I beg to differ, both are much used because of legacy.
The fcl-web and fpc-net packages are under serious development with the aim to remove the third party dependencies.
As it stands there is a lot of code and knowledge out there that use synapse or Indy, the expertise on fcl-web and fcl-net is still a bit less, but:
- fcl-web and fcl-net have close to the same feature set as Synapse for the sockets and http(s) side of things and a more modern - although similar - architecture.
- Indy has the advantage (or disadvantage!) that it has a huge set of components, which is ideal for beginners.

The more serious web related development inside the FPC core packages like pas2js  is all based on the newer fcl-web and fcl-net.

To summarize: all development related to web inside the fpc core packages is done with fcl-web/net which in itself is well maintained and with a growing feature set.....
I do almost all new development with it, not least because it supports more platforms, unless I need to maintain Delphi compatibility in which case I prefer synapse over Indy.

So this would have the basic and most importantly reliable resources I'd need to fairly easily use GET and POST http requests for the REST API's?
Thanks for helping me by the way!!!

anon101

  • New Member
  • *
  • Posts: 13
Re: Using REST API's
« Reply #10 on: November 06, 2018, 08:39:43 am »
Hay guys,

  I'm using Wekan. (Nodejs)
  In 2019, I'm planning to create a program which will use Wekan API to receive information on a specific card

  https://github.com/wekan/wekan/wiki/REST-API
So this may be a little off track here, but since you've mentioned Wekan, would you be able to tell me a little about it (like how it compares to the KanBan Flow and Trello, whether it can integrate with other web services like Clockify (that's what I'm looking for) and whether it syncs with a Google or Microsoft account). Just because I'd actually consider switching if it's got decent features since it's open source and I'm an advocate of that!!!

Thanks a load for telling me about that though. I'll definitely keep Wekan in my bookmarks bar.

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Using REST API's
« Reply #11 on: November 06, 2018, 02:52:13 pm »
So this would have the basic and most importantly reliable resources I'd need to fairly easily use GET and POST http requests for the REST API's?
Thanks for helping me by the way!!!

Yes. For example that's how the google api components work. Including the google api importer, which is an extremely good example of an automated REST api converter.
Here's a nice article Michael van Canneyt  wrote: https://www.freepascal.org/~michael/articles/googleapi/googleapi.pdf
That should get you started with any REST api.
Specialize a type, not a var.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Using REST API's
« Reply #12 on: November 06, 2018, 07:46:23 pm »
Here I used REST to connect to Paymo API
https://github.com/Arandusoft/fpcpaymowidget

Here is the core
https://github.com/Arandusoft/fpcpaymowidget/blob/master/upaymo.pas

Using FPHttpClient.

kevin.black

  • Full Member
  • ***
  • Posts: 121
Re: Using REST API's
« Reply #13 on: February 14, 2019, 12:19:40 am »
@Avra,
If you want quick results then there is a commercial 3rd party lib to consider:
https://forum.lazarus.freepascal.org/index.php/topic,30423.msg193639.html#msg193639

OK, so I am coming from Delphi (FMX no Darwin 32bit compiler). I have purchased the full TMS Cloud Pack which includes the LCL version. I have installed LCL version into Lazarus (As a noob I have other issues that are causing me grief). I can confirm that it does install (there are issues with multiple occurrences of some files) the REST functions are quite simple. If you want to connect to one of the myriad of cloud services they are all built in. I use TMS functions for 'normal' REST calls and I use their functions to decode the JSON returns.

Having said that TMS have done no development on the LCL package since 2015. I queried this and was told as far as they were concerned nothing has change, meh. They did say they were looking at an update, they always say that. I suspect the multiple occurrences of files when I also install SYNACODE is because TMS haven't looked at this package for 3-4 years.

Further, I am using (from Delphi/Win) these components in a DLL and trying to convert to a DYLIB (Lazarus) on Darwin. I have some issues and when i asked TMS they pretty much ignored me. Well TBF they did get back, their response: we have not tested our components in a DLL, we defiantly have not tested our components in a DYLIB. We do not intend to test in a DLL or DYLIB, that would be too much work and there has been no calls for such (there has been - me). So I have the package installed, I haven't got to the point where I'm using it yet, to many other things are breaking/not working, but I suspect much of that is my lack of competence.

Anyway, you get the source with the purchase, and for the most part I have been able to code my way around. The Do.Connect function did not work in XE7 in a DLL so I had to craft a forms based app that talked to the DLL by named pipes to pass the authentication credentials (no help from TMS, we don't do DLLs). It worked again in XE10.2.3 Tokyo.

So bottom line, yes available through TMS. Depending on what you do support is OK or non-existent. LCL packages are OLD.

You can download a trial of the LCL Package/components and see what they do, but if you want functions for generic REST related tasks, you probably need the source. You can also download the user manual from their site, but I'd suggest it's somewhat sparse. It will give you an idea of how to use it (for the named cloud services). FWIW, here's a sample of some (DELPHI) code to do a Dropbox search for a file (and you can just amend the parameters for a non-Dropbox request for example, it would probably be the same or similar in FPC/Lazarus):
Code: Pascal  [Select][+][-]
  1.       URL := DROPBOX_BASE_URL + '/files/search';
  2.       AddHeader(headers, 'Authorization', 'Bearer ' + AccessToken);
  3.       AddHeader(headers, 'Content-Type', 'application/json');
  4.       postdata := '{'
  5.                 + '"path": "' + filePath + '",'
  6.                 + '"query": "' + fIleName + '",'
  7.                 + '"mode": "filename"'
  8.                 + '}';
  9.       tJSONReturn := '';
  10.       ReturnCode := TMSDropbox.HTTPSPost(TMSDropbox.ExtractServer(URL), TMSDropbox.RemoveServer(URL), headers, postdata, response);
  11.       if ReturnCode <> 200 then
  12.       begin

Of course YMMV.

Kevin

 

TinyPortal © 2005-2018