-
Notifications
You must be signed in to change notification settings - Fork 693
/
Copy pathUserAgent.cs
48 lines (41 loc) · 1.47 KB
/
UserAgent.cs
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
44
45
46
47
48
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Net.Http;
using NuGet.Packaging;
namespace NuGet.Protocol.Core.Types
{
public static class UserAgent
{
static UserAgent()
{
// Set default user agent string
UserAgentString = new UserAgentStringBuilder().Build();
}
public static void SetUserAgentString(UserAgentStringBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
UserAgentString = builder.Build();
}
public static string UserAgentString { get; private set; }
/// <summary>
/// Set user agent string on HttpClient to the static string.
/// </summary>
/// <param name="client">Http client</param>
public static void SetUserAgent(HttpClient client)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}
if (!string.IsNullOrEmpty(UserAgentString))
{
client.DefaultRequestHeaders.Add("user-agent", UserAgentString);
client.DefaultRequestHeaders.Add("X-NuGet-Client-Version", MinClientVersionUtility.GetNuGetClientVersion().ToNormalizedString());
}
}
}
}