-
Notifications
You must be signed in to change notification settings - Fork 4
/
ProwlClient.cs
113 lines (94 loc) · 2.89 KB
/
ProwlClient.cs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
using Crestron.SimplSharp.Net.Http;
using Crestron.SimplSharp.Net.Https;
namespace ProwlSimplSharp
{
public class ProwlClient : HttpsClient
{
private List<string> _apiKeys;
private object ApiKeysLock = new object();
public ProwlClient() : base()
{
_apiKeys = new List<string>();
this.PeerVerification = false;
}
private string ApiKeys
{
get
{
lock (ApiKeysLock)
{
return string.Join(",", _apiKeys.ToArray());
}
}
}
public int Send(string app, short priority, string url, string subject, string message)
{
if (string.IsNullOrEmpty(app))
app = string.Empty;
if (string.IsNullOrEmpty(url))
url = string.Empty;
if (string.IsNullOrEmpty(subject))
subject = string.Empty;
if (string.IsNullOrEmpty(message))
message = string.Empty;
string keys = this.ApiKeys;
if (String.IsNullOrEmpty(keys))
{
return -1;
}
var parameters = new Dictionary<string, string>()
{{ "apikey", keys },
{ "application", app },
{ "priority", priority.ToString() },
{ "url", url },
{ "event", subject },
{ "description", message }};
var request = new ProwlRequest("add", parameters);
return ProwlDispatch(request);
}
private int ProwlDispatch(ProwlRequest request)
{
try
{
HttpsClientResponse response = Dispatch(request);
return response.Code;
}
catch (HttpsException e)
{
ErrorLog.Exception("Got exception dispatching Prowl request", e);
return 0;
}
}
private bool IsValidKeyFormat(string key)
{
return !String.IsNullOrEmpty(key) && key.Length.Equals(40); // Order important here
}
public int AddApiKey(string apiKey)
{
if (IsValidKeyFormat(apiKey))
{
lock (ApiKeysLock)
{
_apiKeys.Add(apiKey);
}
return 1;
}
else
{
return 0;
}
}
public void RemoveApiKey(string key)
{
lock (ApiKeysLock)
{
_apiKeys.Remove(key);
}
}
}
}