private async Task <HttpAgentUpdateResultResponse> GetResult(PhotonServerDefinition server, string sessionId) { HttpClientEx client = null; try { var url = NetPath.Combine(server.Url, "api/agent/update/result"); client = HttpClientEx.Get(url, new { session = sessionId, }); await client.Send(); return(client.ParseJsonResponse <HttpAgentUpdateResultResponse>()); } catch (HttpStatusCodeException error) { if (error.HttpCode == HttpStatusCode.NotFound) { throw new ApplicationException($"Photon-Server instance '{server.Name}' not found!"); } throw; } finally { client?.Dispose(); } }
private async Task DownloadUpdate(HttpPackageIndex index) { if (!Directory.Exists(updateDirectory)) { Directory.CreateDirectory(updateDirectory); } try { var url = NetPath.Combine(Configuration.DownloadUrl, "CLI", index.Version, index.MsiFilename); using (var client = HttpClientEx.Get(url)) { await client.Send(); using (var fileStream = File.Open(updateFilename, FileMode.Create, FileAccess.Write)) using (var responseStream = client.ResponseBase.GetResponseStream()) { if (responseStream != null) { await responseStream.CopyToAsync(fileStream); } } } } catch (HttpStatusCodeException error) { throw new ApplicationException("Failed to download CLI update!", error); } }
private async Task <OutputData> UpdateOutput(PhotonServerDefinition server, string sessionId, int position) { HttpClientEx client = null; try { var url = NetPath.Combine(server.Url, "api/session/output"); client = HttpClientEx.Get(url, new { session = sessionId, start = position, }); await client.Send(); bool _complete; if (client.ResponseBase.StatusCode == HttpStatusCode.NotModified) { bool.TryParse(client.ResponseBase.Headers.Get("X-Complete"), out _complete); return(new OutputData { IsComplete = _complete }); } var result = new OutputData(); if (bool.TryParse(client.ResponseBase.Headers.Get("X-Complete"), out _complete)) { result.IsComplete = _complete; } if (int.TryParse(client.ResponseBase.Headers.Get("X-Text-Pos"), out var _textPos)) { result.NewLength = _textPos; } using (var responseStream = client.ResponseBase.GetResponseStream()) { if (responseStream == null) { return(result); } using (var reader = new StreamReader(responseStream)) { result.NewText = reader.ReadToEnd(); } } result.IsModified = true; return(result); } catch (HttpStatusCodeException error) { if (error.HttpCode == HttpStatusCode.NotFound) { throw new ApplicationException($"Photon-Server instance '{server.Name}' not found!"); } throw; } finally { client?.Dispose(); } }
private async Task BeginServerUpdate(PhotonServerDefinition server, HttpPackageIndex index) { ConsoleEx.Out.Write("Downloading Server update ", ConsoleColor.DarkCyan) .Write(index.Version, ConsoleColor.Cyan) .WriteLine("...", ConsoleColor.DarkCyan); string updateFilename; try { var url = NetPath.Combine(Configuration.DownloadUrl, "server", index.Version, index.MsiFilename); var updateDirectory = Path.Combine(Configuration.Directory, "Updates"); updateFilename = Path.Combine(updateDirectory, "Photon.Server.msi"); PathEx.CreatePath(updateDirectory); using (var client = HttpClientEx.Get(url)) { await client.Send(); using (var responseStream = client.ResponseBase.GetResponseStream()) { using (var fileStream = File.Open(updateFilename, FileMode.Create, FileAccess.Write)) if (responseStream != null) { await responseStream.CopyToAsync(fileStream); } } } ConsoleEx.Out.WriteLine("Download Complete.", ConsoleColor.DarkBlue); } catch (HttpStatusCodeException error) { if (error.HttpCode == HttpStatusCode.NotFound) { throw new ApplicationException($"Photon-Server instance '{server.Name}' not found!"); } throw; } ConsoleEx.Out.WriteLine("Uploading update to Server...", ConsoleColor.DarkCyan); try { var url = NetPath.Combine(server.Url, "api/server/update"); using (var client = HttpClientEx.Post(url)) { client.ContentType = "application/octet-stream"; client.BodyFunc = () => File.Open(updateFilename, FileMode.Open, FileAccess.Read); await client.Send(); if (client.ResponseBase.StatusCode == HttpStatusCode.BadRequest) { var text = await client.GetResponseTextAsync(); throw new ApplicationException($"Bad Update Request! {text}"); } } ConsoleEx.Out.WriteLine("Upload Complete.", ConsoleColor.DarkBlue); } catch (HttpStatusCodeException error) { if (error.HttpCode == HttpStatusCode.NotFound) { throw new ApplicationException($"Photon-Server instance '{server.Name}' not found!"); } throw; } }