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;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
6using Waher.Content;
12using Waher.Script;
17
19{
24 {
29 : base("Tokens/GetTokenEvents",
30 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
31 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
32 {
33 }
34
35 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(GetTokenEvents).Namespace + ".JSON.GetTokenEvents.req");
36 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetTokenEvents).Namespace + ".XML.GetTokenEvents.req");
37
46 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
47 {
49 string TokenId = (string)Parameters["PTokenId"].AssociatedObjectValue;
50
51 int Offset = 0;
52 int MaxCount = int.MaxValue;
53
54 if (Parameters.TryGetValue("POffset", out IElement E) && !(E is null) &&
55 E.AssociatedObjectValue is double d)
56 {
57 Offset = (int)d;
58 }
59
60 if (Parameters.TryGetValue("PMaxCount", out E) && !(E is null) &&
61 E.AssociatedObjectValue is double d2)
62 {
63 MaxCount = (int)d2;
64 }
65
66 string BareJid = User.UserName + "@" + Gateway.Domain;
67
68 Token Token = await NeuroFeaturesProcessor.GetToken(TokenId, true)
69 ?? throw new NotFoundException("Token not found.");
70
71 await GetToken.AssertAuthorization(Request, Token, new XmppAddress(BareJid));
72
73 IEnumerable<TokenEvent> Events = await NeuroFeaturesProcessor.GetTokenEvents(Token, Offset, MaxCount);
74
75 StringBuilder Xml = new StringBuilder();
76
77 Xml.Append("<events xmlns='");
79 Xml.Append("'>");
80
81 foreach (TokenEvent Event in Events)
82 Event.Serialize(Xml);
83
84 Xml.Append("</events>");
85
86 XmlDocument Doc = XML.ParseXml(Xml.ToString(), true);
87
88 await Response.Return(new NamedDictionary<string, object>("EventsResult", AgentNamespace)
89 {
90 { "Events", Doc }
91 });
92 }
93
94 }
95}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Helps with common XML-related tasks.
Definition: XML.cs:21
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
Definition: XML.cs:713
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:3087
Represents an HTTP request.
Definition: HttpRequest.cs:22
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
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
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:13
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:55
Class managing a script expression.
Definition: Expression.cs:41
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:21