StatumRequest.vala 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using Invercargill;
  2. using Invercargill.DataStructures;
  3. using Astralis;
  4. namespace Statum {
  5. /**
  6. * The per-request Statum context, shared by page, entrypoint and action
  7. * handlers.
  8. */
  9. public class StatumRequest : Object {
  10. /** The request URI (e.g. the page's `<pstm-uri>` or an action URI). */
  11. public string uri { get; set; }
  12. /** Route parameters captured from the matched {@link EndpointRoute}. */
  13. public ReadOnlyAssociative<string, string> route_params { get; set; default = new Dictionary<string, string>(); }
  14. /** Held slots, indexed by type name. */
  15. public ReadOnlyAssociative<string, HeldSlot> held { get; set; default = new Dictionary<string, HeldSlot>(); }
  16. /** The request's query parameters. */
  17. public Catalogue<string, string> query { get; set; }
  18. /**
  19. * Action-private data decrypted from the `X-Statum-Private` header, or
  20. * `null` for non-action requests.
  21. */
  22. public Properties? action_private { get; set; }
  23. /** Parsed form body for POST/PUT/PATCH actions, or `null`. */
  24. public FormData? form { get; set; }
  25. /**
  26. * Reads a held slot's public data as a typed model, or `null` if the slot
  27. * is not held or cannot be mapped.
  28. */
  29. public TPublic? held_as<TPublic>(string type_name) {
  30. HeldSlot slot;
  31. if (!held.try_get(type_name, out slot) || slot == null) {
  32. return null;
  33. }
  34. try {
  35. return GObjectMapping.from_properties_typed<TPublic>(typeof(TPublic), ((!)slot).@public);
  36. } catch (GLib.Error e) {
  37. return null;
  38. }
  39. }
  40. /** Raw route parameter (from a `<pstm-uri>` template like `/cats/{species}`), or `null`. */
  41. public string? route(string name) {
  42. return route_params.get_or_default(name);
  43. }
  44. }
  45. }