Document 406 response

- Remove ApiHeaders.ApplicationJson
This commit is contained in:
Jordan Brown
2020-07-04 14:28:31 -04:00
parent 6cc82b7bd1
commit ef7c1f3a47
5 changed files with 22 additions and 14 deletions
+4 -8
View File
@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Mime;
using System.Reflection;
using System.Text;
@@ -16,11 +17,6 @@ namespace Tgstation.Server.Api
/// </summary>
public sealed class ApiHeaders
{
/// <summary>
/// TODO: Remove this when we upgrade to .NET Standard 2.1
/// </summary>
public const string ApplicationJson = "application/json";
/// <summary>
/// The <see cref="ApiVersion"/> header key
/// </summary>
@@ -146,9 +142,9 @@ namespace Tgstation.Server.Api
if (requestHeaders == null)
throw new ArgumentNullException(nameof(requestHeaders));
var jsonAccept = new Microsoft.Net.Http.Headers.MediaTypeHeaderValue(ApplicationJson);
var jsonAccept = new Microsoft.Net.Http.Headers.MediaTypeHeaderValue(MediaTypeNames.Application.Json);
if (!requestHeaders.Accept.Any(x => x.MediaType == jsonAccept.MediaType))
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Client does not accept {0}!", ApplicationJson));
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Client does not accept {0}!", MediaTypeNames.Application.Json));
if (!requestHeaders.Headers.TryGetValue(HeaderNames.UserAgent, out var userAgentValues) || userAgentValues.Count == 0)
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Missing {0} headers!", HeaderNames.UserAgent));
@@ -264,7 +260,7 @@ namespace Tgstation.Server.Api
throw new InvalidOperationException("Specified instance ID in constructor and SetRequestHeaders!");
headers.Clear();
headers.Accept.Add(new MediaTypeWithQualityHeaderValue(ApplicationJson));
headers.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json));
if (IsTokenAuthentication)
headers.Authorization = new AuthenticationHeaderValue(JwtAuthenticationScheme, Token);
else
+5 -1
View File
@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@@ -157,7 +158,10 @@ namespace Tgstation.Server.Client
using (var request = new HttpRequestMessage(method, fullUri))
{
if (body != null)
request.Content = new StringContent(JsonConvert.SerializeObject(body, serializerSettings), Encoding.UTF8, ApiHeaders.ApplicationJson);
request.Content = new StringContent(
JsonConvert.SerializeObject(body, serializerSettings),
Encoding.UTF8,
MediaTypeNames.Application.Json);
var headersToUse = tokenRefresh ? tokenRefreshHeaders! : headers;
headersToUse.SetRequestHeaders(request.Headers, instanceId);
@@ -6,6 +6,7 @@ using Serilog.Context;
using System;
using System.Linq;
using System.Net;
using System.Net.Mime;
using System.Threading.Tasks;
using Tgstation.Server.Api;
using Tgstation.Server.Api.Models;
@@ -17,7 +18,7 @@ namespace Tgstation.Server.Host.Controllers
/// <summary>
/// A <see cref="Controller"/> for API functions
/// </summary>
[Produces(ApiHeaders.ApplicationJson)]
[Produces(MediaTypeNames.Application.Json)]
[ApiController]
public abstract class ApiController : Controller
{
@@ -5,9 +5,9 @@ using Newtonsoft.Json;
using Serilog.Context;
using System;
using System.Net;
using System.Net.Mime;
using System.Threading;
using System.Threading.Tasks;
using Tgstation.Server.Api;
using Tgstation.Server.Host.Components.Interop;
using Tgstation.Server.Host.Components.Interop.Bridge;
@@ -17,7 +17,7 @@ namespace Tgstation.Server.Host.Controllers
/// <see cref="Controller"/> for recieving DMAPI requests from DreamDaemon.
/// </summary>
[Route("Bridge")]
[Produces(ApiHeaders.ApplicationJson)]
[Produces(MediaTypeNames.Application.Json)]
public class BridgeController : Controller
{
/// <summary>
@@ -86,7 +86,7 @@ namespace Tgstation.Server.Host.Controllers
var responseJson = JsonConvert.SerializeObject(response, DMApiConstants.SerializerSettings);
logger.LogTrace("Bridge Response: {0}", responseJson);
return Content(responseJson, ApiHeaders.ApplicationJson);
return Content(responseJson, MediaTypeNames.Application.Json);
}
}
}
@@ -7,6 +7,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mime;
using Tgstation.Server.Api;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Api.Rights;
@@ -34,7 +35,7 @@ namespace Tgstation.Server.Host.Core
var errorMessageContent = new Dictionary<string, OpenApiMediaType>
{
{
ApiHeaders.ApplicationJson,
MediaTypeNames.Application.Json,
new OpenApiMediaType
{
Schema = new OpenApiSchema
@@ -90,6 +91,12 @@ namespace Tgstation.Server.Host.Core
Content = errorMessageContent
});
AddDefaultResponse(HttpStatusCode.NotAcceptable, new OpenApiResponse
{
Description = "Invalid Accept header, TGS requires `Accept: application/json`.",
Content = errorMessageContent
});
AddDefaultResponse(HttpStatusCode.InternalServerError, new OpenApiResponse
{
Description = ErrorCode.InternalServerError.Describe(),