/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>Success</returns> /// <exception cref="ApiException">A server side error occurred.</exception> public async System.Threading.Tasks.Task UpdateVoteOptionsForRoomAsync(HostVoteOptions body, System.Threading.CancellationToken cancellationToken) { if (body == null) { throw new System.ArgumentNullException("body"); } var urlBuilder_ = new System.Text.StringBuilder(); urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/Hosting/UpdateVoteOptionsForRoom"); var client_ = _httpClient; var disposeClient_ = false; try { using (var request_ = new System.Net.Http.HttpRequestMessage()) { var content_ = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(body, _settings.Value)); content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json"); request_.Content = content_; request_.Method = new System.Net.Http.HttpMethod("POST"); PrepareRequest(client_, request_, urlBuilder_); var url_ = urlBuilder_.ToString(); request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute); PrepareRequest(client_, request_, url_); var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); var disposeResponse_ = true; try { var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value); if (response_.Content != null && response_.Content.Headers != null) { foreach (var item_ in response_.Content.Headers) { headers_[item_.Key] = item_.Value; } } ProcessResponse(client_, response_); var status_ = (int)response_.StatusCode; if (status_ == 200) { return; } else { var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false); throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null); } } finally { if (disposeResponse_) { response_.Dispose(); } } } } finally { if (disposeClient_) { client_.Dispose(); } } }
private static void OnFileInChaosDirectoryChanged(object source, FileSystemEventArgs eventArgs) { if (!eventArgs.Name.Equals("voteoptions.txt")) { return; } try { var now = DateTime.Now; var timeSinceLastEventFired = (now - _lastRead).TotalMilliseconds; if (timeSinceLastEventFired < 500) { return; } Console.WriteLine("Vote options found by filewatcher, updating on voting server."); _lastRead = now; string voteOptionsText = ""; try { voteOptionsText = File.ReadAllText(eventArgs.FullPath); } catch (Exception) { Thread.Sleep(200); voteOptionsText = File.ReadAllText(eventArgs.FullPath); } var voteOptionsLines = voteOptionsText.Split(Environment.NewLine); var hostVoteOptions = new HostVoteOptions { RoomCode = _roomCode, VoteOptions = voteOptionsLines .Where(x => !string.IsNullOrWhiteSpace(x)) .Select(x => { var split = x.Split(','); return(new HostVoteOption { Description = string.Join(",", split.Skip(1)), ChaosId = split[0] }); }) .ToList() }; if (!hostVoteOptions.VoteOptions.Any()) { Console.WriteLine("Options are empty in file."); return; } chaosApiClient.UpdateVoteOptionsForRoomAsync(hostVoteOptions).Wait(); } catch (Exception exc) { Console.WriteLine($"ERROR in filewatch: {exc}"); } }
/// <returns>Success</returns> /// <exception cref="ApiException">A server side error occurred.</exception> public System.Threading.Tasks.Task UpdateVoteOptionsForRoomAsync(HostVoteOptions body) { return(UpdateVoteOptionsForRoomAsync(body, System.Threading.CancellationToken.None)); }