Skip to content

Update userAgent of autogen cmdlet #15939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/Accounts/Accounts/CommonModule/UserAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.WindowsAzure.Commands.Utilities.Common;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Management.Automation;
using System.Net.Http;
using Microsoft.Azure.Commands.Common.Authentication;
using System.Net.Http.Headers;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.Common
{
Expand All @@ -30,16 +31,18 @@ namespace Microsoft.Azure.Commands.Common
/// </summary>
public class UserAgent
{
Version _version;
private ProductInfoHeaderValue[] _userAgents;

public UserAgent(InvocationInfo invocation)
: this(invocation?.MyCommand?.Module?.Version ?? new Version("1.0.0"))
{
}

public UserAgent(Version moduleVersion)
{
_version = moduleVersion;
List<ProductInfoHeaderValue> list = new List<ProductInfoHeaderValue>();
string azVersion = (String.IsNullOrWhiteSpace(AzurePSCmdlet.AzVersion)) ? "0.0.0" : AzurePSCmdlet.AzVersion;
list.Add(new ProductInfoHeaderValue("AzurePowershell", $"v{azVersion}"));
if(!String.IsNullOrWhiteSpace(AzurePSCmdlet.PowerShellVersion))
{
list.Add(new ProductInfoHeaderValue("PSVersion", $"v{AzurePSCmdlet.PowerShellVersion}"));
}
_userAgents = list.ToArray();
}

/// <summary>
Expand All @@ -53,10 +56,7 @@ public UserAgent(Version moduleVersion)
/// <returns>Amended pipeline for retrieving a response</returns>
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token, Action cancel, SignalDelegate signal, NextDelegate next)
{
var userAgents = new ProductInfoHeaderValue[] { new ProductInfoHeaderValue("AzurePowershell", $"Az4.0.0-preview") };
// add user agent headers

foreach (var userAgent in userAgents)
foreach (var userAgent in _userAgents)
{
request.Headers.UserAgent.Add(userAgent);
}
Expand Down