ActionDto.vala 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Invercargill;
  2. using Invercargill.Mapping;
  3. using InvercargillJson;
  4. namespace Statum.Model {
  5. /**
  6. * A Statum action.
  7. *
  8. * An action describes an HTTP request the frontend can perform. Actions are
  9. * embedded (by path) inside a snapshot's {@link SnapshotDto.public} data and
  10. * invoked via `stm-action`. The response to an action is a JSON array of
  11. * {@link DirectiveDto}s.
  12. */
  13. public class ActionDto : Object {
  14. /** The URI to request. */
  15. public string uri { get; set; }
  16. /** The HTTP verb to use. */
  17. public string method { get; set; }
  18. /**
  19. * Base64-encoded data only readable by the application server (see
  20. * {@link Statum.Cryptography.EncryptionProvider}), carried to the server
  21. * via the `X-Statum-Private` header, or `null`/undefined if the action
  22. * carries no private payload.
  23. */
  24. public string? @private { get; set; }
  25. /**
  26. * Serialises this action to a {@link JsonElement} so it can be embedded
  27. * as a value in a snapshot's public data (e.g.
  28. * `state.public_data["checkout"] = ref.to_element()`), where the client
  29. * resolves it via `stm-action` and echoes `private` back.
  30. */
  31. public JsonElement to_element() throws GLib.Error {
  32. return new JsonElement.from_properties(get_mapper().map_from(this));
  33. }
  34. /**
  35. * The {@link PropertyMapper} used to (de)serialise an {@link ActionDto}.
  36. */
  37. public static PropertyMapper<ActionDto> get_mapper() {
  38. return PropertyMapper.build_for<ActionDto>(cfg => {
  39. cfg.map<string>("uri", o => o.uri, (o, v) => o.uri = v);
  40. cfg.map<string>("method", o => o.method, (o, v) => o.method = v);
  41. cfg.map<string?>("private", o => o.@private, (o, v) => o.@private = v)
  42. .undefined_when(o => o.@private == null)
  43. .when_undefined(o => o.@private = null)
  44. .when_null(o => o.@private = null);
  45. cfg.set_constructor(() => new ActionDto());
  46. });
  47. }
  48. }
  49. }