forked from PedroCavaleiro/whmcs-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.cs
280 lines (231 loc) · 12.4 KB
/
API.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace WHMCS_API
{
public class API
{
JsonSerializerSettings settings;
private readonly Call _call;
public API(string Identifier, string Secret, string Url)
: this (Identifier, Secret, "", Url)
{
}
public API(string Identifier, string Secret, string AccessKey, string Url)
{
if(AccessKey == "") {
_call = new Call(Identifier, Secret, Url);
} else {
_call = new Call(Identifier, Secret, AccessKey, Url);
}
settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
}
public ValidateLogin.ValidateLogin ValidateLogin(string Email, string Password)
{
NameValueCollection data = new NameValueCollection()
{
{ "action", APIEnums.Actions.ValidateLogin.ToString() },
{ EnumUtil.GetString(APIEnums.ValidateLoginParams.Email), Email },
{ EnumUtil.GetString(APIEnums.ValidateLoginParams.Password), Password }
};
string req = _call.MakeCall(data);
JObject result = JObject.Parse(req);
if (result["result"].ToString() == "success")
return JsonConvert.DeserializeObject<ValidateLogin.ValidateLogin>(req, settings);
else
throw new Exception("An API Error Ocurred", new Exception(result["message"].ToString()));
}
public DomainWhoIs DomainWhoIs(string Domain)
{
NameValueCollection data = new NameValueCollection()
{
{ "action", APIEnums.Actions.DomainWhois.ToString() },
{ EnumUtil.GetString(APIEnums.DomainWhoisParams.Domain), Domain },
};
string req = _call.MakeCall(data);
JObject result = JObject.Parse(req);
if (result["result"].ToString() == "success")
return JsonConvert.DeserializeObject<DomainWhoIs>(req, settings);
else
throw new Exception("An API Error Ocurred", new Exception(result["message"].ToString()));
}
public int AddClient(AddClient ClientInfo)
{
NameValueCollection data = new NameValueCollection()
{
{ "action", APIEnums.Actions.AddClient.ToString() }
};
//Processes all the data in ClientInfo model into the data NameValueCollection
foreach (string key in ClientInfo.ClientInfo)
{
data.Add(key, ClientInfo.ClientInfo[key]);
}
JObject result = JObject.Parse(_call.MakeCall(data));
if (result["result"].ToString() == "success")
return Convert.ToInt32(result["clientid"]);
else
throw new Exception("An API Error Ocurred", new Exception(result["message"].ToString()));
}
public GetClientsDetails.GetClientsDetails GetClientsDetails(int ClientID = -1, string ClientEmail = "", bool Stats = false)
{
if (ClientID == -1 && ClientEmail == "")
throw new Exception("ClientID or ClientEmail needed");
NameValueCollection data = new NameValueCollection()
{
{ "action", APIEnums.Actions.GetClientsDetails.ToString() },
{ EnumUtil.GetString(APIEnums.GetClientsDetailsParams.Stats), Stats.ToString() },
};
if (ClientID != -1)
data.Add(EnumUtil.GetString(APIEnums.GetClientsDetailsParams.ClientID), ClientID.ToString());
if (ClientEmail != "" && ClientID == -1)
data.Add(EnumUtil.GetString(APIEnums.GetClientsDetailsParams.Email), ClientEmail);
string req = _call.MakeCall(data);
JObject result = JObject.Parse(req);
if (result["result"].ToString() == "success")
return JsonConvert.DeserializeObject<GetClientsDetails.GetClientsDetails>(req, settings);
else
throw new Exception("An API Error occurred", new Exception(result["message"].ToString()));
}
public GetOrders.GetOrders GetOrders(int LimitStart = 0, int LimitNumber = 25, int OrderID = -1, int UserID = -1, string Status = "")
{
NameValueCollection data = new NameValueCollection()
{
{ "action", APIEnums.Actions.GetOrders.ToString() },
{ EnumUtil.GetString(APIEnums.GetOrdersParams.LimitStart), LimitStart.ToString() },
{ EnumUtil.GetString(APIEnums.GetOrdersParams.LimitNumber), LimitNumber.ToString() }
};
if (OrderID != -1)
data.Add(EnumUtil.GetString(APIEnums.GetOrdersParams.OrderID), OrderID.ToString());
if (UserID != -1)
data.Add(EnumUtil.GetString(APIEnums.GetOrdersParams.UserID), UserID.ToString());
if (Status != "")
data.Add(EnumUtil.GetString(APIEnums.GetOrdersParams.Status), Status);
return JsonConvert.DeserializeObject<GetOrders.GetOrders>(_call.MakeCall(data), settings);
}
public GetTransactions.GetTransactions GetTransactions(int InvoiceID = -1, int ClientID = -1, string TransactionID = "")
{
NameValueCollection data = new NameValueCollection()
{
{ "action", APIEnums.Actions.GetTransactions.ToString() }
};
if (InvoiceID != -1)
data.Add(EnumUtil.GetString(APIEnums.GetTransactionsParams.InvoiceID), InvoiceID.ToString());
if (ClientID != -1)
data.Add(EnumUtil.GetString(APIEnums.GetTransactionsParams.ClientID), ClientID.ToString());
if (TransactionID != "")
data.Add(EnumUtil.GetString(APIEnums.GetTransactionsParams.TransactionID), TransactionID);
return JsonConvert.DeserializeObject<GetTransactions.GetTransactions>(_call.MakeCall(data), settings);
}
public GetClientsProducts.GetClientsProducts GetClientsProducts(int LimitStart = 0, int LimitNum = 25, int ClientID = -1, int ServiceID = -1, int ProductID = -1, string Domain = "", string Username = "")
{
NameValueCollection data = new NameValueCollection()
{
{ "action", APIEnums.Actions.GetClientsProducts.ToString() },
{ EnumUtil.GetString(APIEnums.GetClientsProductsParams.ResultsStartOffset), LimitStart.ToString()},
{ EnumUtil.GetString(APIEnums.GetClientsProductsParams.ResultsLimit), LimitNum.ToString()},
};
if (ClientID != -1)
data.Add(EnumUtil.GetString(APIEnums.GetClientsProductsParams.ClientID), ClientID.ToString());
if (ServiceID != -1)
data.Add(EnumUtil.GetString(APIEnums.GetClientsProductsParams.ServiceID), ServiceID.ToString());
if (ProductID != -1)
data.Add(EnumUtil.GetString(APIEnums.GetClientsProductsParams.ProductID), ProductID.ToString());
if (Domain != "")
data.Add(EnumUtil.GetString(APIEnums.GetClientsProductsParams.Domain), Domain);
if (Username != "")
data.Add(EnumUtil.GetString(APIEnums.GetClientsProductsParams.Username), Username);
return JsonConvert.DeserializeObject<GetClientsProducts.GetClientsProducts>(_call.MakeCall(data), settings);
}
public GetInvoices.GetInvoices GetInvoices(int LimitStart = 0, int LimitNumber = 25, int UserID = -1, string Status = "")
{
NameValueCollection data = new NameValueCollection()
{
{ "action", APIEnums.Actions.GetInvoices.ToString() },
{ EnumUtil.GetString(APIEnums.GetInvoicesParams.LimitStart), LimitStart.ToString() },
{ EnumUtil.GetString(APIEnums.GetInvoicesParams.LimitNumber), LimitNumber.ToString() }
};
if (UserID != -1)
data.Add(EnumUtil.GetString(APIEnums.GetInvoicesParams.UserID), UserID.ToString());
if (Status != "")
data.Add(EnumUtil.GetString(APIEnums.GetInvoicesParams.Status), Status);
return JsonConvert.DeserializeObject<GetInvoices.GetInvoices>(_call.MakeCall(data), settings);
}
public GetInvoice.GetInvoice GetInvoice(int InvoiceID)
{
NameValueCollection data = new NameValueCollection()
{
{ "action", EnumUtil.GetString(APIEnums.Actions.GetInvoice) },
{ EnumUtil.GetString(APIEnums.GetInvoiceParams.InvoiceID), InvoiceID.ToString() }
};
string req = _call.MakeCall(data);
JObject result = JObject.Parse(req);
if (result["result"].ToString() == "success")
return JsonConvert.DeserializeObject<GetInvoice.GetInvoice>(req, settings);
else
throw new Exception("An API Error Ocurred", new Exception(result["message"].ToString()));
}
public GetClientsDomains.GetClientsDomains GetClientsDomains(int LimitStart = 0, int LimitNumber = 25, int ClientID = -1, int DomainID = -1, string Domain = "")
{
NameValueCollection data = new NameValueCollection()
{
{ "action", EnumUtil.GetString(APIEnums.Actions.GetClientsDomains) },
{ EnumUtil.GetString(APIEnums.GetClientsDomainsParams.LimitStart), LimitStart.ToString() },
{ EnumUtil.GetString(APIEnums.GetClientsDomainsParams.LimitNumber), LimitNumber.ToString() }
};
if (ClientID != -1)
data.Add(EnumUtil.GetString(APIEnums.GetClientsDomainsParams.ClientID), ClientID.ToString());
if (DomainID != -1)
data.Add(EnumUtil.GetString(APIEnums.GetClientsDomainsParams.DomainID), DomainID.ToString());
if (Domain != "")
data.Add(EnumUtil.GetString(APIEnums.GetClientsDomainsParams.Domain), Domain);
return JsonConvert.DeserializeObject<GetClientsDomains.GetClientsDomains>(_call.MakeCall(data), settings);
}
public bool ModuleChangePassword(int ServiceID, string NewPassword, bool getException = true)
{
NameValueCollection data = new NameValueCollection()
{
{ "action", EnumUtil.GetString(APIEnums.Actions.ModuleChangePassword) },
{ EnumUtil.GetString(APIEnums.ModuleChangePasswordParams.ServiceID), ServiceID.ToString() },
{ EnumUtil.GetString(APIEnums.ModuleChangePasswordParams.NewPassword), NewPassword }
};
JObject result = JObject.Parse(_call.MakeCall(data));
if (result["result"].ToString() == "success")
return true;
else if(result["result"].ToString() != "success" && getException)
throw new Exception("An API Error Ocurred", new Exception(result["message"].ToString()));
return false;
}
public bool ModuleCustomCommand(int ServiceID, string Command, bool getException = true)
{
NameValueCollection data = new NameValueCollection()
{
{ "action", EnumUtil.GetString(APIEnums.Actions.ModuleCustomCommand) },
{ EnumUtil.GetString(APIEnums.ModuleCustomCommandParams.ServiceID), ServiceID.ToString() },
{ EnumUtil.GetString(APIEnums.ModuleCustomCommandParams.Command), Command }
};
JObject result = JObject.Parse(_call.MakeCall(data));
if (result["result"].ToString() == "success")
return true;
else if (result["result"].ToString() != "success" && getException)
throw new Exception("An API Error Ocurred", new Exception(result["message"].ToString()));
return false;
}
public bool UpdateClientProduct(UpdateClientProduct.UpdateClientProduct ClientProductUpdateInfo)
{
JObject result = JObject.Parse(_call.MakeCall(ClientProductUpdateInfo.nvm));
if (result["result"].ToString() == "success")
return true;
else
throw new Exception("An API Error Ocurred", new Exception(result["message"].ToString()));
}
}
}