Request.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.InteropServices.ComTypes;
  5. using InABox.Core;
  6. namespace InABox.Clients
  7. {
  8. public class Credentials : ISerializeBinary
  9. {
  10. [Obsolete]
  11. public string? UserID { get; set; }
  12. [Obsolete]
  13. public string? Password { get; set; }
  14. public string Platform { get; set; }
  15. public string Version { get; set; }
  16. public Guid Session { get; set; }
  17. public void SerializeBinary(CoreBinaryWriter writer)
  18. {
  19. writer.Write(UserID ?? "");
  20. writer.Write(Password ?? "");
  21. writer.Write(Platform);
  22. writer.Write(Version);
  23. writer.Write(Session);
  24. }
  25. public void DeserializeBinary(CoreBinaryReader reader)
  26. {
  27. UserID = reader.ReadString();
  28. Password = reader.ReadString();
  29. Platform = reader.ReadString();
  30. Version = reader.ReadString();
  31. Session = reader.ReadGuid();
  32. }
  33. }
  34. public abstract class Request
  35. {
  36. public Request()
  37. {
  38. Credentials = new Credentials();
  39. }
  40. public static Action<Request>? BeforeRequest { get; set; }
  41. public Credentials Credentials { get; set; }
  42. public abstract RequestMethod GetMethod();
  43. public virtual void SerializeBinary(CoreBinaryWriter writer)
  44. {
  45. Credentials.SerializeBinary(writer);
  46. }
  47. public virtual void DeserializeBinary(CoreBinaryReader reader)
  48. {
  49. Credentials.DeserializeBinary(reader);
  50. }
  51. }
  52. public enum StatusCode
  53. {
  54. OK,
  55. Incomplete,
  56. Error,
  57. Unauthenticated,
  58. BadServer
  59. }
  60. public enum RequestMethod
  61. {
  62. Info,
  63. Ping,
  64. Check2FA,
  65. Validate,
  66. MultiQuery,
  67. MultiDelete,
  68. Delete,
  69. Save,
  70. MultiSave,
  71. Query,
  72. Notify
  73. }
  74. public abstract class Response
  75. {
  76. public Response()
  77. {
  78. Status = StatusCode.Incomplete;
  79. Messages = new List<string>();
  80. }
  81. public StatusCode Status { get; set; }
  82. public List<string> Messages { get; }
  83. public virtual void DeserializeBinary(CoreBinaryReader reader)
  84. {
  85. Status = (StatusCode)Enum.ToObject(typeof(StatusCode), reader.ReadInt32());
  86. Messages.Clear();
  87. var nMessages = reader.ReadInt32();
  88. for (int i = 0; i < nMessages; ++i)
  89. {
  90. Messages.Add(reader.ReadString());
  91. }
  92. }
  93. public virtual void SerializeBinary(CoreBinaryWriter writer)
  94. {
  95. writer.Write((int)Status);
  96. writer.Write(Messages.Count);
  97. foreach(var message in Messages)
  98. {
  99. writer.Write(message);
  100. }
  101. }
  102. }
  103. public abstract class BaseRequest<TEntity> : Request where TEntity : Entity, new()
  104. {
  105. }
  106. public abstract class BaseResponse<TEntity> : Response where TEntity : Entity, new()
  107. {
  108. }
  109. /*public class ListRequest<TEntity> : BaseRequest<TEntity> where TEntity : Entity, new()
  110. {
  111. public Filter<TEntity>? Filter { get; set; }
  112. public Columns<TEntity>? Columns { get; set; }
  113. public SortOrder<TEntity>? Sort { get; set; }
  114. public override RequestMethod GetMethod() => RequestMethod.Query;
  115. }
  116. public class ListResponse<TEntity> : BaseResponse<TEntity> where TEntity : Entity, new()
  117. {
  118. public IEnumerable<object[]> Data { get; set; }
  119. }*/
  120. public class QueryRequest<TEntity> : BaseRequest<TEntity> where TEntity : Entity, new()
  121. {
  122. public Filter<TEntity>? Filter { get; set; }
  123. public Columns<TEntity>? Columns { get; set; }
  124. public SortOrder<TEntity>? Sort { get; set; }
  125. public override RequestMethod GetMethod() => RequestMethod.Query;
  126. }
  127. public class QueryResponse<TEntity> : BaseResponse<TEntity>, ISerializeBinary where TEntity : Entity, new()
  128. {
  129. public CoreTable? Items { get; set; }
  130. public override void DeserializeBinary(CoreBinaryReader reader)
  131. {
  132. base.DeserializeBinary(reader);
  133. if (reader.ReadBoolean())
  134. {
  135. Items = new CoreTable();
  136. Items.DeserializeBinary(reader);
  137. }
  138. else
  139. {
  140. Items = null;
  141. }
  142. }
  143. public override void SerializeBinary(CoreBinaryWriter writer)
  144. {
  145. base.SerializeBinary(writer);
  146. if(Items is null)
  147. {
  148. writer.Write(false);
  149. }
  150. else
  151. {
  152. writer.Write(true);
  153. Items.SerializeBinary(writer);
  154. }
  155. }
  156. }
  157. /*public class LoadRequest<TEntity> : BaseRequest<TEntity> where TEntity : Entity, new()
  158. {
  159. public Filter<TEntity>? Filter { get; set; }
  160. public SortOrder<TEntity>? Sort { get; set; }
  161. }
  162. public class LoadResponse<TEntity> : BaseResponse<TEntity> where TEntity : Entity, new()
  163. {
  164. public TEntity[] Items { get; set; }
  165. }*/
  166. public class MultiSaveRequest<TEntity> : BaseRequest<TEntity>, ISerializeBinary where TEntity : Entity, new()
  167. {
  168. public TEntity[] Items { get; set; }
  169. public string AuditNote { get; set; }
  170. [Obsolete("We don't like this; it should always be true.")]
  171. // Added 11/04/23 to address incompatibility, remove as soon as possible; the relevant code to update is in RestService; update assuming that ReturnOnlyChanged is always true.
  172. public bool ReturnOnlyChanged { get; set; } = false;
  173. public override RequestMethod GetMethod() => RequestMethod.MultiSave;
  174. public override void SerializeBinary(CoreBinaryWriter writer)
  175. {
  176. base.SerializeBinary(writer);
  177. writer.WriteObjects(Items);
  178. writer.Write(AuditNote);
  179. writer.Write(ReturnOnlyChanged);
  180. }
  181. public override void DeserializeBinary(CoreBinaryReader reader)
  182. {
  183. base.DeserializeBinary(reader);
  184. Items = reader.ReadObjects<TEntity>().ToArray();
  185. AuditNote = reader.ReadString();
  186. ReturnOnlyChanged = reader.ReadBoolean();
  187. }
  188. }
  189. public class MultiSaveResponse<TEntity> : BaseResponse<TEntity> where TEntity : Entity, new()
  190. {
  191. //public Guid[] IDs { get; set; }
  192. public TEntity[] Items { get; set; }
  193. public List<Dictionary<string, object?>> ChangedValues { get; set; } = new List<Dictionary<string, object?>>();
  194. }
  195. public class SaveRequest<TEntity> : BaseRequest<TEntity>, ISerializeBinary where TEntity : Entity, new()
  196. {
  197. public TEntity Item { get; set; }
  198. public string AuditNote { get; set; }
  199. public bool ReturnOnlyChanged { get; set; } = false;
  200. public override RequestMethod GetMethod() => RequestMethod.Save;
  201. public override void SerializeBinary(CoreBinaryWriter writer)
  202. {
  203. base.SerializeBinary(writer);
  204. writer.WriteObject(Item);
  205. writer.Write(AuditNote);
  206. writer.Write(ReturnOnlyChanged);
  207. }
  208. public override void DeserializeBinary(CoreBinaryReader reader)
  209. {
  210. base.DeserializeBinary(reader);
  211. Item = reader.ReadObject<TEntity>();
  212. AuditNote = reader.ReadString();
  213. ReturnOnlyChanged = reader.ReadBoolean();
  214. }
  215. }
  216. public class SaveResponse<TEntity> : BaseResponse<TEntity> where TEntity : Entity, new()
  217. {
  218. //public Guid ID { get; set; }
  219. public TEntity Item { get; set; }
  220. public Dictionary<string, object?> ChangedValues { get; set; } = new Dictionary<string, object?>();
  221. }
  222. public class DeleteRequest<TEntity> : BaseRequest<TEntity> where TEntity : Entity, new()
  223. {
  224. public TEntity Item { get; set; }
  225. public string AuditNote { get; set; }
  226. public override RequestMethod GetMethod() => RequestMethod.Delete;
  227. }
  228. public class DeleteResponse<TEntity> : BaseResponse<TEntity> where TEntity : Entity, new()
  229. {
  230. }
  231. public class MultiDeleteRequest<TEntity> : BaseRequest<TEntity> where TEntity : Entity, new()
  232. {
  233. public TEntity[] Items { get; set; }
  234. public string AuditNote { get; set; }
  235. public override RequestMethod GetMethod() => RequestMethod.MultiDelete;
  236. }
  237. public class MultiDeleteResponse<TEntity> : BaseResponse<TEntity> where TEntity : Entity, new()
  238. {
  239. }
  240. public class MultiQueryRequest : Request
  241. {
  242. public Dictionary<string, string> TableTypes { get; set; }
  243. public Dictionary<string, string> Filters { get; set; }
  244. public Dictionary<string, string> Columns { get; set; }
  245. public Dictionary<string, string> Sorts { get; set; }
  246. public override RequestMethod GetMethod() => RequestMethod.MultiQuery;
  247. }
  248. public class MultiQueryResponse : Response, ISerializeBinary
  249. {
  250. public MultiQueryResponse()
  251. {
  252. Tables = new Dictionary<string, CoreTable>();
  253. }
  254. public Dictionary<string, CoreTable> Tables { get; set; }
  255. public override void DeserializeBinary(CoreBinaryReader reader)
  256. {
  257. base.DeserializeBinary(reader);
  258. Tables.Clear();
  259. var nTables = reader.ReadInt32();
  260. for(int i = 0; i < nTables; ++i)
  261. {
  262. var name = reader.ReadString();
  263. var table = new CoreTable();
  264. table.DeserializeBinary(reader);
  265. Tables[name] = table;
  266. }
  267. }
  268. public override void SerializeBinary(CoreBinaryWriter writer)
  269. {
  270. base.SerializeBinary(writer);
  271. writer.Write(Tables.Count);
  272. foreach(var (name, table) in Tables)
  273. {
  274. writer.Write(name);
  275. table.SerializeBinary(writer);
  276. }
  277. }
  278. }
  279. public enum ValidationResult
  280. {
  281. VALID,
  282. INVALID,
  283. REQUIRE_2FA,
  284. PASSWORD_EXPIRED
  285. }
  286. public class ValidateRequest : Request
  287. {
  288. public string? UserID { get; set; }
  289. public string? Password { get; set; }
  290. public string? PIN { get; set; }
  291. public bool UsePIN { get; set; }
  292. public override RequestMethod GetMethod() => RequestMethod.Validate;
  293. }
  294. public class ValidateResponse : Response
  295. {
  296. public ValidationResult ValidationResult { get; set; }
  297. public Guid UserGuid { get; set; }
  298. public string UserID { get; set; }
  299. public Guid SecurityID { get; set; }
  300. public Guid Session { get; set; }
  301. public string? Recipient2FA { get; set; }
  302. public DateTime PasswordExpiration { get; set; }
  303. }
  304. public class Check2FARequest : Request
  305. {
  306. public string Code { get; set; }
  307. public override RequestMethod GetMethod() => RequestMethod.Check2FA;
  308. }
  309. public class Check2FAResponse : Response
  310. {
  311. public bool Valid { get; set; }
  312. }
  313. public class PingRequest : Request
  314. {
  315. public override RequestMethod GetMethod() => RequestMethod.Ping;
  316. }
  317. public class PingResponse : Response { }
  318. public class InfoRequest : Request
  319. {
  320. public override RequestMethod GetMethod() => RequestMethod.Info;
  321. }
  322. public class DatabaseInfo
  323. {
  324. public string? ColorScheme { get; set; }
  325. public byte[]? Logo { get; set; }
  326. public string Version { get; set; }
  327. public bool IsHTTPS { get; set; }
  328. }
  329. public class InfoResponse : Response
  330. {
  331. public DatabaseInfo Info { get; set; }
  332. public InfoResponse() : base()
  333. {
  334. Info = new DatabaseInfo();
  335. }
  336. }
  337. public static class Extensions
  338. {
  339. public static T Status<T>(this T response, StatusCode status) where T : Response
  340. {
  341. response.Status = status;
  342. return response;
  343. }
  344. }
  345. public class RequestException : Exception
  346. {
  347. public StatusCode Status { get; set; }
  348. public RequestMethod Method { get; set; }
  349. public RequestException(string message, StatusCode status, Request request): base(message)
  350. {
  351. Status = status;
  352. Method = request.GetMethod();
  353. }
  354. }
  355. }