-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHttpUserAgentParserAccessor.cs
More file actions
43 lines (36 loc) · 1.43 KB
/
HttpUserAgentParserAccessor.cs
File metadata and controls
43 lines (36 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright © https://myCSharp.de - all rights reserved
using Microsoft.AspNetCore.Http;
using MyCSharp.HttpUserAgentParser.Providers;
namespace MyCSharp.HttpUserAgentParser.AspNetCore;
/// <summary>
/// User Agent parser accessor. Implements <see cref="IHttpContextAccessor.HttpContext"/>
/// </summary>
/// <remarks>
/// Creates a new instance of <see cref="HttpUserAgentParserAccessor"/>
/// </remarks>
public class HttpUserAgentParserAccessor(IHttpUserAgentParserProvider httpUserAgentParser)
: IHttpUserAgentParserAccessor
{
/// <summary>
/// The name of the Meter used for metrics.
/// </summary>
public const string MeterName = "MyCSharp.HttpUserAgentParser.AspNetCore";
private readonly IHttpUserAgentParserProvider _httpUserAgentParser = httpUserAgentParser;
/// <summary>
/// User agent of current <see cref="IHttpContextAccessor"/>
/// </summary>
public string? GetHttpContextUserAgent(HttpContext httpContext)
=> httpContext.GetUserAgentString();
/// <summary>
/// Returns current <see cref="HttpUserAgentInformation"/> of current <see cref="IHttpContextAccessor"/>
/// </summary>
public HttpUserAgentInformation? Get(HttpContext httpContext)
{
string? httpUserAgent = GetHttpContextUserAgent(httpContext);
if (string.IsNullOrEmpty(httpUserAgent))
{
return null;
}
return _httpUserAgentParser.Parse(httpUserAgent);
}
}