-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiCall.cs
87 lines (78 loc) · 3.15 KB
/
ApiCall.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.Net;
using DatawoodGH.Network;
using DatawoodGH.Properties;
using Grasshopper.Kernel;
using Rhino.Geometry;
namespace DatawoodGH.Network
{
public class ApiCall : NetworkComponent
{
/// <summary>
/// Initializes a new instance of the APICallComponent class.
/// </summary>
public ApiCall()
: base("API Call", "API",
"Send get call to an api and retrieve result"
)
{
}
protected override void RegisterInputParams(GH_InputParamManager pManager)
{
pManager.AddTextParameter("URL", "U", "Url of api to call, make sure this url does not contain any - characters", GH_ParamAccess.item);
pManager.AddBooleanParameter("Run", "R", "Runs component", GH_ParamAccess.item, true);
pManager.AddBooleanParameter("Post", "P", "Is a post call", GH_ParamAccess.item, false);
}
protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
pManager.AddTextParameter("JSON", "J", "The JSON Output", GH_ParamAccess.item);
pManager.AddBooleanParameter("Finished", "F", "Finished it's call", GH_ParamAccess.item);
}
protected override void SolveInstance(IGH_DataAccess DA)
{
string url = null;
string result = null;
bool run = false;
bool post = false;
DA.SetData("Finished", false);
if (!DA.GetData("URL", ref url)) return;
DA.GetData("Run", ref run);
DA.GetData("Post", ref post);
if (run) {
try
{
using (WebClient wc = new WebClient())
{
if (post) {
//http://127.0.0.1:5000/start_scan/multiangle/single/preset1/C:\\Users\\test\\folder\\newfolder
//URI builder replaces \ with /. Thus the above url won't work. This is why we replace \ with - so it can be reverted serverside.
url = url.Replace('\\', '-');
result = wc.UploadString(url, "POST", string.Empty);
}
else {
//this is the get call
result = wc.DownloadString(url);
}
}
DA.SetData("JSON", result);
DA.SetData("Finished", true);
}
catch (WebException webex) {
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, webex.Message);
}
}
}
/// <summary>
/// Provides an Icon for the component.
/// </summary>
protected override System.Drawing.Bitmap Icon => Resources.datawoodapi.ToBitmap();
/// <summary>
/// Gets the unique ID for this component. Do not change this ID after release.
/// </summary>
public override Guid ComponentGuid
{
get { return new Guid("770D14BD-F236-4AED-8955-2002379C18F3"); }
}
}
}