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(); } }
protected async Task <T> WebClientEx <T>(PhotonServerDefinition server, Action <HttpClientEx> requestAction, Func <HttpClientEx, T> responseAction) { HttpClientEx client = null; try { client = new HttpClientEx { Method = "GET", Username = Username, Password = Password, }; requestAction?.Invoke(client); await client.Send(); //if (client.ResponseBase.StatusCode == HttpStatusCode.Unauthorized) // throw new ApplicationException("Access not authorized!"); if (client.ResponseBase.StatusCode == HttpStatusCode.BadRequest) { var text = await client.GetResponseTextAsync(); throw new ApplicationException($"Bad Update Request! {text}"); } if (responseAction != null) { return(responseAction.Invoke(client)); } return(default(T)); } catch (HttpStatusCodeException error) { if (error.HttpCode == HttpStatusCode.NotFound) { throw new ApplicationException($"Photon-Server instance '{server.Name}' not found!"); } if (error.HttpCode == HttpStatusCode.Unauthorized) { throw new HttpUnauthorizedException(); } throw; } finally { client?.Dispose(); } }
private async Task <HttpBuildStartResponse> StartSession(PhotonServerDefinition server, HttpBuildStartRequest request) { HttpClientEx client = null; MemoryStream requestStream = null; try { requestStream = new MemoryStream(); JsonSettings.Serializer.Serialize(requestStream, request, true); var url = NetPath.Combine(server.Url, "api/build/start"); client = HttpClientEx.Post(url, new { refspec = GitRefspec, }); requestStream.Seek(0, SeekOrigin.Begin); client.Body = requestStream; await client.Send(); if (client.ResponseBase.StatusCode == HttpStatusCode.BadRequest) { var text = await client.GetResponseTextAsync(); throw new ApplicationException($"Bad Build Request! {text}"); } return(client.ParseJsonResponse <HttpBuildStartResponse>()); } catch (HttpStatusCodeException error) { if (error.HttpCode == HttpStatusCode.NotFound) { throw new ApplicationException($"Photon-Server instance '{server.Name}' not found!"); } throw; } finally { requestStream?.Dispose(); client?.Dispose(); } }
private async Task <HttpAgentUpdateStartResponse> StartSession(PhotonServerDefinition server, string[] agentIds, string updateFilename) { HttpClientEx client = null; try { var url = NetPath.Combine(server.Url, "api/agent/update/start"); client = HttpClientEx.Post(url); client.Query = new { agents = string.Join(";", agentIds), }; 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}"); } return(client.ParseJsonResponse <HttpAgentUpdateStartResponse>()); } 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 <HttpDeployStartResponse> StartSession(PhotonServerDefinition server) { HttpClientEx client = null; try { var url = NetPath.Combine(server.Url, "api/deploy/start"); client = HttpClientEx.Post(url, new { project = ProjectId, package = ProjectPackageId, version = ProjectPackageVersion, env = Environment, }); await client.Send(); if (client.ResponseBase.StatusCode == HttpStatusCode.BadRequest) { var text = await client.GetResponseTextAsync(); throw new ApplicationException($"Bad Deploy Request! {text}"); } return(client.ParseJsonResponse <HttpDeployStartResponse>()); } 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 <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(); } }