Recent

Author Topic: SOLVED Could someone translate this Javascript API to Lazarus/FP please  (Read 1093 times)

Ten_Mile_Hike

  • Jr. Member
  • **
  • Posts: 63
As the Subject states.
---I'm using Win10 + Latest Laz release

Code: Javascript  [Select][+][-]
  1. const endpoint = "https://earthquake.usgs.gov/fdsnws/event/1/query";
  2. // Set query parameters
  3. const params = {
  4.   starttime: "2021-01-01",
  5.   endtime: "2021-02-01",
  6.   minmagnitude: 6.0,
  7.   latitude: 34.0522,
  8.   longitude: -118.2437,
  9.   maxradiuskm: 1000,
  10.   orderby: "time"
  11. };
  12.  
  13. // Construct query string
  14. const queryString = Object.keys(params)
  15.   .map(key => key + "=" + encodeURIComponent(params[key]))
  16.   .join("&");
  17.  
  18. // Make API request with fetch
  19. fetch(endpoint + "?" + queryString)
  20.   .then(response => response.json())
  21.   .then(data => {
  22.     console.log(data);
  23.   });
« Last Edit: May 10, 2024, 04:00:49 pm by Ten_Mile_Hike »

jamie

  • Hero Member
  • *****
  • Posts: 6301
Re: Could someone translate this Javascript API to Lazarus/FP please
« Reply #1 on: May 09, 2024, 12:53:02 am »
Go here and study.
https://wiki.lazarus.freepascal.org/fphttpclient
Install the "fpWeb" and you can use the TFpHttpClient.

The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 2790
Re: Could someone translate this Javascript API to Lazarus/FP please
« Reply #2 on: May 09, 2024, 08:33:36 am »
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  [Select][+][-]
  1. const
  2.   endpoint = 'https://earthquake.usgs.gov/fdsnws/event/1/query';
  3.  
  4.   params =
  5.   '{' +
  6.    '"starttime": "2021-01-01",' +
  7.    '"endtime": "2021-02-01",' +
  8.    '"minmagnitude": 6.0,' +
  9.    '"latitude": 34.0522,' +
  10.    '"longitude": -118.2437,' +
  11.    '"maxradiuskm": 1000,' +
  12.    '"orderby": "time"' +
  13.   '}';
  14.  
  15. function fetch(const aURL: string): string;
  16. var
  17.   Client: TFPHTTPClient;
  18. begin
  19.   fetch:= '';
  20.   Client:= TFPHTTPClient.Create(nil);
  21.   try
  22.     CLient.AllowRedirect:= true;
  23.     fetch:= Client.Get(aURL);
  24.   finally
  25.     Client.Free;
  26.   end;
  27. end;
  28.  
  29. function encodeURIComponent(const aComponent: string): string;
  30. begin
  31.   // TODO: properly encode URI component.
  32.   // See also:
  33.   // - https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
  34.   // - https://en.wikipedia.org/wiki/Query_string#URL_encoding
  35.   encodeURIComponent:= aComponent;
  36. end;
  37.  
  38. function Params2Query(const aParams: string): string;
  39. var
  40.   JSONParams: TJSONData;
  41.   JSONParam: TJSONEnum;
  42.   key, value: string;
  43. begin
  44.   Params2Query:= '';
  45.   JSONParams:= getJSON(aParams);
  46.  
  47.   for JSONParam in JSONParams do
  48.   begin
  49.     key:= JSONParam.Key;
  50.     case JSONParam.value.JSONType of
  51.       jtNumber, jtstring, jtboolean: Writestr(value, JSONParam.Value.Value);
  52.       else value:= '';
  53.     end;
  54.     Params2Query:= Params2Query + JSONParam.Key + '=' + encodeURIComponent(value);
  55.     if JSONParam.KeyNum < pred(JSONParams.Count)
  56.       then Params2Query := Params2Query + '&';
  57.   end;
  58.  
  59.   JSONParams.Free;
  60. end;
  61.  
  62. var
  63.   queryString: string;
  64.   response: string;
  65.   responseAsJSON: TJSONData;
  66. begin
  67.   queryString:= Params2Query(params);
  68.   response:= fetch(endpoint + '?' + queryString);
  69.   responseAsJSON:= getJSON(response);
  70.   writeln(responseAsJSON.FormatJSON);
  71.   responseAsJSON.Free;
  72. end.
  73.  

Ten_Mile_Hike

  • Jr. Member
  • **
  • Posts: 63
Re: Could someone translate this Javascript API to Lazarus/FP please
« Reply #3 on: May 10, 2024, 03:57:47 pm »
Thank you TRon and Jamie.

Dzandaa

  • Sr. Member
  • ****
  • Posts: 316
  • From C# to Lazarus
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->

Regards,
Dzandaa

jamie

  • Hero Member
  • *****
  • Posts: 6301
is that one of those things you need a registration key from google ?
The only true wisdom is knowing you know nothing

Dzandaa

  • Sr. Member
  • ****
  • Posts: 316
  • From C# to Lazarus
Regards,
Dzandaa

jamie

  • Hero Member
  • *****
  • Posts: 6301
Hmm, that gives me an idea to add to a program I have here used on Ham Radio!  :)
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018