Skip to content

Commit 48ec28c

Browse files
author
Adam Nierzad
authored
Merge pull request #34 from cantarus/development
Development
2 parents 75f21a2 + a03ecc0 commit 48ec28c

25 files changed

+573
-180
lines changed

DeployClient/API.cs

+31-24
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,34 @@
55
using System.Net;
66
using System.Net.Http;
77
using System.Threading.Tasks;
8+
using System.Web;
89
using System.Web.Script.Serialization;
910

1011
namespace DeployClient
1112
{
1213
class API
1314
{
14-
private static string APIKey = Properties.Settings.Default.APIKey;
15+
private static string APIKey = Program.Options.APIKey;
1516

1617
private static HttpClient BuildClient()
1718
{
18-
HttpClient client = new HttpClient();
19+
HttpClient client = new HttpClient()
20+
{
21+
BaseAddress = new Uri(new Uri(Program.Options.TargetUri), "DesktopModules/PolyDeploy/API/")
22+
};
1923

20-
client.BaseAddress = new Uri(new Uri(Properties.Settings.Default.TargetUri), "DesktopModules/PolyDeploy/API/");
21-
client.DefaultRequestHeaders.Add("x-api-key", Properties.Settings.Default.APIKey);
22-
client.Timeout = TimeSpan.FromSeconds(25);
24+
client.DefaultRequestHeaders.Add("x-api-key", APIKey);
2325

2426
return client;
2527
}
2628

27-
public static string CreateSession()
29+
public static async Task<string> CreateSessionAsync()
2830
{
2931
string endpoint = "Remote/CreateSession";
3032

3133
using (HttpClient client = BuildClient())
3234
{
33-
string json = client.GetStringAsync(endpoint).Result;
35+
string json = await client.GetStringAsync(endpoint);
3436

3537
JavaScriptSerializer jsonSer = new JavaScriptSerializer();
3638

@@ -47,21 +49,31 @@ public static string CreateSession()
4749
}
4850
}
4951

50-
public static Dictionary<string, dynamic> GetSession(string sessionGuid)
52+
public static async Task<(bool success, Dictionary<string, dynamic> results)> GetSessionAsync(string sessionGuid)
5153
{
5254
string endpoint = string.Format("Remote/GetSession?sessionGuid={0}", sessionGuid);
5355

5456
JavaScriptSerializer jsonSer = new JavaScriptSerializer();
5557

5658
using (HttpClient client = BuildClient())
5759
{
58-
string json = client.GetStringAsync(endpoint).Result;
60+
var httpResponse = await client.GetAsync(endpoint);
61+
if (httpResponse.StatusCode == HttpStatusCode.OK)
62+
{
63+
string json = httpResponse.Content.ReadAsStringAsync().Result;
64+
return (true, jsonSer.Deserialize<Dictionary<string, dynamic>>(json));
65+
}
66+
67+
if (httpResponse.StatusCode == HttpStatusCode.NotFound)
68+
{
69+
return (false, new Dictionary<string, dynamic>(0));
70+
}
5971

60-
return jsonSer.Deserialize<Dictionary<string, dynamic>>(json);
72+
throw new HttpException($"Invalid status code returned from remote api: {httpResponse.StatusCode}");
6173
}
6274
}
6375

64-
public static void AddPackages(string sessionGuid, List<KeyValuePair<string, Stream>> streams)
76+
public static async Task AddPackagesAsync(string sessionGuid, List<KeyValuePair<string, Stream>> streams)
6577
{
6678
string endpoint = string.Format("Remote/AddPackages?sessionGuid={0}", sessionGuid);
6779

@@ -74,11 +86,11 @@ public static void AddPackages(string sessionGuid, List<KeyValuePair<string, Str
7486
form.Add(new StreamContent(keyValuePair.Value), "none", keyValuePair.Key);
7587
}
7688

77-
HttpResponseMessage response = client.PostAsync(endpoint, form).Result;
89+
await client.PostAsync(endpoint, form);
7890
}
7991
}
8092

81-
public static void AddPackageAsync(string sessionGuid, Stream stream, string filename)
93+
public static async Task AddPackageAsync(string sessionGuid, Stream stream, string filename)
8294
{
8395
string endpoint = string.Format("Remote/AddPackages?sessionGuid={0}", sessionGuid);
8496

@@ -88,31 +100,26 @@ public static void AddPackageAsync(string sessionGuid, Stream stream, string fil
88100

89101
form.Add(new StreamContent(stream), "none", filename);
90102

91-
HttpResponseMessage response = client.PostAsync(endpoint, form).Result;
103+
await client.PostAsync(endpoint, form);
92104
}
93105
}
94106

95-
public static bool Install(string sessionGuid, out SortedList<string, dynamic> response)
107+
public static async Task<(bool success, SortedList<string, dynamic> response)> InstallAsync(string sessionGuid)
96108
{
97109
string endpoint = string.Format("Remote/Install?sessionGuid={0}", sessionGuid);
98110

99-
bool success = false;
100-
101111
JavaScriptSerializer jsonSer = new JavaScriptSerializer();
102112

103-
response = null;
104-
105113
using (HttpClient client = BuildClient())
106114
{
107115
try
108116
{
109-
HttpResponseMessage httpResponse = client.GetAsync(endpoint).Result;
117+
HttpResponseMessage httpResponse = await client.GetAsync(endpoint);
110118

111119
if (httpResponse.StatusCode.Equals(HttpStatusCode.OK))
112120
{
113-
success = true;
114-
string json = httpResponse.Content.ReadAsStringAsync().Result;
115-
response = jsonSer.Deserialize<SortedList<string, dynamic>>(json);
121+
string json = await httpResponse.Content.ReadAsStringAsync();
122+
return (true, jsonSer.Deserialize<SortedList<string, dynamic>>(json));
116123
}
117124
}
118125
catch (Exception ex)
@@ -121,7 +128,7 @@ public static bool Install(string sessionGuid, out SortedList<string, dynamic> r
121128
}
122129

123130
// Always fail.
124-
return false;
131+
return (false, null);
125132
}
126133
}
127134
}

DeployClient/CommandlineOptions.cs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using CommandLine;
2+
using CommandLine.Text;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace DeployClient
10+
{
11+
internal class CommandLineOptions
12+
{
13+
[Option("silent",
14+
DefaultValue = false,
15+
HelpText = "Whether to not print any output")]
16+
public bool IsSilent { get; set; }
17+
18+
[Option("no-prompt",
19+
DefaultValue = false,
20+
HelpText = "Whether to prompt the user for confirmation")]
21+
public bool NoPrompt { get; set; }
22+
23+
[Option("target-uri",
24+
Required = false,
25+
HelpText = "The URL to deploy the packages to.",
26+
DefaultValue = null)]
27+
public string TargetUri { get; set; }
28+
29+
[Option("api-key",
30+
Required = false,
31+
HelpText = "The API Key to use",
32+
DefaultValue = null)]
33+
public string APIKey { get; set; }
34+
35+
[Option("encryption-key",
36+
Required = false,
37+
HelpText = "The Encryption Key to use",
38+
DefaultValue = null)]
39+
public string EncryptionKey { get; set; }
40+
41+
[Option("installation-status-timeout",
42+
Required = false,
43+
HelpText = "The number of seconds to ignore 404 errors when checking installation status",
44+
DefaultValue = 60)]
45+
public double InstallationStatusTimeout { get; set; }
46+
47+
[HelpOption]
48+
public string GetUsage()
49+
{
50+
return HelpText.AutoBuild(this,
51+
(HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
52+
}
53+
}
54+
}

DeployClient/DeployClient.csproj

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<DefineConstants>DEBUG;TRACE</DefineConstants>
2323
<ErrorReport>prompt</ErrorReport>
2424
<WarningLevel>4</WarningLevel>
25+
<LangVersion>latest</LangVersion>
2526
</PropertyGroup>
2627
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2728
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -31,11 +32,19 @@
3132
<DefineConstants>TRACE</DefineConstants>
3233
<ErrorReport>prompt</ErrorReport>
3334
<WarningLevel>4</WarningLevel>
35+
<LangVersion>latest</LangVersion>
3436
</PropertyGroup>
3537
<ItemGroup>
38+
<Reference Include="CommandLine, Version=1.9.71.2, Culture=neutral, PublicKeyToken=de6f01bd326f8c32, processorArchitecture=MSIL">
39+
<HintPath>..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath>
40+
</Reference>
3641
<Reference Include="System" />
3742
<Reference Include="System.Configuration" />
3843
<Reference Include="System.Core" />
44+
<Reference Include="System.ValueTuple, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
45+
<HintPath>..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
46+
</Reference>
47+
<Reference Include="System.Web" />
3948
<Reference Include="System.Web.Extensions" />
4049
<Reference Include="System.Xml.Linq" />
4150
<Reference Include="System.Data.DataSetExtensions" />
@@ -46,6 +55,7 @@
4655
</ItemGroup>
4756
<ItemGroup>
4857
<Compile Include="API.cs" />
58+
<Compile Include="CommandLineOptions.cs" />
4959
<Compile Include="Program.cs" />
5060
<Compile Include="Properties\AssemblyInfo.cs" />
5161
<Compile Include="Properties\Settings.Designer.cs">
@@ -58,6 +68,7 @@
5868
<None Include="App.config">
5969
<SubType>Designer</SubType>
6070
</None>
71+
<None Include="packages.config" />
6172
<None Include="Properties\Settings.settings">
6273
<Generator>SettingsSingleFileGenerator</Generator>
6374
<LastGenOutput>Settings.Designer.cs</LastGenOutput>

0 commit comments

Comments
 (0)