Client.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Timers;
  6. using InABox.Core;
  7. namespace InABox.Clients
  8. {
  9. public enum SerializerProtocol
  10. {
  11. Rest,
  12. RPC
  13. }
  14. public class QueryMultipleResults
  15. {
  16. private readonly Dictionary<string, CoreTable> Results;
  17. internal QueryMultipleResults(Dictionary<string, CoreTable> results)
  18. {
  19. Results = results;
  20. }
  21. public CoreTable this[string name] => Results[name];
  22. public CoreTable Get<T>() => Results[typeof(T).Name];
  23. /// <summary>
  24. /// Like <see cref="Get{T}"/>, but calls <see cref="CoreTable.ToObjects{T}"/> on the table.
  25. /// </summary>
  26. /// <typeparam name="T"></typeparam>
  27. /// <returns></returns>
  28. public IEnumerable<T> GetObjects<T>()
  29. where T: BaseObject, new()
  30. => Results[typeof(T).Name].ToObjects<T>();
  31. public CoreTable Get(string name) => Results[name];
  32. }
  33. public abstract class Client
  34. {
  35. public abstract CoreTable Query(IFilter? filter = null, IColumns? columns = null, ISortOrder? sortOrder = null);
  36. public abstract void Save(Entity entity, string auditNote);
  37. public abstract void Save(IEnumerable<Entity> entity, string auditNote);
  38. private static IClient CheckClient()
  39. {
  40. using (new Profiler(true))
  41. return ClientFactory.CreateClient<User>();
  42. }
  43. public static Dictionary<string, CoreTable> QueryMultiple(Dictionary<string, IQueryDef> queries)
  44. {
  45. try
  46. {
  47. using var timer = new Profiler(false);
  48. var result = CheckClient().QueryMultiple(queries);
  49. timer.Log(result.Sum(x => x.Value.Rows.Count));
  50. return result;
  51. }
  52. catch (RequestException e)
  53. {
  54. ClientFactory.RaiseRequestError(e);
  55. throw;
  56. }
  57. }
  58. public static void QueryMultiple(
  59. Action<Dictionary<string, CoreTable>?, Exception?> callback,
  60. Dictionary<string, IQueryDef> queries)
  61. {
  62. try
  63. {
  64. using var timer = new Profiler(false);
  65. CheckClient().QueryMultiple((result, e) =>
  66. {
  67. timer.Dispose(result != null ? result.Sum(x => x.Value.Rows.Count) : -1);
  68. callback?.Invoke(result, e);
  69. }, queries);
  70. }
  71. catch (RequestException e)
  72. {
  73. ClientFactory.RaiseRequestError(e);
  74. throw;
  75. }
  76. }
  77. public static QueryMultipleResults QueryMultiple(params IKeyedQueryDef[] queries) =>
  78. new QueryMultipleResults(QueryMultiple(queries.ToDictionary(x => x.Key, x => x as IQueryDef)));
  79. public static void QueryMultiple(Action<QueryMultipleResults?, Exception?> callback, params IKeyedQueryDef[] queries) =>
  80. QueryMultiple((results, e) =>
  81. {
  82. if (results != null)
  83. {
  84. callback?.Invoke(new QueryMultipleResults(results), e);
  85. }
  86. else
  87. {
  88. callback?.Invoke(null, e);
  89. }
  90. }, queries.ToDictionary(x => x.Key, x => x as IQueryDef));
  91. public static QueryMultipleResults QueryMultiple(IEnumerable<IKeyedQueryDef> queries) =>
  92. new QueryMultipleResults(QueryMultiple(queries.ToDictionary(x => x.Key, x => x as IQueryDef)));
  93. public static void QueryMultiple(Action<QueryMultipleResults?, Exception?> callback, IEnumerable<IKeyedQueryDef> queries) =>
  94. QueryMultiple((results, e) =>
  95. {
  96. if(results != null)
  97. {
  98. callback?.Invoke(new QueryMultipleResults(results), e);
  99. }
  100. else
  101. {
  102. callback?.Invoke(null, e);
  103. }
  104. }, queries.ToDictionary(x => x.Key, x => x as IQueryDef));
  105. public static IValidationData Validate(Guid session)
  106. {
  107. try
  108. {
  109. using (new Profiler(true))
  110. return CheckClient().Validate(session);
  111. }
  112. catch (RequestException e)
  113. {
  114. ClientFactory.RaiseRequestError(e);
  115. throw;
  116. }
  117. }
  118. public static IValidationData Validate(string pin, Guid session = default)
  119. {
  120. try
  121. {
  122. using (new Profiler(true))
  123. return CheckClient().Validate(pin, session);
  124. }
  125. catch (RequestException e)
  126. {
  127. ClientFactory.RaiseRequestError(e);
  128. throw;
  129. }
  130. }
  131. public static IValidationData Validate(string userid, string password, Guid session = default)
  132. {
  133. try
  134. {
  135. using (new Profiler(true))
  136. return CheckClient().Validate(userid, password, session);
  137. }
  138. catch (RequestException e)
  139. {
  140. ClientFactory.RaiseRequestError(e);
  141. throw;
  142. }
  143. }
  144. public static bool Check2FA(string code, Guid? session = null)
  145. {
  146. try
  147. {
  148. using (new Profiler(true))
  149. return CheckClient().Check2FA(code, session);
  150. }
  151. catch (RequestException e)
  152. {
  153. ClientFactory.RaiseRequestError(e);
  154. throw;
  155. }
  156. }
  157. public static bool Ping()
  158. {
  159. try
  160. {
  161. using (new Profiler(true))
  162. return CheckClient().Ping();
  163. }
  164. catch (RequestException e)
  165. {
  166. ClientFactory.RaiseRequestError(e);
  167. throw;
  168. }
  169. }
  170. public static DatabaseInfo Info()
  171. {
  172. try
  173. {
  174. using (new Profiler(true))
  175. return CheckClient().Info();
  176. }
  177. catch (RequestException e)
  178. {
  179. ClientFactory.RaiseRequestError(e);
  180. throw;
  181. }
  182. }
  183. public static string Version()
  184. {
  185. try
  186. {
  187. using (new Profiler(true))
  188. return CheckClient().Version();
  189. }
  190. catch (RequestException e)
  191. {
  192. ClientFactory.RaiseRequestError(e);
  193. throw;
  194. }
  195. }
  196. public static string ReleaseNotes()
  197. {
  198. try
  199. {
  200. using (new Profiler(true))
  201. return CheckClient().ReleaseNotes();
  202. }
  203. catch (RequestException e)
  204. {
  205. ClientFactory.RaiseRequestError(e);
  206. throw;
  207. }
  208. }
  209. public static byte[]? Installer()
  210. {
  211. try
  212. {
  213. using (new Profiler(true))
  214. return CheckClient().Installer();
  215. }
  216. catch (RequestException e)
  217. {
  218. ClientFactory.RaiseRequestError(e);
  219. throw;
  220. }
  221. }
  222. public static Client Create(Type TEntity) =>
  223. (Activator.CreateInstance(typeof(Client<>).MakeGenericType(TEntity)) as Client)!;
  224. }
  225. public class Client<TEntity> : Client, IDisposable where TEntity : Entity, IPersistent, IRemotable, new()
  226. {
  227. private IClient<TEntity> _client;
  228. public Client()
  229. {
  230. _client = ClientFactory.CreateClient<TEntity>();
  231. }
  232. public void Dispose()
  233. {
  234. }
  235. private void CheckSupported()
  236. {
  237. if (!ClientFactory.IsSupported<TEntity>())
  238. throw new NotSupportedException(string.Format("{0} is not supported in this context", typeof(TEntity).EntityName()));
  239. }
  240. private string FilterToString(Filter<TEntity> filter)
  241. {
  242. return filter != null ? filter.AsOData() : "";
  243. }
  244. private string OrderToString(SortOrder<TEntity> order)
  245. {
  246. return order != null ? order.AsOData() : "";
  247. }
  248. public CoreTable Query(Filter<TEntity>? filter = null, Columns<TEntity>? columns = null, SortOrder<TEntity>? orderby = null)
  249. {
  250. try
  251. {
  252. using (var timer = new Profiler<TEntity>(false))
  253. {
  254. CheckSupported();
  255. var result = _client.Query(filter, columns, orderby);
  256. timer.Log(result.Rows.Count);
  257. return result;
  258. }
  259. }
  260. catch(RequestException e)
  261. {
  262. ClientFactory.RaiseRequestError(e);
  263. throw;
  264. }
  265. }
  266. public override CoreTable Query(IFilter? filter, IColumns? columns, ISortOrder? sortOrder)
  267. {
  268. return Query(filter as Filter<TEntity>, columns as Columns<TEntity>, sortOrder as SortOrder<TEntity>);
  269. }
  270. public void Query(Filter<TEntity>? filter, Columns<TEntity>? columns, SortOrder<TEntity>? sort, Action<CoreTable?, Exception?> callback)
  271. {
  272. try
  273. {
  274. var timer = new Profiler<TEntity>(false);
  275. CheckSupported();
  276. _client.Query(filter, columns, sort, (c, e) =>
  277. {
  278. timer.Dispose(c != null ? c.Rows.Count : -1);
  279. callback?.Invoke(c, e);
  280. });
  281. }
  282. catch (RequestException e)
  283. {
  284. ClientFactory.RaiseRequestError(e);
  285. throw;
  286. }
  287. }
  288. public TEntity[] Load(Filter<TEntity>? filter = null, SortOrder<TEntity>? sort = null)
  289. {
  290. try
  291. {
  292. using (var timer = new Profiler<TEntity>(false))
  293. {
  294. CheckSupported();
  295. var result = _client.Load(filter, sort);
  296. foreach (var entity in result)
  297. entity.CommitChanges();
  298. timer.Log(result.Length);
  299. return result;
  300. }
  301. }
  302. catch (RequestException e)
  303. {
  304. ClientFactory.RaiseRequestError(e);
  305. throw;
  306. }
  307. }
  308. public void Load(Filter<TEntity> filter, SortOrder<TEntity> sort, Action<TEntity[]?, Exception?> callback)
  309. {
  310. try
  311. {
  312. var timer = new Profiler<TEntity>(false);
  313. CheckSupported();
  314. _client.Load(filter, sort, (i, e) =>
  315. {
  316. timer.Dispose(i != null ? i.Length : -1);
  317. callback?.Invoke(i, e);
  318. });
  319. }
  320. catch (RequestException e)
  321. {
  322. ClientFactory.RaiseRequestError(e);
  323. throw;
  324. }
  325. }
  326. public override void Save(Entity entity, string auditNote)
  327. {
  328. try
  329. {
  330. Save((entity as TEntity)!, auditNote);
  331. }
  332. catch (RequestException e)
  333. {
  334. ClientFactory.RaiseRequestError(e);
  335. throw;
  336. }
  337. }
  338. public override void Save(IEnumerable<Entity> entities, string auditNote)
  339. {
  340. try
  341. {
  342. Save(entities.Cast<TEntity>(), auditNote);
  343. }
  344. catch (RequestException e)
  345. {
  346. ClientFactory.RaiseRequestError(e);
  347. throw;
  348. }
  349. }
  350. public void Save(TEntity entity, string auditnote)
  351. {
  352. try
  353. {
  354. using (new Profiler<TEntity>(true))
  355. {
  356. CheckSupported();
  357. entity.LastUpdate = DateTime.Now;
  358. entity.LastUpdateBy = ClientFactory.UserID;
  359. _client.Save(entity, auditnote);
  360. entity.CommitChanges();
  361. }
  362. }
  363. catch (RequestException e)
  364. {
  365. ClientFactory.RaiseRequestError(e);
  366. throw;
  367. }
  368. }
  369. public void Save(TEntity entity, string auditnote, Action<TEntity, Exception?> callback)
  370. {
  371. try
  372. {
  373. var timer = new Profiler<TEntity>(false);
  374. CheckSupported();
  375. _client.Save(entity, auditnote, (i, c) =>
  376. {
  377. timer.Dispose();
  378. callback?.Invoke(i, c);
  379. });
  380. }
  381. catch (RequestException e)
  382. {
  383. ClientFactory.RaiseRequestError(e);
  384. throw;
  385. }
  386. }
  387. public void Save(IEnumerable<TEntity> entities, string auditnote)
  388. {
  389. try
  390. {
  391. using var timer = new Profiler<TEntity>(false);
  392. CheckSupported();
  393. if (entities.Any())
  394. _client.Save(entities, auditnote);
  395. timer.Log(entities.Count());
  396. }
  397. catch (RequestException e)
  398. {
  399. ClientFactory.RaiseRequestError(e);
  400. throw;
  401. }
  402. }
  403. public void Save(IEnumerable<TEntity> entities, string auditnote, Action<IEnumerable<TEntity>, Exception?> callback)
  404. {
  405. try
  406. {
  407. var timer = new Profiler<TEntity>(false);
  408. CheckSupported();
  409. if (entities.Any())
  410. _client.Save(entities, auditnote, (i, e) =>
  411. {
  412. timer.Dispose(i.Count());
  413. callback?.Invoke(i, e);
  414. });
  415. }
  416. catch (RequestException e)
  417. {
  418. ClientFactory.RaiseRequestError(e);
  419. throw;
  420. }
  421. }
  422. public void Delete(TEntity entity, string auditnote)
  423. {
  424. try
  425. {
  426. using (new Profiler<TEntity>(true))
  427. {
  428. CheckSupported();
  429. _client.Delete(entity, auditnote);
  430. }
  431. }
  432. catch (RequestException e)
  433. {
  434. ClientFactory.RaiseRequestError(e);
  435. throw;
  436. }
  437. }
  438. public void Delete(TEntity entity, string auditnote, Action<TEntity, Exception?> callback)
  439. {
  440. try
  441. {
  442. var timer = new Profiler<TEntity>(true);
  443. CheckSupported();
  444. _client.Delete(entity, auditnote, (i, e) =>
  445. {
  446. timer.Dispose();
  447. callback?.Invoke(i, e);
  448. });
  449. }
  450. catch (RequestException e)
  451. {
  452. ClientFactory.RaiseRequestError(e);
  453. throw;
  454. }
  455. }
  456. public void Delete(IList<TEntity> entities, string auditnote)
  457. {
  458. try
  459. {
  460. using var timer = new Profiler<TEntity>(false);
  461. CheckSupported();
  462. _client.Delete(entities, auditnote);
  463. timer.Log(entities.Count());
  464. }
  465. catch (RequestException e)
  466. {
  467. ClientFactory.RaiseRequestError(e);
  468. throw;
  469. }
  470. }
  471. public void Delete(IList<TEntity> entities, string auditnote, Action<IList<TEntity>, Exception?> callback)
  472. {
  473. try
  474. {
  475. var timer = new Profiler<TEntity>(false);
  476. CheckSupported();
  477. _client.Delete(entities, auditnote, (i, e) =>
  478. {
  479. timer.Dispose(entities.Count());
  480. callback?.Invoke(i, e);
  481. });
  482. }
  483. catch (RequestException e)
  484. {
  485. ClientFactory.RaiseRequestError(e);
  486. throw;
  487. }
  488. }
  489. public IEnumerable<string> SupportedTypes()
  490. {
  491. try
  492. {
  493. using (new Profiler(true))
  494. return _client.SupportedTypes();
  495. }
  496. catch (RequestException e)
  497. {
  498. ClientFactory.RaiseRequestError(e);
  499. throw;
  500. }
  501. }
  502. public new DatabaseInfo Info()
  503. {
  504. try
  505. {
  506. using (new Profiler(true))
  507. return _client.Info();
  508. }
  509. catch (RequestException e)
  510. {
  511. ClientFactory.RaiseRequestError(e);
  512. throw;
  513. }
  514. }
  515. }
  516. }