-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathAPI.cs
More file actions
138 lines (109 loc) · 4.42 KB
/
API.cs
File metadata and controls
138 lines (109 loc) · 4.42 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Script.Serialization;
namespace DeployClient
{
class API
{
private static string APIKey = Program.Options.APIKey;
private static HttpClient BuildClient()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpClient client = new HttpClient()
{
BaseAddress = new Uri(new Uri(Program.Options.TargetUri), "DesktopModules/PolyDeploy/API/")
};
client.DefaultRequestHeaders.Add("x-api-key", APIKey);
return client;
}
public static async Task<string> CreateSessionAsync()
{
string endpoint = "Remote/CreateSession";
using (HttpClient client = BuildClient())
{
string json = await client.GetStringAsync(endpoint);
JavaScriptSerializer jsonSer = new JavaScriptSerializer();
Dictionary<string, dynamic> session = jsonSer.Deserialize<Dictionary<string, dynamic>>(json);
string sessionGuid = null;
if (session.ContainsKey("Guid"))
{
sessionGuid = session["Guid"];
}
return sessionGuid;
}
}
public static async Task<(bool success, Dictionary<string, dynamic> results)> GetSessionAsync(string sessionGuid)
{
string endpoint = string.Format("Remote/GetSession?sessionGuid={0}", sessionGuid);
JavaScriptSerializer jsonSer = new JavaScriptSerializer();
using (HttpClient client = BuildClient())
{
HttpResponseMessage httpResponse;
try
{
httpResponse = await client.GetAsync(endpoint);
}
catch
{
return (false, new Dictionary<string, dynamic>(0));
}
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
string json = await httpResponse.Content.ReadAsStringAsync();
return (true, jsonSer.Deserialize<Dictionary<string, dynamic>>(json));
}
if (httpResponse.StatusCode == HttpStatusCode.NotFound)
{
return (false, new Dictionary<string, dynamic>(0));
}
throw new HttpException($"Invalid status code returned from remote api: {httpResponse.StatusCode}");
}
}
public static async Task AddPackagesAsync(string sessionGuid, List<KeyValuePair<string, Stream>> streams)
{
string endpoint = string.Format("Remote/AddPackages?sessionGuid={0}", sessionGuid);
using (HttpClient client = BuildClient())
{
MultipartFormDataContent form = new MultipartFormDataContent();
foreach (KeyValuePair<string, Stream> keyValuePair in streams)
{
form.Add(new StreamContent(keyValuePair.Value), "none", keyValuePair.Key);
}
await client.PostAsync(endpoint, form);
}
}
public static async Task AddPackageAsync(string sessionGuid, Stream stream, string filename)
{
string endpoint = string.Format("Remote/AddPackages?sessionGuid={0}", sessionGuid);
using (HttpClient client = BuildClient())
{
MultipartFormDataContent form = new MultipartFormDataContent();
form.Add(new StreamContent(stream), "none", filename);
await client.PostAsync(endpoint, form);
}
}
public static async Task<(bool success, SortedList<string, dynamic> response)> InstallAsync(string sessionGuid)
{
string endpoint = string.Format("Remote/Install?sessionGuid={0}", sessionGuid);
using (HttpClient client = BuildClient())
{
try
{
await client.GetAsync(endpoint);
}
catch (Exception ex)
{
// Nothing to do.
}
// Always fail.
return (false, null);
}
}
}
}