Skip to content

Commit 0bb3c21

Browse files
shestakovgGennadii Shestakov
andauthored
OSS-665 driver notification (#153)
OSS-665 * get server nuget version * get nuget current version and display message * get latest NuGet version and compare with current version Co-authored-by: Gennadii Shestakov <[email protected]>
1 parent bf8c304 commit 0bb3c21

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

FaunaDB.Client.Test/PageTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using System.Threading.Tasks;
4+
using FaunaDB.Client.Utils;
45
using FaunaDB.Query;
56
using FaunaDB.Types;
67
using NUnit.Framework;

FaunaDB.Client/Client/FaunaClient.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using FaunaDB.Query;
99
using FaunaDB.Types;
1010
using Newtonsoft.Json;
11+
using FaunaDB.Client.Utils;
1112

1213
namespace FaunaDB.Client
1314
{
@@ -32,13 +33,15 @@ public class FaunaClient
3233
/// <param name="endpoint">URL for the FaunaDB server. Defaults to "https://db.fauna.com:443"</param>
3334
/// <param name="timeout">Timeout for I/O operations. Defaults to 1 minute.</param>
3435
/// <param name="httpVersion">Version of http. Default value is HttpVersion.Version11, is you use .net core 3.0 and above you can enable http/2 support by passing HttpVersion.Version20</param>
36+
/// <param name="checkNewVersion">Check new NuGet package driver version. Default value is true (Check new version).</param>
3537
public FaunaClient(
3638
string secret,
3739
string endpoint = "https://db.fauna.com:443",
3840
TimeSpan? timeout = null,
3941
HttpClient httpClient = null,
40-
Version httpVersion = null)
41-
: this(CreateClient(secret, endpoint, timeout, httpClient, httpVersion))
42+
Version httpVersion = null,
43+
bool checkNewVersion = true)
44+
: this(CreateClient(secret, endpoint, timeout, httpClient, httpVersion, checkNewVersion))
4245
{ }
4346

4447
/// <summary>
@@ -245,10 +248,16 @@ static IClientIO CreateClient(
245248
string endpoint,
246249
TimeSpan? timeout = null,
247250
HttpClient httpClient = null,
248-
Version httpVersion = null)
251+
Version httpVersion = null,
252+
bool checkNewVersion = true)
249253
{
250254
secret.AssertNotNull(nameof(secret));
251255
endpoint.AssertNotNull(nameof(endpoint));
256+
257+
if (checkNewVersion && !CheckLatestVersion.AlreadyChecked)
258+
{
259+
Task.Run(() => CheckLatestVersion.GetVersionAsync());
260+
}
252261

253262
return new DefaultClientIO(
254263
secret: secret,
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Net.Http;
6+
using System.Reflection;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace FaunaDB.Client.Utils
11+
{
12+
public class CheckLatestVersion
13+
{
14+
private const string packageName = "FaunaDB.Client";
15+
public static bool AlreadyChecked { get; set; } = false;
16+
public static async Task GetVersionAsync()
17+
{
18+
if (AlreadyChecked) return;
19+
var latestNuGetVesrionString = string.Empty;
20+
var url = $"https://api.nuget.org/v3-flatcontainer/{packageName}/index.json";
21+
try
22+
{
23+
var httpClient = new HttpClient();
24+
#if NET45
25+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
26+
#endif
27+
var response = await httpClient.GetAsync(url);
28+
string versionsResponse = await response.Content.ReadAsStringAsync();
29+
Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(versionsResponse);
30+
var latestNuGetVesrion = ((Newtonsoft.Json.Linq.JArray)jObject.First.First).Children().LastOrDefault();
31+
latestNuGetVesrionString = latestNuGetVesrion.ToString();
32+
AlreadyChecked = true;
33+
}
34+
catch(Exception ex)
35+
{
36+
AlreadyChecked = true;
37+
var message = $"Enable to check new Fauna driver version. Exception: {ex.Message}";
38+
return;
39+
}
40+
41+
Assembly asm = typeof(CheckLatestVersion).GetTypeInfo().Assembly;
42+
var currentVersion = asm.GetName().Version;
43+
var currentVersionString = $"{currentVersion.Major}.{currentVersion.Minor}.{currentVersion.Build}";
44+
if (!latestNuGetVesrionString.Equals(currentVersionString))
45+
{
46+
var message = GetMessage(latestNuGetVesrionString, currentVersionString);
47+
Debug.WriteLine(message);
48+
System.Console.WriteLine(message);
49+
}
50+
}
51+
52+
private static string GetMessage(string latestNuGetVesrion, string currentVersion)
53+
{
54+
StringBuilder sb = new StringBuilder();
55+
sb.Append("".PadLeft(80, '='));
56+
sb.Append(System.Environment.NewLine);
57+
sb.Append(System.Environment.NewLine);
58+
sb.Append(System.Environment.NewLine);
59+
sb.Append($"New fauna version available {currentVersion} -> {latestNuGetVesrion}");
60+
sb.Append(System.Environment.NewLine);
61+
sb.Append($"Changelog: https://github.com/fauna/faunadb-csharp/blob/master/CHANGELOG.md");
62+
sb.Append(System.Environment.NewLine);
63+
sb.Append(System.Environment.NewLine);
64+
sb.Append(System.Environment.NewLine);
65+
sb.Append(System.Environment.NewLine);
66+
sb.Append("".PadLeft(80, '='));
67+
return sb.ToString();
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)