private async void SetVariable(string VarName, int Value) { string RestUrl = ApiUrl + @"variables/ByName/" + VarName; VarValue value = new VarValue(); value.Token = ApiToken; value.Value = Value; value.Name = VarName; value.Parent = String.Empty; // Serialize our concrete class into a JSON String var stringValue = JsonConvert.SerializeObject(value); var httpContent = new StringContent(stringValue, Encoding.UTF8, "application/json"); var httpResponse = await this.httpClient.PutAsync(RestUrl, httpContent); httpResponse.EnsureSuccessStatusCode(); // If the response contains content we want to read it! if (httpResponse.Content != null) { var responseContent = await httpResponse.Content.ReadAsStringAsync(); // From here on you could deserialize the ResponseContent back again to a concrete C# type using Json.Net } }
private async void SetVariable(string VarName, int Value) { if (string.IsNullOrEmpty(VarName)) { this.AppController.TrackAppEvent("Error: trying set room status for null or empty room reference"); return; } try { string RestUrl = ApiUrl + @"variables/ByName/" + VarName; this.AppController.TrackAppEvent("Http call " + RestUrl); VarValue value = new VarValue(); value.Token = ApiToken; value.Value = Value; value.Name = VarName; value.Parent = String.Empty; // Serialize our concrete class into a JSON String var stringValue = JsonConvert.SerializeObject(value); var httpContent = new StringContent(stringValue, Encoding.UTF8, "application/json"); await httpClient.PutAsync(RestUrl, httpContent) .ContinueWith(task => { if (task.Status != TaskStatus.RanToCompletion) { this.AppController.TrackAppEvent(String.Format("Error Http request {0} task status {1}", RestUrl, task.Status)); } else { var respMsg = task.Result; // If request faild - log this if (respMsg != null && !respMsg.IsSuccessStatusCode) { this.AppController.TrackAppEvent("Http req. " + RestUrl + " error: " + respMsg.StatusCode); } } }); } catch (Exception ex) { this.AppController.TrackAppException(ex); } }