Forum > General

SOLVED Could someone translate this Javascript API to Lazarus/FP please

(1/2) > >>

Ten_Mile_Hike:
As the Subject states.
---I'm using Win10 + Latest Laz release


--- Code: Javascript  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const endpoint = "https://earthquake.usgs.gov/fdsnws/event/1/query";// Set query parametersconst params = {  starttime: "2021-01-01",  endtime: "2021-02-01",  minmagnitude: 6.0,  latitude: 34.0522,  longitude: -118.2437,  maxradiuskm: 1000,  orderby: "time"}; // Construct query stringconst queryString = Object.keys(params)  .map(key => key + "=" + encodeURIComponent(params[key]))  .join("&"); // Make API request with fetchfetch(endpoint + "?" + queryString)  .then(response => response.json())  .then(data => {    console.log(data);  });

jamie:
Go here and study.
https://wiki.lazarus.freepascal.org/fphttpclient
Install the "fpWeb" and you can use the TFpHttpClient.

TRon:
Perhaps the following approach will suffice.

Note that there is no error checking (which is really mandatory) thus would have to implement that yourself and I left the encoding as an exercise (FPC does not seem to come shipped with (proper) parameter encoding.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const  endpoint = 'https://earthquake.usgs.gov/fdsnws/event/1/query';   params =  '{' +   '"starttime": "2021-01-01",' +   '"endtime": "2021-02-01",' +   '"minmagnitude": 6.0,' +   '"latitude": 34.0522,' +   '"longitude": -118.2437,' +   '"maxradiuskm": 1000,' +   '"orderby": "time"' +  '}'; function fetch(const aURL: string): string;var  Client: TFPHTTPClient;begin  fetch:= '';  Client:= TFPHTTPClient.Create(nil);  try    CLient.AllowRedirect:= true;    fetch:= Client.Get(aURL);  finally    Client.Free;  end;end; function encodeURIComponent(const aComponent: string): string;begin  // TODO: properly encode URI component.  // See also:  // - https://en.wikipedia.org/wiki/Uniform_Resource_Identifier  // - https://en.wikipedia.org/wiki/Query_string#URL_encoding  encodeURIComponent:= aComponent;end; function Params2Query(const aParams: string): string;var  JSONParams: TJSONData;  JSONParam: TJSONEnum;  key, value: string;begin  Params2Query:= '';  JSONParams:= getJSON(aParams);   for JSONParam in JSONParams do  begin    key:= JSONParam.Key;    case JSONParam.value.JSONType of      jtNumber, jtstring, jtboolean: Writestr(value, JSONParam.Value.Value);      else value:= '';    end;    Params2Query:= Params2Query + JSONParam.Key + '=' + encodeURIComponent(value);    if JSONParam.KeyNum < pred(JSONParams.Count)      then Params2Query := Params2Query + '&';  end;   JSONParams.Free;end; var  queryString: string;  response: string;  responseAsJSON: TJSONData;begin  queryString:= Params2Query(params);  response:= fetch(endpoint + '?' + queryString);  responseAsJSON:= getJSON(response);  writeln(responseAsJSON.FormatJSON);  responseAsJSON.Free;end. 

Ten_Mile_Hike:
Thank you TRon and Jamie.

Dzandaa:
Hi,

I wrote this little program to show earthquakes with LazMapViewer. Feel free to modify it (correct it :) )

There maybe/surely be unfreed memory :)

The Right button mark the center (Lat. Long) of the search.
The Middle button Show Info when clicking on an earthquake.
The Left button is panning and the middle wheel for Zooming.

Try to limit your search to 50-100km .

Have a nice day.

B->

Navigation

[0] Message Index

[#] Next page

Go to full version