-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathResourceInterface.cs
executable file
·277 lines (242 loc) · 9.88 KB
/
ResourceInterface.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Plivo.Client;
using Plivo.Exception;
namespace Plivo.Resource {
/// <summary>
/// Resource interface.
/// </summary>
public abstract class ResourceInterface {
/// <summary>
/// The client.
/// </summary>
public HttpClient Client;
/// <summary>
/// The URI.
/// </summary>
protected string Uri;
/// <summary>
/// Initializes a new instance of the <see cref="T:plivo.Resource.ResourceInterface"/> class.
/// </summary>
/// <param name="client">Client.</param>
protected ResourceInterface (HttpClient client) {
Client = client ??
throw new ArgumentNullException (nameof (client));
}
/// <summary>
/// Gets the resource.
/// </summary>
/// <returns>The resource.</returns>
/// <param name="id">Identifier.</param>
/// <param name="data">Data.</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public async Task<T> GetResource<T> (string id, Dictionary<string, object> data = null)
where T : new () {
string to_append = id;
if (id != "") {
to_append = to_append + "/";
}
var result = await Client.Fetch<T> (
Uri + to_append, data);
try {
result.Object.GetType ().GetRuntimeProperty ("StatusCode").SetValue (result.Object, result.StatusCode, null);
} catch (System.NullReferenceException) {
}
return result.Object;
}
public async Task<T> GetSecondaryResource<T> (string id, Dictionary<string, object> data = null,
string secondaryName = null, string secondaryId = null)
where T : new () {
string to_append = id;
if (id != "") {
to_append = to_append + "/";
}
if (secondaryName != null)
{
to_append = to_append + secondaryName;
}
if (secondaryId != null)
{
to_append = to_append + "/" + secondaryId + "/";
}
var result = await Client.Fetch<T> (
Uri + to_append, data);
try {
result.Object.GetType ().GetRuntimeProperty ("StatusCode").SetValue (result.Object, result.StatusCode, null);
} catch (System.NullReferenceException) {
}
return result.Object;
}
public async Task<T> UpdateSecondaryResource<T> (string id, Dictionary<string, object> data = null,
string secondaryName = null, string secondaryId = null)
where T : new () {
string to_append = id;
if (id != "") {
to_append = to_append + "/";
}
if (secondaryName != null)
{
to_append = to_append + secondaryName;
}
if (secondaryId != null)
{
to_append = to_append + "/" + secondaryId + "/";
}
var result = await Client.Update<T> (
Uri + to_append, data);
try {
result.Object.GetType ().GetRuntimeProperty ("StatusCode").SetValue (result.Object, result.StatusCode, null);
} catch (System.NullReferenceException) {
}
return result.Object;
}
public async Task<T> DeleteSecondaryResource<T> (string id, Dictionary<string, object> data = null,
string secondaryName = null, string secondaryId = null)
where T : new () {
string to_append = id;
if (id != "") {
to_append = to_append + "/";
}
if (secondaryName != null)
{
to_append = to_append + secondaryName;
}
if (secondaryId != null)
{
to_append = to_append + "/" + secondaryId + "/";
}
var result = await Client.Delete<T> (
Uri + to_append, data);
try {
result.Object.GetType ().GetRuntimeProperty ("StatusCode").SetValue (result.Object, result.StatusCode, null);
} catch (System.NullReferenceException) {
}
return result.Object;
}
/// <summary>
/// Lists the resources.
/// </summary>
/// <returns>The resources.</returns>
/// <param name="data">Data.</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public async Task<T> ListResources<T> (Dictionary<string, object> data = null)
where T : new () {
var result = await Client.Fetch<T> (Uri, data);
try {
result.Object.GetType ().GetRuntimeProperty ("StatusCode").SetValue (result.Object, result.StatusCode, null);
} catch (System.NullReferenceException) {
}
return result.Object;
}
/// <summary>
/// Lists the resources.
/// </summary>
/// <returns>The resources.</returns>
/// <param name="data">Data.</param>
/// <param name="url"> URL </param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public async Task<T> ListResources<T>(string url, Dictionary<string, object> data = null)
where T : new()
{
string to_append = url;
if (url != "")
{
to_append = to_append + "/";
}
var result = await Client.Fetch<T>(Uri + to_append, data);
try
{
result.Object.GetType().GetRuntimeProperty("StatusCode").SetValue(result.Object, result.StatusCode, null);
}
catch (System.NullReferenceException)
{
}
return result.Object;
}
/// <summary>
/// Deletes the resource.
/// </summary>
/// <returns>The resource.</returns>
/// <param name="id">Identifier.</param>
/// <param name="data">Data.</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public async Task<T> DeleteResource<T> (string id, Dictionary<string, object> data = null)
where T : new () {
var result = await Client.Delete<T> (Uri + id + "/", data);
try {
result.Object.GetType ().GetRuntimeProperty ("StatusCode").SetValue (result.Object, result.StatusCode, null);
} catch (System.NullReferenceException) {
}
return result.Object;
}
/// <summary>
/// Creates the data.
/// </summary>
/// <returns>The data.</returns>
/// <param name="propertyInfos"></param>
/// <param name="data">Data.</param>
public static Dictionary<string, object> CreateData (List<string> propertyInfos, dynamic data) {
var dict = new Dictionary<string, object> ();
foreach (PropertyInfo pi in data.GetType ().GetProperties ()) {
if (propertyInfos.Contains (pi.Name)) {
if (string.IsNullOrEmpty (pi.GetValue (data)))
throw new PlivoValidationException (pi.Name + " is mandatory, can not be null or empty");
}
if (pi.Name.Equals ("limit")) {
if (pi.GetValue (data) > 20) {
throw new PlivoValidationException ("limit:" + pi.GetValue (data) + " is out of range [0,20]");
}
}
if (pi.GetValue (data) == null) continue;
var name_char_array = pi.Name.ToCharArray ();
if (name_char_array.ElementAt (0) == '_') {
name_char_array = string.Concat (name_char_array).Substring (1).ToCharArray ();
}
var value = pi.GetValue (data);
if (name_char_array.All (char.IsUpper)) {
dict.Add (string.Concat (name_char_array), value);
} else {
dict.Add (
string.Concat (
name_char_array.Select (
(x, i) => i > 0 && char.IsUpper (x) ? "_" + x.ToString ().ToLower () : x.ToString ())),
value);
}
}
return dict;
}
/// <summary>
/// Returns the list of names of parameters which are mandatory.
/// </summary>
/// <param name="parameterInfos"></param>
/// <returns>List of names of mandatory parameters.</returns>
public static List<string> GetMethodParameterProperties (ParameterInfo[] parameterInfos) {
return (from pi in parameterInfos where!pi.IsOptional select pi.Name).ToList ();
}
public static T ExecuteWithExceptionUnwrap<T> (Func<T> func) where T : class {
try {
return func ();
} catch (AggregateException ex) {
ex.Flatten ();
if (ex.InnerExceptions[0] is Newtonsoft.Json.JsonReaderException){
throw new PlivoValidationException ("Unexpected error occured. Please contact plivo support. Exception is"+ ex.ToString());
}
throw ex.InnerExceptions[0];
}
}
public static void ExecuteWithExceptionUnwrap (Action func) {
try {
func ();
} catch (AggregateException ex) {
ex.Flatten ();
throw ex.InnerExceptions[0];
} catch(System.Exception ex){
throw new PlivoServerException(ex.ToString());
}
}
}
}