diff --git a/src/ComDiadocApi.cs b/src/ComDiadocApi.cs index 82f474df..1feda8bf 100644 --- a/src/ComDiadocApi.cs +++ b/src/ComDiadocApi.cs @@ -4,6 +4,7 @@ using System.Security; using Diadoc.Api.Com; using Diadoc.Api.Cryptography; +using Diadoc.Api.Http; using Diadoc.Api.Proto; using Diadoc.Api.Proto.Certificates; using Diadoc.Api.Proto.Departments; @@ -583,7 +584,9 @@ public class ComDiadocApi : SafeComObject, IComDiadocApi public void Initialize(string apiClientId, string serverUrl) { - diadoc = new DiadocApi(apiClientId, serverUrl, new WinApiCrypt()); + var httpClient = new HttpClient(serverUrl); + httpClient.SetUserAgent(UserAgentBuilder.Build("COM")); + diadoc = new DiadocApi(new DiadocHttpApi(apiClientId, httpClient, new WinApiCrypt())); DocflowApi = new ComDocflowApi(diadoc.Docflow); } diff --git a/src/Http/HttpClient.cs b/src/Http/HttpClient.cs index 2b32f664..d189e250 100644 --- a/src/Http/HttpClient.cs +++ b/src/Http/HttpClient.cs @@ -38,6 +38,7 @@ public class HttpClient : IHttpClient private readonly string baseUrl; private NetworkCredential proxyCredential; private Uri proxyUri; + private string userAgentString; public HttpClient([NotNull] string baseUrl) { @@ -53,6 +54,7 @@ public HttpClient([NotNull] string baseUrl) this.baseUrl = baseUrl.EndsWith("/") ? baseUrl.Substring(0, baseUrl.Length - 1) : baseUrl; UseSystemProxy = true; + userAgentString = UserAgentBuilder.Build("C#"); } /// @@ -93,6 +95,11 @@ public void SetProxyCredentials([NotNull] string user, [NotNull] SecureString pa proxyCredential = new NetworkCredential(userDomain.User, SecureStringToString(password), userDomain.Domain); } + internal void SetUserAgent([NotNull] string userAgent) + { + userAgentString = userAgent ?? throw new ArgumentNullException(nameof(userAgent)); + } + [NotNull] private static string SecureStringToString([NotNull] SecureString secureString) { @@ -289,7 +296,7 @@ private WebRequest PrepareWebRequest([NotNull] HttpRequest request) webRequest.ContentLength = 0; } - webRequest.UserAgent = UserAgentString; + webRequest.UserAgent = userAgentString; return webRequest; } @@ -304,33 +311,5 @@ private void ProxifyWebRequest([NotNull] WebRequest request) request.Proxy.Credentials = proxyCredential; } } - - private static string UserAgentString { get; set; } - - static HttpClient() - { - string sdkVersion = typeof(HttpClient).Assembly.GetName().Version.ToString(); - string netFxVersion = Environment.Version.ToString(); - try - { - sdkVersion = FileVersionInfo.GetVersionInfo(typeof(HttpClient).Assembly.Location).FileVersion; - } - catch - { - } - - try - { - netFxVersion = FileVersionInfo.GetVersionInfo(typeof(int).Assembly.Location).FileVersion; - } - catch - { - } - - UserAgentString = string.Format("Diadoc C# SDK={0};OS={1};NETFX={2}", - sdkVersion, - Environment.OSVersion.VersionString, - netFxVersion); - } } } diff --git a/src/Http/UserAgentBuilder.cs b/src/Http/UserAgentBuilder.cs new file mode 100644 index 00000000..8e5d329d --- /dev/null +++ b/src/Http/UserAgentBuilder.cs @@ -0,0 +1,44 @@ +using System; +using System.Diagnostics; +using JetBrains.Annotations; + +namespace Diadoc.Api.Http +{ + internal static class UserAgentBuilder + { + private static readonly string SdkVersion; + private static readonly string NetFxVersion; + + static UserAgentBuilder() + { + SdkVersion = typeof(HttpClient).Assembly.GetName().Version.ToString(); + NetFxVersion = Environment.Version.ToString(); + try + { + SdkVersion = FileVersionInfo.GetVersionInfo(typeof(HttpClient).Assembly.Location).FileVersion; + } + catch + { + } + + try + { + NetFxVersion = FileVersionInfo.GetVersionInfo(typeof(int).Assembly.Location).FileVersion; + } + catch + { + } + } + + public static string Build([NotNull] string sdkName) + { + if (sdkName == null) + throw new ArgumentNullException(nameof(sdkName)); + return string.Format("Diadoc {0} SDK={1};OS={2};NETFX={3}", + sdkName, + SdkVersion, + Environment.OSVersion.VersionString, + NetFxVersion); + } + } +} diff --git a/tests/DiadocApi-Tests.csproj b/tests/DiadocApi-Tests.csproj index 6d27449c..966f632a 100644 --- a/tests/DiadocApi-Tests.csproj +++ b/tests/DiadocApi-Tests.csproj @@ -2,7 +2,7 @@ Diadoc.Api.Tests DiadocApi.Tests - net35;net40;net45;net461;net472;netcoreapp2.2 + net35;net40;net45;net461;net472;net5.0 false $(SolutionDir)\bin\$(Configuration)\$(AssemblyName)\