Refactor API key auth handling with switch and error check

Refactored the "API Key" authentication logic to use a switch
statement on the API key location, improving code clarity.
Added a default case to throw a DataIntegrityException for
unsupported API key locations, enhancing error handling.
This commit is contained in:
2025-12-12 13:21:09 +01:00
parent 3b77345aee
commit 84e403f411

View File

@@ -64,17 +64,24 @@ public class InvokeRecActionCommandHandler(
case "API Key": case "API Key":
if (action.EndpointAuthApiKey is string apiKey && action.EndpointAuthApiValue is string apiValue) if (action.EndpointAuthApiKey is string apiKey && action.EndpointAuthApiValue is string apiValue)
{ {
if (action.EndpointAuthApiKeyAddTo == ApiKeyLocation.Header) switch (action.EndpointAuthApiKeyAddTo)
{ {
httpReq.Headers.Add(apiKey, apiValue); case ApiKeyLocation.Header:
} httpReq.Headers.Add(apiKey, apiValue);
else // Defaults to Query String break;
{ case ApiKeyLocation.Query:
var uriBuilder = new UriBuilder(httpReq.RequestUri!); var uriBuilder = new UriBuilder(httpReq.RequestUri!);
var query = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query); var query = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query);
query[apiKey] = apiValue; query[apiKey] = apiValue;
uriBuilder.Query = query.ToString(); uriBuilder.Query = query.ToString();
httpReq.RequestUri = uriBuilder.Uri; httpReq.RequestUri = uriBuilder.Uri;
break;
default:
throw new DataIntegrityException(
$"The API key location '{action.EndpointAuthApiKeyAddTo}' is not supported. " +
$"ProfileId: {action.ProfileId}, " +
$"Id: {action.Id}"
);
} }
} }
break; break;