5
5
using System . Net ;
6
6
using System . Net . Http ;
7
7
using System . Threading . Tasks ;
8
+ using System . Web ;
8
9
using System . Web . Script . Serialization ;
9
10
10
11
namespace DeployClient
11
12
{
12
13
class API
13
14
{
14
- private static string APIKey = Properties . Settings . Default . APIKey ;
15
+ private static string APIKey = Program . Options . APIKey ;
15
16
16
17
private static HttpClient BuildClient ( )
17
18
{
18
- HttpClient client = new HttpClient ( ) ;
19
+ HttpClient client = new HttpClient ( )
20
+ {
21
+ BaseAddress = new Uri ( new Uri ( Program . Options . TargetUri ) , "DesktopModules/PolyDeploy/API/" )
22
+ } ;
19
23
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 ) ;
23
25
24
26
return client ;
25
27
}
26
28
27
- public static string CreateSession ( )
29
+ public static async Task < string > CreateSessionAsync ( )
28
30
{
29
31
string endpoint = "Remote/CreateSession" ;
30
32
31
33
using ( HttpClient client = BuildClient ( ) )
32
34
{
33
- string json = client . GetStringAsync ( endpoint ) . Result ;
35
+ string json = await client . GetStringAsync ( endpoint ) ;
34
36
35
37
JavaScriptSerializer jsonSer = new JavaScriptSerializer ( ) ;
36
38
@@ -47,21 +49,31 @@ public static string CreateSession()
47
49
}
48
50
}
49
51
50
- public static Dictionary < string , dynamic > GetSession ( string sessionGuid )
52
+ public static async Task < ( bool success , Dictionary < string , dynamic > results ) > GetSessionAsync ( string sessionGuid )
51
53
{
52
54
string endpoint = string . Format ( "Remote/GetSession?sessionGuid={0}" , sessionGuid ) ;
53
55
54
56
JavaScriptSerializer jsonSer = new JavaScriptSerializer ( ) ;
55
57
56
58
using ( HttpClient client = BuildClient ( ) )
57
59
{
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
+ }
59
71
60
- return jsonSer . Deserialize < Dictionary < string , dynamic > > ( json ) ;
72
+ throw new HttpException ( $ "Invalid status code returned from remote api: { httpResponse . StatusCode } " ) ;
61
73
}
62
74
}
63
75
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 )
65
77
{
66
78
string endpoint = string . Format ( "Remote/AddPackages?sessionGuid={0}" , sessionGuid ) ;
67
79
@@ -74,11 +86,11 @@ public static void AddPackages(string sessionGuid, List<KeyValuePair<string, Str
74
86
form . Add ( new StreamContent ( keyValuePair . Value ) , "none" , keyValuePair . Key ) ;
75
87
}
76
88
77
- HttpResponseMessage response = client . PostAsync ( endpoint , form ) . Result ;
89
+ await client . PostAsync ( endpoint , form ) ;
78
90
}
79
91
}
80
92
81
- public static void AddPackageAsync ( string sessionGuid , Stream stream , string filename )
93
+ public static async Task AddPackageAsync ( string sessionGuid , Stream stream , string filename )
82
94
{
83
95
string endpoint = string . Format ( "Remote/AddPackages?sessionGuid={0}" , sessionGuid ) ;
84
96
@@ -88,31 +100,26 @@ public static void AddPackageAsync(string sessionGuid, Stream stream, string fil
88
100
89
101
form . Add ( new StreamContent ( stream ) , "none" , filename ) ;
90
102
91
- HttpResponseMessage response = client . PostAsync ( endpoint , form ) . Result ;
103
+ await client . PostAsync ( endpoint , form ) ;
92
104
}
93
105
}
94
106
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 )
96
108
{
97
109
string endpoint = string . Format ( "Remote/Install?sessionGuid={0}" , sessionGuid ) ;
98
110
99
- bool success = false ;
100
-
101
111
JavaScriptSerializer jsonSer = new JavaScriptSerializer ( ) ;
102
112
103
- response = null ;
104
-
105
113
using ( HttpClient client = BuildClient ( ) )
106
114
{
107
115
try
108
116
{
109
- HttpResponseMessage httpResponse = client . GetAsync ( endpoint ) . Result ;
117
+ HttpResponseMessage httpResponse = await client . GetAsync ( endpoint ) ;
110
118
111
119
if ( httpResponse . StatusCode . Equals ( HttpStatusCode . OK ) )
112
120
{
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 ) ) ;
116
123
}
117
124
}
118
125
catch ( Exception ex )
@@ -121,7 +128,7 @@ public static bool Install(string sessionGuid, out SortedList<string, dynamic> r
121
128
}
122
129
123
130
// Always fail.
124
- return false ;
131
+ return ( false , null ) ;
125
132
}
126
133
}
127
134
}
0 commit comments