Sending REST API messages with Delphi to Parse.com
Parse.com is one of the most interesting cloud base solutions I have found so far, and most importantly; basic accounts are for free (up to 30 requests/second, after that is when you have to pay). Using Parse.com I can keep track of my applications, using the Analytics feature. This is an easy way of knowing how many installs you have running and also it provides cloud storage to add some more details etc. I'm using this module for my project "Flickr Photo Analytics" as I currently have over thousand downloads but I don't have a clue whether the app is being used or not. Here is where the Analytics feature of parse.com gets really handy.
Embarcadero has its own AppAnalytics but I just wanted something that I could handle on my own with an easy setup. I have been using Parse for a while for few mobile applications and now I've decided to extend it to my windows desktop applications.
The idea is very simple. Every time the application is used, it will send a notification to Parse.com via REST API. Parse.com provides several communication protocols for different languages e.g. Java, Javascript, .NET, etc. but unfortunately not for Delphi. Here is why I decided to write about it as I haven't seen anything on the Internet and it could be interesting for many users out there.
To use it in Delphi, we can use the REST API via IdHTTP component. On their website they provide the syntax using curl and here is the correct translation to use it with Delphi and that works:
Curl:
Delphi:
With just this little piece of code, the Analytics feature of Parse will start tracking down your installs. It's not a real time tracking so it takes time to appear on the dashboard. Once you have your applications there, you can review the stats:
As I'm not expecting any traffic at all in my app it is really a good solution and it serves my needs. I also have a small android app that pulls from there so I can read some of the stats published by my app anywhere I go. In the end it all lies to connectivity and what's the best and easy way to achieve it. Using this approach didn't take me long at all.
I hope it can be useful to the community.
PS: If you know any better solution out there, please do let me know!.
Jordi
I've been doing most HTTP calls with the XMLHTTP object from MSXML2_TLB.pas , MSXML has been reliably installed in most computers since IE5.5
ReplyDeleteThat's a valid solution also. I stopped using Msxml long back and using Indy components instead for this.
DeleteThese days it's safer to use Wininet or WinHTTP whenever https is involved, both support latest TLS versions and certificate validations. Indy relies on a bundled OpenSSL, which is problematic as it is not going to be easily (and timely updated).
ReplyDeleteThanks Eric. I will definitely try using wininet or WinHTTP as I know that indy relies on OpenSSL and it is problematic.
DeleteJordi
This comment has been removed by the author.
DeleteTExtendParseApi = class(TParseApi)
ReplyDeletepublic
procedure SendUsageStat;
end;
//curl -X POST \
// -H "X-Parse-Application-Id: yourAppId" \
// -H "X-Parse-REST-API-Key: yourRESTAPIKey" \
// -H "Content-Type: application/json" \
// -d '{
// }' \
// https://api.parse.com/1/events/AppOpened
procedure TExtendParseApi.SendUsageStat;
var
LJSON: TJSONObject;
begin
try
Request.ResetToDefaults;
AddAuthParameters;
Request.Method := TRESTRequestMethod.rmPOST;
Request.Resource := 'events/AppOpened';
LJSON := TJSONObject.Create;
try
Request.AddBody(LJSON);
Request.Execute;
CheckForResponseError([201]);
finally
LJSON.Free;
end;
except
end;
end;
and on create do
FApi := TExtendParseAPI.Create(Self);
ParseProvider1.UpdateApi(FApi);
Excellent implementation, many thanks. I'll try to use it.
DeleteRegards,
Jordi