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