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;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
12using Waher.Script;
17
19{
24 {
29 : base("Wallet/ProcessEDalerUri",
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(ProcessEDalerUri).Namespace + ".JSON.ProcessEDalerUri.req");
36 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(ProcessEDalerUri).Namespace + ".XML.ProcessEDalerUri.req");
37
46 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
47 {
49 string Uri = (string)Parameters["PUri"].AssociatedObjectValue;
50
52 throw new ServiceUnavailableException("Transaction module not running.");
53
55 EDalerUri Parsed = await EDalerUri.Parse(Uri, State, XmppServerModule.EDaler);
56
57 if (!(Parsed is null))
58 {
59 if (XmppServerModule.EDaler is null)
60 throw new ServiceUnavailableException("EDaler component not properly initialized.");
61
62 await XmppServerModule.EDaler.ProcessUri(Parsed,
63 new XmppAddress(User.UserName + "@" + Gateway.Domain));
64 }
65
66 if (State.ErrorType.HasValue || !string.IsNullOrEmpty(State.ErrorMessage))
67 {
68 string Msg = State.ErrorMessage;
69 if (string.IsNullOrEmpty(Msg))
70 Msg = "Unable to process URI.";
71
72 switch (State.ErrorType ?? EDalerUriErrorType.Internal)
73 {
74 case EDalerUriErrorType.BadRequest:
75 throw new BadRequestException(Msg);
76
77 case EDalerUriErrorType.Forbidden:
78 throw new ForbiddenException(Msg);
79
80 case EDalerUriErrorType.ServiceUnavailable:
81 case EDalerUriErrorType.Remote:
82 case EDalerUriErrorType.Internal:
83 default:
84 throw new ServiceUnavailableException(Msg);
85
86 case EDalerUriErrorType.ResourceConstraint:
87 throw new TooManyRequestsException(Msg);
88
89 case EDalerUriErrorType.Conflict:
90 throw new ConflictException(Msg);
91 }
92 }
93
95
96 if (XmppServerModule.EDaler is null)
97 {
98 Wallet = await Database.FindFirstIgnoreRest<EDaler.Wallet>(
99 new FilterFieldEqualTo("Account", User.UserName));
100 }
101 else
102 Wallet = await XmppServerModule.EDaler.GetWallet(User.UserName, Gateway.Domain);
103
104 if (Wallet is null)
105 {
106 string Currency = string.Empty;
107
108 if (!(XmppServerModule.EDaler is null))
109 Currency = await XmppServerModule.EDaler.GetDefaultCurrency();
110
111 Wallet = new EDaler.Wallet()
112 {
113 Account = User.UserName,
114 Balance = 0,
115 Reserved = 0,
116 BalanceTimestamp = DateTime.UtcNow,
117 Created = DateTime.UtcNow,
118 Currency = Currency,
119 Domain = Gateway.Domain
120 };
121 }
122
123 XmlDocument TransactionResult;
124
125 if (string.IsNullOrEmpty(State.ResultXml))
126 TransactionResult = null;
127 else
128 {
129 TransactionResult = new XmlDocument()
130 {
131 PreserveWhitespace = true
132 };
133 TransactionResult.LoadXml(State.ResultXml);
134 }
135
136 await Response.Return(new NamedDictionary<string, object>("ProcessResult", AgentNamespace)
137 {
138 { "Balance", new Dictionary<string, object>()
139 {
140 { "amount", Wallet.Balance },
141 { "reserved", Wallet.Reserved },
142 { "currency", Wallet.Currency.Value },
143 { "timestamp", Wallet.BalanceTimestamp }
144 }
145 },
146 { "Result", TransactionResult }
147 });
148 }
149 }
150}
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
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: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,...
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:19
This filter selects objects that have a named field equal to a given value.
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:39
Contains information about a broker account.
Definition: Account.cs:28
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: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
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.