Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
HttpPost.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
9
11{
15 public class HttpPost : ActionNode
16 {
17 private Header[] headers;
18 private Resource resource;
19 private Notes.Content content;
20
24 public HttpPost()
25 : base()
26 {
27 }
28
32 public Resource Resource => this.resource;
33
37 [DefaultValueNull]
38 public Notes.Content Content => this.content;
39
43 [DefaultValueNull]
44 public Header[] Headers => this.headers;
45
49 [DefaultValueNull]
50 public string ResponseVariable { get; set; }
51
55 public override string LocalName => nameof(HttpPost);
56
61 public override IStateMachineNode Create()
62 {
63 return new HttpPost();
64 }
65
70 public override async Task Parse(XmlElement Xml)
71 {
72 this.ResponseVariable = XML.Attribute(Xml, "responseVariable");
73
74 await base.Parse(Xml);
75
77 new Type[]
78 {
79 typeof(Resource),
80 typeof(Notes.Content)
81 },
82 new bool[]
83 {
84 true,
85 true
86 });
87 }
88
92 protected override void OnChildNodesUpdated()
93 {
94 base.OnChildNodesUpdated();
95
96 this.resource = this.GetValueElement<Resource>();
97 this.content = this.GetValueElement<Notes.Content>();
98 this.headers = this.GetChildElements<Header>();
99 }
100
105 public override async Task Execute(EvaluationArguments Arguments)
106 {
107 string Resource = (await this.resource.Evaluate(Arguments))?.ToString();
108 Uri ResourceUri = new Uri(Resource);
109
110 object Content = await this.content.Evaluate(Arguments);
111 List<KeyValuePair<string, string>> Headers = new List<KeyValuePair<string, string>>();
112
113 if (!(this.headers is null))
114 {
115 foreach (Header Header in this.headers)
116 {
117 Headers.Add(new KeyValuePair<string, string>(
118 (await Header.Key.Evaluate(Arguments))?.ToString() ?? string.Empty,
119 (await Header.Value.Evaluate(Arguments))?.ToString() ?? string.Empty));
120 }
121 }
122
123 ContentResponse Response = await InternetContent.PostAsync(ResourceUri, Content,
124 Gateway.Certificate, Headers.ToArray());
125
126 if (Response.HasError)
127 throw Response.Error;
128
129 if (!string.IsNullOrEmpty(this.ResponseVariable))
130 Arguments.Variables[this.ResponseVariable] = Response.Decoded;
131 }
132 }
133}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Exception Error
Error response.
Static class managing encoding and decoding of internet content.
static Task< ContentResponse > PostAsync(Uri Uri, object Data, params KeyValuePair< string, string >[] Headers)
Posts to a resource, using a Uniform Resource Identifier (or Locator).
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:1062
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static X509Certificate2 Certificate
Domain certificate.
Definition: Gateway.cs:3082
Abstract base class for State-Machine action nodes.
Definition: ActionNode.cs:9
Defines a HTTP POST action
Definition: HttpPost.cs:16
Notes.Content Content
Content value
Definition: HttpPost.cs:38
Header[] Headers
HTTP Headers
Definition: HttpPost.cs:44
HttpPost()
Defines a HTTP POST action
Definition: HttpPost.cs:24
string ResponseVariable
Optional response variable.
Definition: HttpPost.cs:50
override string LocalName
Local name
Definition: HttpPost.cs:55
override void OnChildNodesUpdated()
Method called whenever ChildNodes is updated.
Definition: HttpPost.cs:92
override async Task Execute(EvaluationArguments Arguments)
Evaluates the action node
Definition: HttpPost.cs:105
override async Task Parse(XmlElement Xml)
Parses the State-machine node.
Definition: HttpPost.cs:70
override IStateMachineNode Create()
Creates a new node of the corresponding type.
Definition: HttpPost.cs:61
Contains information required for evaluating script in a state-machine.
void ConvertValueAttributesToElements(XmlElement Xml, Type[] ValueTypes, bool[] Required)
Converts value attributes to parsed elements. The XML definition has to be parsed before,...
Task< object > Evaluate(EvaluationArguments Arguments)
Evaluates the value node.
Definition: Value.cs:147
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.