<commit_before>﻿#nullable enable

using System.Net.Http.Formatting;
using System.Web.Http;
using System.Web.Http.ExceptionHandling;
using Newtonsoft.Json.Serialization;
using Snittlistan.Web.Infrastructure;
using Snittlistan.Web.Infrastructure.Attributes;

namespace Snittlistan.Web;

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        _ = config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional });

        // camelCase by default
        JsonMediaTypeFormatter formatter = config.Formatters.JsonFormatter;
        formatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        config.Services.Add(typeof(IExceptionLogger), new LoggingExceptionLogger());

        config.Filters.Add(new UnhandledExceptionFilter());
        config.Filters.Add(new ValidateModelAttribute());
        config.Formatters.Add(new ICalFormatter());
        config.MessageHandlers.Add(new OutlookAgentMessageHandler());
    }
}<commit_msg>fixes #551<commit_after>﻿#nullable enable

using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Web.Http;
using System.Web.Http.ExceptionHandling;
using Newtonsoft.Json.Serialization;
using Snittlistan.Web.Infrastructure;
using Snittlistan.Web.Infrastructure.Attributes;

namespace Snittlistan.Web;

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        _ = config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional });

        // camelCase by default
        JsonMediaTypeFormatter formatter = config.Formatters.JsonFormatter;
        formatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        config.Services.Add(typeof(IExceptionLogger), new LoggingExceptionLogger());

        config.Filters.Add(new UnhandledExceptionFilter());
        config.Filters.Add(new ValidateModelAttribute());
        config.Formatters.Add(new ICalFormatter());
        config.MessageHandlers.Add(new OutlookAgentMessageHandler());
        config.MessageHandlers.Add(new CanceledTaskMessageHandler());
    }

    private class CanceledTaskMessageHandler : DelegatingHandler
    {
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
            return cancellationToken.IsCancellationRequested
                ? new HttpResponseMessage(HttpStatusCode.InternalServerError)
                : response;
        }
    }
}
{'fraction_non_alphanumeric': 0.08912280701754385, 'fraction_numerical': 0.0010526315789473684, 'mean_word_length': 3.7437603993344424, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1775588', 'n_tokens_mistral': 724, 'n_tokens_neox': 720, 'n_words': 137}