-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStatHat.pas
93 lines (79 loc) · 1.98 KB
/
StatHat.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
unit StatHat;
interface
uses
Classes;
type
TErrorHandler = reference to procedure(ErrorMessage: string);
procedure EzPostCount(const EzKey, Stat: string; Count: Double; ErrorHandler: TErrorHandler = nil);
procedure EzPostValue(const EzKey, Stat: string; Value: Double; ErrorHandler: TErrorHandler = nil);
implementation
uses
IdHTTP,
SysUtils;
const
EzURL = 'http://api.stathat.com/ez';
procedure Post(Params: TStrings);
var
HTTP: TIdHTTP;
begin
HTTP := TIdHTTP.Create(nil);
try
HTTP.Post(EzURL, Params);
finally
HTTP.Free;
end;
end;
function ConvertDouble(Value: Double): string;
var
FS: TFormatSettings;
begin
FS.DecimalSeparator := '.';
Result := FloatToStr(Value, FS);
end;
procedure EzPost(const EzKey, Stat, StatType: string; StatValue: Double; ErrorHandler: TErrorHandler);
var
Params: TStringList;
ErrorMessage: string;
begin
Params := TStringList.Create;
try
try
Params.Values['ezkey'] := EzKey;
Params.Values['stat'] := Stat;
Params.Values[StatType] := ConvertDouble(StatValue);
Post(Params);
except
on e: Exception do
begin
ErrorMessage := e.Message;
if Assigned(ErrorHandler) then
begin
TThread.Synchronize(nil,
procedure
begin
ErrorHandler(ErrorMessage)
end);
end;
end;
end;
finally
Params.Free;
end;
end;
procedure EzPostAsync(const EzKey, Stat, StatType: string; StatValue: Double; ErrorHandler: TErrorHandler);
begin
TThread.CreateAnonymousThread(
procedure
begin
EzPost(EzKey, Stat, StatType, StatValue, ErrorHandler);
end).Start;
end;
procedure EzPostCount(const EzKey, Stat: string; Count: Double; ErrorHandler: TErrorHandler = nil);
begin
EzPostAsync(EzKey, Stat, 'count', Count, ErrorHandler);
end;
procedure EzPostValue(const EzKey, Stat: string; Value: Double; ErrorHandler: TErrorHandler = nil);
begin
EzPostAsync(EzKey, Stat, 'value', Value, ErrorHandler);
end;
end.