Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ProcessEDalerUri.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
14using Waher.Script;
19
21{
26 {
31 : base("Wallet/ProcessEDalerUri",
32 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
33 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
34 {
35 }
36
37 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(ProcessEDalerUri).Namespace + ".JSON.ProcessEDalerUri.req");
38 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(ProcessEDalerUri).Namespace + ".XML.ProcessEDalerUri.req");
39
48 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
49 {
51 string Uri = (string)Parameters["PUri"].AssociatedObjectValue;
52
54 throw new ServiceUnavailableException("Transaction module not running.");
55
57 EDalerUri Parsed = await EDalerUri.Parse(Uri, State, XmppServerModule.EDaler);
58
59 if (!(Parsed is null))
60 {
61 if (XmppServerModule.EDaler is null)
62 throw new ServiceUnavailableException("EDaler component not properly initialized.");
63
64 await XmppServerModule.EDaler.ProcessUri(Parsed,
65 new XmppAddress(User.UserName + "@" + Gateway.Domain));
66 }
67
68 if (State.ErrorType.HasValue || !string.IsNullOrEmpty(State.ErrorMessage))
69 {
70 string Msg = State.ErrorMessage;
71 if (string.IsNullOrEmpty(Msg))
72 Msg = "Unable to process URI.";
73
74 throw (State.ErrorType ?? EDalerUriErrorType.Internal) switch
75 {
76 EDalerUriErrorType.BadRequest => new BadRequestException(Msg),
77 EDalerUriErrorType.Forbidden => new ForbiddenException(Request, Msg),
78 EDalerUriErrorType.ResourceConstraint => new TooManyRequestsException(Msg),
79 EDalerUriErrorType.Conflict => new ConflictException(Msg),
81 };
82 }
83
85
86 if (XmppServerModule.EDaler is null)
87 {
88 Wallet = await Database.FindFirstIgnoreRest<EDaler.Wallet>(
89 new FilterFieldEqualTo("Account", User.UserName));
90 }
91 else
92 Wallet = await XmppServerModule.EDaler.GetWallet(User.UserName, Gateway.Domain);
93
94 if (Wallet is null)
95 {
96 string Currency = string.Empty;
97
98 if (!(XmppServerModule.EDaler is null))
99 Currency = await XmppServerModule.EDaler.GetDefaultCurrency();
100
101 Wallet = new EDaler.Wallet()
102 {
103 Account = User.UserName,
104 Balance = 0,
105 Reserved = 0,
106 BalanceTimestamp = DateTime.UtcNow,
107 Created = DateTime.UtcNow,
108 Currency = Currency,
109 Domain = Gateway.Domain
110 };
111 }
112
113 XmlDocument TransactionResult;
114
115 if (string.IsNullOrEmpty(State.ResultXml))
116 TransactionResult = null;
117 else
118 {
119 TransactionResult = XML.ParseXml(State.ResultXml, true);
120 }
121
122 await Response.Return(new NamedDictionary<string, object>("ProcessResult", AgentNamespace)
123 {
124 { "Balance", new Dictionary<string, object>()
125 {
126 { "amount", Wallet.Balance },
127 { "reserved", Wallet.Reserved },
128 { "currency", Wallet.Currency.Value },
129 { "timestamp", Wallet.BalanceTimestamp }
130 }
131 },
132 { "Result", TransactionResult }
133 });
134 }
135 }
136}
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
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
The request could not be completed due to a conflict with the current state of the resource....
The server understood the request, but is refusing to fulfill it. Authorization will not help and the...
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.
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
The user has sent too many requests in a given amount of time. Intended for use with rate limiting sc...
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
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
Module making sure no unfinished transactions are left when system ends.
static bool Running
If the transaction module is running.
Class managing a script expression.
Definition: Expression.cs:41
Contains information about a broker account.
Definition: Account.cs:41
Abstract base class for eDaler URIs
Definition: EDalerUri.cs:20
static async Task< EDalerUri > Parse(string Uri, EDalerUriState State, EDalerComponent EDaler)
Parses an eDaler URI
Definition: EDalerUri.cs:260
EDalerUriErrorType? ErrorType
Type of error, if any.
string ErrorMessage
Error message, or null if no error.
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
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Service Module hosting the XMPP broker and its components.
Definition: ImplTypes.g.cs:58