Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GetBalance.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
11using Waher.Script;
14
16{
21 {
25 public GetBalance()
26 : base("Wallet/GetBalance",
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(GetBalance).Namespace + ".JSON.GetBalance.req");
33 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetBalance).Namespace + ".XML.GetBalance.req");
34
43 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
44 {
46
47 EDaler.Wallet Wallet;
48
49 if (XmppServerModule.EDaler is null)
50 {
51 Wallet = await Database.FindFirstIgnoreRest<EDaler.Wallet>(
52 new FilterFieldEqualTo("Account", User.UserName));
53 }
54 else
55 Wallet = await XmppServerModule.EDaler.GetWallet(User.UserName, Gateway.Domain);
56
57 if (Wallet is null)
58 {
59 string Currency = string.Empty;
60
61 if (!(XmppServerModule.EDaler is null))
62 Currency = await XmppServerModule.EDaler.GetDefaultCurrency();
63
64 Wallet = new EDaler.Wallet()
65 {
66 Account = User.UserName,
67 Balance = 0,
68 Reserved = 0,
69 BalanceTimestamp = DateTime.UtcNow,
70 Created = DateTime.UtcNow,
71 Currency = Currency,
72 Domain = Gateway.Domain
73 };
74 }
75
76 await Response.Return(new NamedDictionary<string, object>("Balance", AgentNamespace)
77 {
78 { "amount", Wallet.Balance },
79 { "reserved", Wallet.Reserved },
80 { "currency", Wallet.Currency.Value },
81 { "timestamp", Wallet.BalanceTimestamp }
82 });
83 }
84 }
85}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
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
Task Return(Exception ex)
Returns an error to the client.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
This filter selects objects that have a named field equal to a given value.
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
Contains information about a broker account.
Definition: Account.cs:41
Retains the current balance of an account.
Definition: Wallet.cs:17
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 the balance of the wallet associated with the account of the caller.
Definition: GetBalance.cs:21
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Definition: GetBalance.cs:43
GetBalance()
Gets the balance of the wallet associated with the account of the caller.
Definition: GetBalance.cs:25
Service Module hosting the XMPP broker and its components.