namespace PrivaPub.Infrastructure.Http { public static class HttpScope { public const string Request = "request"; sealed record Tags(string Purpose, string Trigger, bool Crawl); static readonly AsyncLocal current = new(); public static string Purpose => current.Value?.Purpose; public static string Trigger => current.Value?.Trigger ?? Request; public static bool Crawling => current.Value?.Crawl == true; public static IDisposable For(string purpose) => Push(tags => tags with { Purpose = purpose }); public static IDisposable Default(string purpose) => Push(tags => tags with { Purpose = tags.Purpose ?? purpose }); public static IDisposable Triggered(string trigger) => Push(tags => tags with { Trigger = trigger }); public static IDisposable Crawl() => Push(tags => tags with { Crawl = true }); static IDisposable Push(Func change) { var previous = current.Value; current.Value = change(previous ?? new Tags(default, default, false)); return new Restore(previous); } sealed class Restore : IDisposable { readonly Tags _previous; public Restore(Tags previous) => _previous = previous; public void Dispose() => current.Value = _previous; } } }