Delphi Using Apilayer REST APIs from Delphi

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,203
Credits
4,034
Using Apilayer REST APIs from Delphi
January 25, 2021 by Marco Cantu

As you might have seen online, Idera (Embarcadero parent company) recently Для просмотра ссылки Войди или Зарегистрируйсяapilayer, a company offering a number of REST API micro-services, ranging from IP address geolocation to email and phone number checks, from financial information to weather and flight tracking. You can find a list of the company products at Для просмотра ссылки Войди или Зарегистрируйся. Each product has itw own web site, where you can generally sign in for a account with a limited number of free REST calls.
1611603362646.png
In this blog post, I don’t want to discuss the available services, but focus on how to call them from RAD Studio. I’ll use only two services, the free and open REST countries API and one of their premium geolocation services.

Using the REST Countries API​

My starting point for exploring apilayer services is a simple and totally free service, available along with full documentation at Для просмотра ссылки Войди или Зарегистрируйся. To make an initial experiment, I’ve used the REST Debugger to query the service, using the name endpoint and optionally passing a parameter, as you can see below (with the parameter value “united”):

1611603374996.png
Once the data looks correct in the REST Debugger, you can just use the Copy Components button to make a snapshot of the configuration for the REST Client Library component required for building an application. Now, create a Delphi or C++ application in RAD Studio, either VCL or FMX, and paste the components in a data module — or a form if you are lazy. In this case I’ve used Delphi and VCL… and I was lazy.

I dropped a panel with an edit and a button and DBGrid (plus a DataSource component), wired them up, and executed the RESTRequest at design time to get a preview of the data:
1611603393824.png
The code for filtering is very simple:
Код:
procedure TForm43.Button1Click(Sender: TObject);
begin
  if Edit1.Text = ” then
    RESTRequest1.Resource := ‘name’
  else
    RESTRequest1.Resource := ‘name/’ + Edit1.Text;
    RESTRequest1.Execute;
end;
The only other change I had to do was to change the TypesMode property of the RESTResponseDataSetAdapter1 component to JSONOnly, as some of the data analysis was mistakenly trying to convert some timezone related text to a date, resulting in an exception.

Reverse GeoCoding with the Position Stack API​

Next I made an attempt at using the free level of a paid API, Для просмотра ссылки Войди или Зарегистрируйся. This service offers direct and inverse geocoding, mapping services, and more. Here for example I’ve done a simple “forward” geocoding, providing an address and reading the latitude, longitude, and other local information in the REST Debugger. To obtain the tabular result I had to configure the JSON root using the data element:
1611603460227.png
In this case, you need to sign up for a developer key and enter it as an additional parameter for the request. (You might want to consider encoding that key rather than having it as a plain string in the final application.)

Now, similarly to what I’ve done above, we can copy the components into a RAD Studio application and have a geo coding FMX app good to go, this time based on Live Bindings. Also this app can display data at design time, in this case the latitude and longitude of my city (plus another city with the same name in the USA):
1611603473427.png

Conclusion​

Leveraging micro-service APIs for application development, both desktop and mobile, can really speed up building and deploying innovative features, and have them on a scalable infrastructure at the same time. I’m looking forward to leverage more of the Для просмотра ссылки Войди или Зарегистрируйся and encourage you to have a look at what they can offer for your current of future projects.