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;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
8
10{
14 public class HttpPost : ActionNode
15 {
16 private Header[] headers;
17 private Resource resource;
18 private Notes.Content content;
19
23 public HttpPost()
24 : base()
25 {
26 }
27
31 public Resource Resource => this.resource;
32
36 [DefaultValueNull]
37 public Notes.Content Content => this.content;
38
42 [DefaultValueNull]
43 public Header[] Headers => this.headers;
44
48 [DefaultValueNull]
49 public string ResponseVariable { get; set; }
50
54 public override string LocalName => nameof(HttpPost);
55
60 public override IStateMachineNode Create()
61 {
62 return new HttpPost();
63 }
64
69 public override async Task Parse(XmlElement Xml)
70 {
71 this.ResponseVariable = XML.Attribute(Xml, "responseVariable");
72
73 await base.Parse(Xml);
74
76 new Type[]
77 {
78 typeof(Resource),
79 typeof(Notes.Content)
80 },
81 new bool[]
82 {
83 true,
84 true
85 });
86 }
87
91 protected override void OnChildNodesUpdated()
92 {
93 base.OnChildNodesUpdated();
94
95 this.resource = this.GetValueElement<Resource>();
96 this.content = this.GetValueElement<Notes.Content>();
97 this.headers = this.GetChildElements<Header>();
98 }
99
104 public override async Task Execute(EvaluationArguments Arguments)
105 {
106 string Resource = (await this.resource.Evaluate(Arguments))?.ToString();
107 Uri ResourceUri = new Uri(Resource);
108
109 object Content = await this.content.Evaluate(Arguments);
110 List<KeyValuePair<string, string>> Headers = new List<KeyValuePair<string, string>>();
111
112 if (!(this.headers is null))
113 {
114 foreach (Header Header in this.headers)
115 {
116 Headers.Add(new KeyValuePair<string, string>(
117 (await Header.Key.Evaluate(Arguments))?.ToString() ?? string.Empty,
118 (await Header.Value.Evaluate(Arguments))?.ToString() ?? string.Empty));
119 }
120 }
121
122 object Response = await InternetContent.PostAsync(ResourceUri, Content, Headers.ToArray());
123
124 if (!string.IsNullOrEmpty(this.ResponseVariable))
125 Arguments.Variables[this.ResponseVariable] = Response;
126 }
127 }
128}
Static class managing encoding and decoding of internet content.
static Task< object > 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:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
Abstract base class for State-Machine action nodes.
Definition: ActionNode.cs:9
Defines a HTTP POST action
Definition: HttpPost.cs:15
Notes.Content Content
Content value
Definition: HttpPost.cs:37
Header[] Headers
HTTP Headers
Definition: HttpPost.cs:43
HttpPost()
Defines a HTTP POST action
Definition: HttpPost.cs:23
string ResponseVariable
Optional response variable.
Definition: HttpPost.cs:49
override string LocalName
Local name
Definition: HttpPost.cs:54
override void OnChildNodesUpdated()
Method called whenever ChildNodes is updated.
Definition: HttpPost.cs:91
override async Task Execute(EvaluationArguments Arguments)
Evaluates the action node
Definition: HttpPost.cs:104
override async Task Parse(XmlElement Xml)
Parses the State-machine node.
Definition: HttpPost.cs:69
override IStateMachineNode Create()
Creates a new node of the corresponding type.
Definition: HttpPost.cs:60
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:146
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.