Client.cs 19 KB

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