Cleanup of some ApiController method calls

This commit is contained in:
Jordan Brown
2020-07-05 11:18:57 -04:00
parent f34827a761
commit 0631f80637
8 changed files with 45 additions and 18 deletions
@@ -119,7 +119,7 @@ namespace Tgstation.Server.Host.Controllers
Logger.LogWarning("Exceeded GitHub rate limit! Exception {0}", exception);
var secondsString = Math.Ceiling((exception.Reset - DateTimeOffset.Now).TotalSeconds).ToString(CultureInfo.InvariantCulture);
Response.Headers.Add("Retry-After", new StringValues(secondsString));
return StatusCode(429, new ErrorMessage(ErrorCode.GitHubApiRateLimit));
return StatusCode(HttpStatusCode.TooManyRequests, new ErrorMessage(ErrorCode.GitHubApiRateLimit));
}
/// <summary>
@@ -149,7 +149,7 @@ namespace Tgstation.Server.Host.Controllers
catch (ApiException e)
{
Logger.LogWarning(OctokitException, e);
return StatusCode((int)HttpStatusCode.FailedDependency);
return StatusCode(HttpStatusCode.FailedDependency);
}
releases = releases.Where(x => x.TagName.StartsWith(updatesConfiguration.GitTagPrefix, StringComparison.InvariantCulture));
@@ -243,7 +243,7 @@ namespace Tgstation.Server.Host.Controllers
catch (ApiException e)
{
Logger.LogWarning(OctokitException, e);
return StatusCode((int)HttpStatusCode.FailedDependency, new ErrorMessage(ErrorCode.GitHubApiError)
return StatusCode(HttpStatusCode.FailedDependency, new ErrorMessage(ErrorCode.GitHubApiError)
{
AdditionalData = e.Message
});
@@ -313,7 +313,7 @@ namespace Tgstation.Server.Host.Controllers
}
catch (InvalidOperationException)
{
return StatusCode((int)HttpStatusCode.ServiceUnavailable);
return StatusCode(HttpStatusCode.ServiceUnavailable);
}
}
@@ -86,19 +86,41 @@ namespace Tgstation.Server.Host.Controllers
/// Generic 410 response.
/// </summary>
/// <returns>An <see cref="ObjectResult"/> with <see cref="HttpStatusCode.Gone"/>.</returns>
protected ObjectResult Gone() => StatusCode((int)HttpStatusCode.Gone, new ErrorMessage(ErrorCode.ResourceNotPresent));
protected ObjectResult Gone() => StatusCode(HttpStatusCode.Gone, new ErrorMessage(ErrorCode.ResourceNotPresent));
/// <summary>
/// Generic 404 response.
/// </summary>
/// <returns>An <see cref="ObjectResult"/> with <see cref="HttpStatusCode.NotFound"/>.</returns>
protected new ObjectResult NotFound() => NotFound(new ErrorMessage(ErrorCode.ResourceNeverPresent));
protected new NotFoundObjectResult NotFound() => NotFound(new ErrorMessage(ErrorCode.ResourceNeverPresent));
/// <summary>
/// Generic 501 response.
/// </summary>
/// <returns>An <see cref="ObjectResult"/> with <see cref="HttpStatusCode.NotImplemented"/>.</returns>
protected ObjectResult RequiresPosixSystemIdentity() => StatusCode((int)HttpStatusCode.NotImplemented, new ErrorMessage(ErrorCode.RequiresPosixSystemIdentity));
protected ObjectResult RequiresPosixSystemIdentity() => StatusCode(HttpStatusCode.NotImplemented, new ErrorMessage(ErrorCode.RequiresPosixSystemIdentity));
/// <summary>
/// Strongly type calls to <see cref="ControllerBase.StatusCode(int)"/>.
/// </summary>
/// <param name="statusCode">The <see cref="HttpStatusCode"/>.</param>
/// <returns>A <see cref="StatusCodeResult"/> with the given <paramref name="statusCode"/>.</returns>
protected StatusCodeResult StatusCode(HttpStatusCode statusCode) => StatusCode((int)statusCode);
/// <summary>
/// Strongly type calls to <see cref="ControllerBase.StatusCode(int, object)"/>.
/// </summary>
/// <param name="statusCode">The <see cref="HttpStatusCode"/>.</param>
/// <param name="errorMessage">The accompanying <see cref="ErrorMessage"/> payload.</param>
/// <returns>A <see cref="StatusCodeResult"/> with the given <paramref name="statusCode"/>.</returns>
protected ObjectResult StatusCode(HttpStatusCode statusCode, object errorMessage) => StatusCode((int)statusCode, errorMessage);
/// <summary>
/// Generic 201 response with a given <paramref name="payload"/>.
/// </summary>
/// <param name="payload">The accompanying API payload.</param>
/// <returns>A <see cref="HttpStatusCode.Created"/> <see cref="ObjectResult"/> with the given <paramref name="payload"/>.</returns>
protected ObjectResult Created(object payload) => StatusCode((int)HttpStatusCode.Created, payload);
/// <summary>
/// Response for missing/Invalid headers.
@@ -123,7 +145,7 @@ namespace Tgstation.Server.Host.Controllers
};
if (headersException.MissingOrMalformedHeaders.HasFlag(HeaderTypes.Accept))
return StatusCode((int)HttpStatusCode.NotAcceptable, errorMessage);
return StatusCode(HttpStatusCode.NotAcceptable, errorMessage);
if (headersException.MissingOrMalformedHeaders == HeaderTypes.Authorization)
return Unauthorized(errorMessage);
@@ -152,7 +174,7 @@ namespace Tgstation.Server.Host.Controllers
if (!ApiHeaders.Compatible())
{
await StatusCode(
(int)HttpStatusCode.UpgradeRequired,
HttpStatusCode.UpgradeRequired,
new ErrorMessage(ErrorCode.ApiMismatch))
.ExecuteResultAsync(context)
.ConfigureAwait(false);
@@ -134,7 +134,7 @@ namespace Tgstation.Server.Host.Controllers
throw;
}
return StatusCode((int)HttpStatusCode.Created, dbModel.ToApi());
return StatusCode(HttpStatusCode.Created, dbModel.ToApi());
}
/// <summary>
@@ -105,7 +105,7 @@ namespace Tgstation.Server.Host.Controllers
newFile.Content = null;
return model.LastReadHash == null ? (IActionResult)StatusCode((int)HttpStatusCode.Created, newFile) : Json(newFile);
return model.LastReadHash == null ? (IActionResult)Created(newFile) : Json(newFile);
}
catch(IOException e)
{
@@ -228,7 +228,13 @@ namespace Tgstation.Server.Host.Controllers
try
{
model.IsDirectory = true;
return await instanceManager.GetInstance(Instance).Configuration.CreateDirectory(model.Path, systemIdentity, cancellationToken).ConfigureAwait(false) ? (IActionResult)Json(model) : StatusCode((int)HttpStatusCode.Created, model);
return await instanceManager
.GetInstance(Instance)
.Configuration
.CreateDirectory(model.Path, systemIdentity, cancellationToken)
.ConfigureAwait(false)
? (IActionResult)Json(model)
: Created(model);
}
catch (IOException e)
{
@@ -311,7 +311,7 @@ namespace Tgstation.Server.Host.Controllers
Logger.LogInformation("{0} {1} instance {2}: {3} ({4})", AuthenticationContext.User.Name, attached ? "attached" : "created", newInstance.Name, newInstance.Id, newInstance.Path);
var api = newInstance.ToApi();
return attached ? (IActionResult)Json(api) : StatusCode((int)HttpStatusCode.Created, api);
return attached ? (IActionResult)Json(api) : Created(api);
}
/// <summary>
@@ -4,7 +4,6 @@ using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Tgstation.Server.Api;
@@ -87,7 +86,7 @@ namespace Tgstation.Server.Host.Controllers
DatabaseContext.InstanceUsers.Add(dbUser);
await DatabaseContext.Save(cancellationToken).ConfigureAwait(false);
return StatusCode((int)HttpStatusCode.Created, dbUser.ToApi());
return Created(dbUser.ToApi());
}
/// <summary>
@@ -247,7 +247,7 @@ namespace Tgstation.Server.Host.Controllers
api.Reference = model.Reference;
api.ActiveJob = job.ToApi();
return StatusCode((int)HttpStatusCode.Created, api);
return Created(api);
}
/// <summary>
@@ -331,7 +331,7 @@ namespace Tgstation.Server.Host.Controllers
{
// user may have fucked with the repo manually, do what we can
await DatabaseContext.Save(cancellationToken).ConfigureAwait(false);
return StatusCode((int)HttpStatusCode.Created, api);
return Created(api);
}
return Json(api);
@@ -171,7 +171,7 @@ namespace Tgstation.Server.Host.Controllers
await DatabaseContext.Save(cancellationToken).ConfigureAwait(false);
return StatusCode((int)HttpStatusCode.Created, dbUser.ToApi(true));
return Created(dbUser.ToApi(true));
}
/// <summary>