Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GetTokenEvents.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
8using Waher.Script;
13using System.Text;
15
17{
22 {
27 : base("Tokens/GetTokenEvents",
28 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
29 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
30 {
31 }
32
33 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(GetTokenEvents).Namespace + ".JSON.GetTokenEvents.req");
34 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetTokenEvents).Namespace + ".XML.GetTokenEvents.req");
35
44 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
45 {
47 string TokenId = (string)Parameters["PTokenId"].AssociatedObjectValue;
48
49 int Offset = 0;
50 int MaxCount = int.MaxValue;
51
52 if (Parameters.TryGetValue("POffset", out IElement E) && !(E is null) &&
53 E.AssociatedObjectValue is double d)
54 {
55 Offset = (int)d;
56 }
57
58 if (Parameters.TryGetValue("PMaxCount", out E) && !(E is null) &&
59 E.AssociatedObjectValue is double d2)
60 {
61 MaxCount = (int)d2;
62 }
63
64 string BareJid = User.UserName + "@" + Gateway.Domain;
65
66 Token Token = await NeuroFeaturesProcessor.GetToken(TokenId, true)
67 ?? throw new NotFoundException("Token not found.");
68
69 await GetToken.AssertAuthorization(Token, new XmppAddress(BareJid));
70
71 IEnumerable<TokenEvent> Events = await NeuroFeaturesProcessor.GetTokenEvents(Token, Offset, MaxCount);
72
73 StringBuilder Xml = new StringBuilder();
74
75 Xml.Append("<events xmlns='");
77 Xml.Append("'>");
78
79 foreach (TokenEvent Event in Events)
80 Event.Serialize(Xml);
81
82 Xml.Append("</events>");
83
84 XmlDocument Doc = new XmlDocument()
85 {
86 PreserveWhitespace = true
87 };
88 Doc.LoadXml(Xml.ToString());
89
90 await Response.Return(new NamedDictionary<string, object>("EventsResult", AgentNamespace)
91 {
92 { "Events", Doc }
93 });
94 }
95
96 }
97}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:96
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:2354
Represents an HTTP request.
Definition: HttpRequest.cs:18
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
The server has not found anything matching the Request-URI. No indication is given of whether the con...
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
Class managing a script expression.
Definition: Expression.cs:39
Abstract base class for token events.
Definition: TokenEvent.cs:19
void Serialize(StringBuilder Xml)
Serializes the event to XML.
Definition: TokenEvent.cs:111
Marketplace processor, brokering sales of items via tenders and offers defined in smart contracts.
const string NeuroFeaturesNamespace
https://paiwise.tagroot.io/Schema/NeuroFeatures.xsd
Abstract base class for agent resources supporting the POST method.
static AccountUser AssertUserAuthenticated(HttpRequest Request)
Makes sure the request is made by an authenticated API user.
const string AgentNamespace
https://waher.se/Schema/BrokerAgent.xsd
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Basic interface for all types of elements.
Definition: IElement.cs:20