Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GetTokens.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
10using Waher.Script;
14
16{
21 {
25 public GetTokens()
26 : base("Tokens/GetTokens",
27 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
28 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
29 {
30 }
31
32 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(GetTokens).Namespace + ".JSON.GetTokens.req");
33 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetTokens).Namespace + ".XML.GetTokens.req");
34
43 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
44 {
46
47 int Offset = 0;
48 int MaxCount = int.MaxValue;
49 bool References = true;
50
51 if (Parameters.TryGetValue("POffset", out IElement E) && !(E is null) &&
52 E.AssociatedObjectValue is double d)
53 {
54 Offset = (int)d;
55 }
56
57 if (Parameters.TryGetValue("PMaxCount", out E) && !(E is null) &&
58 E.AssociatedObjectValue is double d2)
59 {
60 MaxCount = (int)d2;
61 }
62
63 if (Parameters.TryGetValue("PReferences", out E) && !(E is null) &&
64 E.AssociatedObjectValue is bool b)
65 {
66 References = b;
67 }
68
69 string Xml;
70 string BareJid = User.UserName + "@" + Gateway.Domain;
71 IEnumerable<TokenReference> TokenReferences = await Database.Find<TokenReference>(Offset, MaxCount,
72 new FilterFieldEqualTo("OwnerJid", BareJid), "-Updated");
73
74 if (References)
75 Xml = NeuroFeaturesProcessor.SerializeTokenReferences(TokenReferences);
76 else
77 {
78 List<Token> Tokens = new List<Token>();
79
80 foreach (TokenReference Ref in TokenReferences)
81 {
82 Token Token = await NeuroFeaturesProcessor.GetToken(Ref.TokenId, false);
83 if (!(Token is null))
84 Tokens.Add(Token);
85 }
86
87 Xml = NeuroFeaturesProcessor.SerializeTokenReferences(Tokens);
88 }
89
90 XmlDocument Doc = new XmlDocument()
91 {
92 PreserveWhitespace = true
93 };
94 Doc.LoadXml(Xml);
95
96 await Response.Return(new NamedDictionary<string, object>("TokensResult", AgentNamespace)
97 {
98 { "Tokens", Doc }
99 });
100 }
101 }
102}
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
async Task Return(object Object)
Returns an object to the client. This method can only be called once per response,...
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:247
This filter selects objects that have a named field equal to a given value.
Class managing a script expression.
Definition: Expression.cs:39
Marketplace processor, brokering sales of items via tenders and offers defined in smart contracts.
Contains a reference to a token, possibly on another neuron.
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
Gets tokens on the Neuron(R) that the account owns.
Definition: GetTokens.cs:21
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Definition: GetTokens.cs:43
GetTokens()
Gets tokens on the Neuron(R) that the account owns.
Definition: GetTokens.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20