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;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
10using Waher.Script;
13
15{
20 {
24 public GetBalance()
25 : base("Wallet/GetBalance",
26 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
27 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
28 {
29 }
30
31 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(GetBalance).Namespace + ".JSON.GetBalance.req");
32 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetBalance).Namespace + ".XML.GetBalance.req");
33
42 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
43 {
45
46 EDaler.Wallet Wallet;
47
48 if (XmppServerModule.EDaler is null)
49 {
50 Wallet = await Database.FindFirstIgnoreRest<EDaler.Wallet>(
51 new FilterFieldEqualTo("Account", User.UserName));
52 }
53 else
54 Wallet = await XmppServerModule.EDaler.GetWallet(User.UserName, Gateway.Domain);
55
56 if (Wallet is null)
57 {
58 string Currency = string.Empty;
59
60 if (!(XmppServerModule.EDaler is null))
61 Currency = await XmppServerModule.EDaler.GetDefaultCurrency();
62
63 Wallet = new EDaler.Wallet()
64 {
65 Account = User.UserName,
66 Balance = 0,
67 Reserved = 0,
68 BalanceTimestamp = DateTime.UtcNow,
69 Created = DateTime.UtcNow,
70 Currency = Currency,
71 Domain = Gateway.Domain
72 };
73 }
74
75 await Response.Return(new NamedDictionary<string, object>("Balance", AgentNamespace)
76 {
77 { "amount", Wallet.Balance },
78 { "reserved", Wallet.Reserved },
79 { "currency", Wallet.Currency.Value },
80 { "timestamp", Wallet.BalanceTimestamp }
81 });
82 }
83 }
84}
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
This filter selects objects that have a named field equal to a given value.
Class managing a script expression.
Definition: Expression.cs:39
Contains information about a broker account.
Definition: Account.cs:28
Retains the current balance of an account.
Definition: Wallet.cs:16
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:20
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Definition: GetBalance.cs:42
GetBalance()
Gets the balance of the wallet associated with the account of the caller.
Definition: GetBalance.cs:24
Service Module hosting the XMPP broker and its components.