6
6
using System . Net . Http . Headers ;
7
7
using System . Threading . Tasks ;
8
8
using System . Collections . Generic ;
9
+ using System . Net ;
9
10
using Newtonsoft . Json ;
10
11
using Newtonsoft . Json . Linq ;
11
12
using CloudCMS ;
@@ -105,12 +106,24 @@ public async Task<JObject> PostAsync(string uri, IDictionary<string, string> que
105
106
return await RequestAsync ( uri , method , queryParams , body ) ;
106
107
}
107
108
109
+ public Task < JObject > PostAsync ( string uri , IDictionary < string , string > queryParams , JObject body )
110
+ {
111
+ HttpContent content = new StringContent ( body . ToString ( ) ) ;
112
+ return PostAsync ( uri , queryParams , content ) ;
113
+ }
114
+
108
115
public async Task < JObject > PutAsync ( string uri , IDictionary < string , string > queryParams = null , HttpContent body = null )
109
116
{
110
117
HttpMethod method = HttpMethod . Put ;
111
118
return await RequestAsync ( uri , method , queryParams , body ) ;
112
119
}
113
120
121
+ public Task < JObject > PutAsync ( string uri , IDictionary < string , string > queryParams , JObject body )
122
+ {
123
+ HttpContent content = new StringContent ( body . ToString ( ) ) ;
124
+ return PutAsync ( uri , queryParams , content ) ;
125
+ }
126
+
114
127
public async Task < JObject > DeleteAsync ( string uri , IDictionary < string , string > queryParams = null )
115
128
{
116
129
HttpMethod method = HttpMethod . Delete ;
@@ -148,6 +161,14 @@ public async Task UploadAsync(string uri, IDictionary<string, string> paramMap,
148
161
HttpContent content = GenerateUploadContent ( payloads ) ;
149
162
await _requestAsync ( uri , HttpMethod . Post , paramMap , content ) ;
150
163
}
164
+
165
+ public async Task < IPlatform > ReadCurrentPlatform ( )
166
+ {
167
+ string uri = "/" ;
168
+ JObject response = await GetAsync ( uri ) ;
169
+ IPlatform result = new Platform ( this , response ) ;
170
+ return result ;
171
+ }
151
172
152
173
private HttpContent GenerateUploadContent ( byte [ ] bytes , string mimetype )
153
174
{
@@ -179,7 +200,13 @@ public async Task<JObject> RequestAsync(string uri, HttpMethod method, IDictiona
179
200
{
180
201
HttpResponseMessage response = await _requestAsync ( uri , method , queryParams , body ) ;
181
202
string responseString = await response . Content . ReadAsStringAsync ( ) ;
182
- return JObject . Parse ( responseString ) ;
203
+ JObject result = JObject . Parse ( responseString ) ;
204
+
205
+ if ( result . ContainsKey ( "error" ) )
206
+ {
207
+ throw new CloudCMSException ( "whoa" ) ;
208
+ }
209
+ return result ;
183
210
}
184
211
185
212
public async Task < string > RequestStringAsync ( string uri , HttpMethod method , IDictionary < string , string > queryParams = null , HttpContent body = null )
@@ -210,6 +237,17 @@ private async Task<HttpResponseMessage> _requestAsync(string uri, HttpMethod met
210
237
client . DefaultRequestHeaders . Authorization = auth ;
211
238
212
239
HttpResponseMessage response = await client . SendAsync ( request ) ;
240
+ if ( response . StatusCode == HttpStatusCode . Unauthorized )
241
+ {
242
+ // Refresh and retry
243
+ await RefreshTokenAsync ( ) ;
244
+ auth = new AuthenticationHeaderValue ( token . token_type , token . access_token ) ;
245
+ client . DefaultRequestHeaders . Authorization = auth ;
246
+
247
+ request = new HttpRequestMessage ( method , url ) ;
248
+ response = await client . SendAsync ( request ) ;
249
+ }
250
+
213
251
if ( ! response . IsSuccessStatusCode )
214
252
{
215
253
string responseString = await response . Content . ReadAsStringAsync ( ) ;
@@ -231,6 +269,11 @@ private string BuildUrl(string uri, IDictionary<string, string> queryParams)
231
269
query [ "full" ] = "true" ;
232
270
}
233
271
272
+ if ( query [ "metadata" ] == null )
273
+ {
274
+ query [ "metadata" ] = "true" ;
275
+ }
276
+
234
277
// Add all params to query string
235
278
if ( queryParams != null )
236
279
{
0 commit comments