From f0006a42002e03edb080e629b2ce9f4dde410739 Mon Sep 17 00:00:00 2001 From: Artem Azaraev Date: Tue, 2 Sep 2025 16:25:50 +0300 Subject: [PATCH 01/14] Extract method to get an api URL --- DevProxy.Abstractions/Data/MSGraphDb.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DevProxy.Abstractions/Data/MSGraphDb.cs b/DevProxy.Abstractions/Data/MSGraphDb.cs index e28ccc09..5991c5c7 100644 --- a/DevProxy.Abstractions/Data/MSGraphDb.cs +++ b/DevProxy.Abstractions/Data/MSGraphDb.cs @@ -23,6 +23,8 @@ public sealed class MSGraphDb(HttpClient httpClient, ILogger logger) // v1 refers to v1 of the db schema, not the graph version public static string MSGraphDbFilePath => Path.Combine(ProxyUtils.AppFolder!, "msgraph-openapi-v1.db"); + private static string GetOpenApiSpecUrl(string version) => $"https://raw.githubusercontent.com/microsoftgraph/msgraph-metadata/master/openapi/{version}/openapi.yaml"; + public SqliteConnection Connection { get @@ -177,7 +179,7 @@ private async Task UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, Cancel continue; } - var url = $"https://raw.githubusercontent.com/microsoftgraph/msgraph-metadata/master/openapi/{version}/openapi.yaml"; + var url = GetOpenApiSpecUrl(version); _logger.LogInformation("Downloading OpenAPI file from {Url}...", url); var response = await _httpClient.GetStringAsync(url, cancellationToken); From a4cd1bcc4e821d7aa62c33a6144ac0ccae7d13b6 Mon Sep 17 00:00:00 2001 From: Artem Azaraev Date: Tue, 2 Sep 2025 16:31:52 +0300 Subject: [PATCH 02/14] Arrange functions to get yaml and e-tag open api filenames --- DevProxy.Abstractions/Data/MSGraphDb.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/DevProxy.Abstractions/Data/MSGraphDb.cs b/DevProxy.Abstractions/Data/MSGraphDb.cs index 5991c5c7..4535eac7 100644 --- a/DevProxy.Abstractions/Data/MSGraphDb.cs +++ b/DevProxy.Abstractions/Data/MSGraphDb.cs @@ -25,6 +25,12 @@ public sealed class MSGraphDb(HttpClient httpClient, ILogger logger) private static string GetOpenApiSpecUrl(string version) => $"https://raw.githubusercontent.com/microsoftgraph/msgraph-metadata/master/openapi/{version}/openapi.yaml"; + private static string GetBaseGraphOpenApiFileName(string version) => $"graph-{version.Replace(".", "_", StringComparison.OrdinalIgnoreCase)}-openapi"; + + private static string GetGraphOpenApiYamlFileName(string version) => $"{GetBaseGraphOpenApiFileName(version)}.yaml"; + + private static string GetGraphOpenApiEtagFileName(string version) => $"{GetBaseGraphOpenApiFileName(version)}.etag.txt"; + public SqliteConnection Connection { get @@ -84,8 +90,6 @@ public async Task GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke } - private static string GetGraphOpenApiYamlFileName(string version) => $"graph-{version.Replace(".", "_", StringComparison.OrdinalIgnoreCase)}-openapi.yaml"; - private async Task CreateDbAsync(CancellationToken cancellationToken) { _logger.LogInformation("Creating database..."); From 2e1182be9cf83f68214205c07c74c01fad56a990 Mon Sep 17 00:00:00 2001 From: Artem Azaraev Date: Tue, 2 Sep 2025 16:38:30 +0300 Subject: [PATCH 03/14] Correct some logging messages --- DevProxy.Abstractions/Data/MSGraphDb.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DevProxy.Abstractions/Data/MSGraphDb.cs b/DevProxy.Abstractions/Data/MSGraphDb.cs index 4535eac7..5ee386ce 100644 --- a/DevProxy.Abstractions/Data/MSGraphDb.cs +++ b/DevProxy.Abstractions/Data/MSGraphDb.cs @@ -60,7 +60,7 @@ public async Task GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke var modifiedToday = dbFileInfo.Exists && dbFileInfo.LastWriteTime.Date == DateTime.Now.Date; if (modifiedToday && skipIfUpdatedToday) { - _logger.LogInformation("Microsoft Graph database already updated today"); + _logger.LogInformation("Microsoft Graph database has already been updated today"); return 1; } @@ -78,7 +78,7 @@ public async Task GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke await FillDataAsync(cancellationToken); SetDbJournaling(true); - _logger.LogInformation("Microsoft Graph database successfully updated"); + _logger.LogInformation("Microsoft Graph database is successfully updated"); return 0; } @@ -179,7 +179,7 @@ private async Task UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, Cancel _logger.LogDebug("Checking for updated OpenAPI file {File}...", file); if (file.Exists && file.LastWriteTime.Date == DateTime.Now.Date) { - _logger.LogInformation("File {File} already updated today", file); + _logger.LogInformation("File {File} has already been updated today", file); continue; } From 62f4bf0c381f11f2e7d01f94d5b9378f67fade2a Mon Sep 17 00:00:00 2001 From: Artem Azaraev Date: Tue, 2 Sep 2025 16:57:47 +0300 Subject: [PATCH 04/14] Move SetDbJournaling() --- DevProxy.Abstractions/Data/MSGraphDb.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/DevProxy.Abstractions/Data/MSGraphDb.cs b/DevProxy.Abstractions/Data/MSGraphDb.cs index 5ee386ce..a7b61f58 100644 --- a/DevProxy.Abstractions/Data/MSGraphDb.cs +++ b/DevProxy.Abstractions/Data/MSGraphDb.cs @@ -74,9 +74,7 @@ public async Task GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke await CreateDbAsync(cancellationToken); - SetDbJournaling(false); await FillDataAsync(cancellationToken); - SetDbJournaling(true); _logger.LogInformation("Microsoft Graph database is successfully updated"); @@ -116,6 +114,8 @@ private async Task FillDataAsync(CancellationToken cancellationToken) { _logger.LogInformation("Filling database..."); + SetDbJournaling(false); + await using var transaction = await Connection.BeginTransactionAsync(cancellationToken); var i = 0; @@ -164,6 +164,8 @@ private async Task FillDataAsync(CancellationToken cancellationToken) await transaction.CommitAsync(cancellationToken); + SetDbJournaling(true); + _logger.LogInformation("Inserted {EndpointCount} endpoints in the database", i); } From d1faa4d5cb17ba0f474e45472a9ad5803ce0d973 Mon Sep 17 00:00:00 2001 From: Artem Azaraev Date: Tue, 2 Sep 2025 17:03:57 +0300 Subject: [PATCH 05/14] Add second check if db should be updated --- DevProxy.Abstractions/Data/MSGraphDb.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/DevProxy.Abstractions/Data/MSGraphDb.cs b/DevProxy.Abstractions/Data/MSGraphDb.cs index a7b61f58..95e9f4a0 100644 --- a/DevProxy.Abstractions/Data/MSGraphDb.cs +++ b/DevProxy.Abstractions/Data/MSGraphDb.cs @@ -64,7 +64,15 @@ public async Task GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke return 1; } - await UpdateOpenAPIGraphFilesIfNecessaryAsync(appFolder, cancellationToken); + var isApiModified = await UpdateOpenAPIGraphFilesIfNecessaryAsync(appFolder, cancellationToken); + + if (!isApiModified) + { + UpdateLastWriteTime(dbFileInfo); + _logger.LogInformation("Microsoft Graph database is already updated"); + return 1; + } + await LoadOpenAPIFilesAsync(appFolder, cancellationToken); if (_openApiDocuments.Count < 1) { @@ -88,6 +96,8 @@ public async Task GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke } + private static void UpdateLastWriteTime(FileInfo fileInfo) => fileInfo.LastWriteTime = DateTime.Now; + private async Task CreateDbAsync(CancellationToken cancellationToken) { _logger.LogInformation("Creating database..."); @@ -169,10 +179,12 @@ private async Task FillDataAsync(CancellationToken cancellationToken) _logger.LogInformation("Inserted {EndpointCount} endpoints in the database", i); } - private async Task UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, CancellationToken cancellationToken) + private async Task UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, CancellationToken cancellationToken) { _logger.LogInformation("Checking for updated OpenAPI files..."); + var isApiUpdated = false; + foreach (var version in graphVersions) { try @@ -192,12 +204,15 @@ private async Task UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, Cancel await File.WriteAllTextAsync(file.FullName, response, cancellationToken); _logger.LogDebug("Downloaded OpenAPI file from {Url} to {File}", url, file); + + isApiUpdated = true; } catch (Exception ex) { _logger.LogError(ex, "Error updating OpenAPI files"); } } + return isApiUpdated; } private async Task LoadOpenAPIFilesAsync(string folder, CancellationToken cancellationToken) From d9e9973e6dcecdb2366679a6218e5be16e3433f1 Mon Sep 17 00:00:00 2001 From: Artem Azaraev Date: Tue, 2 Sep 2025 17:13:06 +0300 Subject: [PATCH 06/14] Extract IsModifiedToday function --- DevProxy.Abstractions/Data/MSGraphDb.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/DevProxy.Abstractions/Data/MSGraphDb.cs b/DevProxy.Abstractions/Data/MSGraphDb.cs index 95e9f4a0..c0ef9510 100644 --- a/DevProxy.Abstractions/Data/MSGraphDb.cs +++ b/DevProxy.Abstractions/Data/MSGraphDb.cs @@ -57,7 +57,7 @@ public async Task GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke try { var dbFileInfo = new FileInfo(MSGraphDbFilePath); - var modifiedToday = dbFileInfo.Exists && dbFileInfo.LastWriteTime.Date == DateTime.Now.Date; + var modifiedToday = IsModifiedToday(dbFileInfo); if (modifiedToday && skipIfUpdatedToday) { _logger.LogInformation("Microsoft Graph database has already been updated today"); @@ -96,6 +96,8 @@ public async Task GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke } + private static bool IsModifiedToday(FileInfo fileInfo) => fileInfo.Exists && fileInfo.LastWriteTime.Date == DateTime.Now.Date; + private static void UpdateLastWriteTime(FileInfo fileInfo) => fileInfo.LastWriteTime = DateTime.Now; private async Task CreateDbAsync(CancellationToken cancellationToken) @@ -191,7 +193,7 @@ private async Task UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, { var file = new FileInfo(Path.Combine(folder, GetGraphOpenApiYamlFileName(version))); _logger.LogDebug("Checking for updated OpenAPI file {File}...", file); - if (file.Exists && file.LastWriteTime.Date == DateTime.Now.Date) + if (IsModifiedToday(file)) { _logger.LogInformation("File {File} has already been updated today", file); continue; From 590102a188ee4ebc6c0fbca3dd3d782d967579d1 Mon Sep 17 00:00:00 2001 From: Artem Azaraev Date: Tue, 2 Sep 2025 17:18:48 +0300 Subject: [PATCH 07/14] Extract DownloadOpenAPIFileAsync() --- DevProxy.Abstractions/Data/MSGraphDb.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/DevProxy.Abstractions/Data/MSGraphDb.cs b/DevProxy.Abstractions/Data/MSGraphDb.cs index c0ef9510..974fb153 100644 --- a/DevProxy.Abstractions/Data/MSGraphDb.cs +++ b/DevProxy.Abstractions/Data/MSGraphDb.cs @@ -202,8 +202,7 @@ private async Task UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, var url = GetOpenApiSpecUrl(version); _logger.LogInformation("Downloading OpenAPI file from {Url}...", url); - var response = await _httpClient.GetStringAsync(url, cancellationToken); - await File.WriteAllTextAsync(file.FullName, response, cancellationToken); + await DownloadOpenAPIFileAsync(file, url, cancellationToken); _logger.LogDebug("Downloaded OpenAPI file from {Url} to {File}", url, file); @@ -217,6 +216,12 @@ private async Task UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, return isApiUpdated; } + private async Task DownloadOpenAPIFileAsync(FileInfo file, string url, CancellationToken cancellationToken) + { + var response = await _httpClient.GetStringAsync(url, cancellationToken); + await File.WriteAllTextAsync(file.FullName, response, cancellationToken); + } + private async Task LoadOpenAPIFilesAsync(string folder, CancellationToken cancellationToken) { _logger.LogInformation("Loading OpenAPI files..."); From eb0576c32abea8ba36239e20e5ac5731bd4414b5 Mon Sep 17 00:00:00 2001 From: Artem Azaraev Date: Wed, 3 Sep 2025 12:02:35 +0300 Subject: [PATCH 08/14] Add conditional downloading by an e-tag --- DevProxy.Abstractions/Data/MSGraphDb.cs | 59 ++++++++++++++++++++----- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/DevProxy.Abstractions/Data/MSGraphDb.cs b/DevProxy.Abstractions/Data/MSGraphDb.cs index 974fb153..694b06b2 100644 --- a/DevProxy.Abstractions/Data/MSGraphDb.cs +++ b/DevProxy.Abstractions/Data/MSGraphDb.cs @@ -7,6 +7,8 @@ using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Readers; +using System.Net; +using System.Net.Http.Headers; namespace DevProxy.Abstractions.Data; @@ -191,22 +193,21 @@ private async Task UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, { try { - var file = new FileInfo(Path.Combine(folder, GetGraphOpenApiYamlFileName(version))); - _logger.LogDebug("Checking for updated OpenAPI file {File}...", file); - if (IsModifiedToday(file)) + var yamlFile = new FileInfo(Path.Combine(folder, GetGraphOpenApiYamlFileName(version))); + _logger.LogDebug("Checking for updated OpenAPI file {File}...", yamlFile); + if (IsModifiedToday(yamlFile)) { - _logger.LogInformation("File {File} has already been updated today", file); + _logger.LogInformation("File {File} has already been updated today", yamlFile); continue; } var url = GetOpenApiSpecUrl(version); _logger.LogInformation("Downloading OpenAPI file from {Url}...", url); - await DownloadOpenAPIFileAsync(file, url, cancellationToken); + var etagFile = new FileInfo(Path.Combine(folder, GetGraphOpenApiEtagFileName(version))); + isApiUpdated |= await DownloadOpenAPIFileAsync(url, yamlFile, etagFile, cancellationToken); - _logger.LogDebug("Downloaded OpenAPI file from {Url} to {File}", url, file); - - isApiUpdated = true; + _logger.LogDebug("Downloaded OpenAPI file from {Url} to {File}", url, yamlFile); } catch (Exception ex) { @@ -216,10 +217,46 @@ private async Task UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, return isApiUpdated; } - private async Task DownloadOpenAPIFileAsync(FileInfo file, string url, CancellationToken cancellationToken) + private async Task DownloadOpenAPIFileAsync(string url, FileInfo yamlFile, FileInfo etagFile, CancellationToken cancellationToken) { - var response = await _httpClient.GetStringAsync(url, cancellationToken); - await File.WriteAllTextAsync(file.FullName, response, cancellationToken); + var tag = string.Empty; + if (etagFile.Exists) + { + tag = await File.ReadAllTextAsync(etagFile.FullName, cancellationToken); + } + + using var requestMessage = new HttpRequestMessage(HttpMethod.Get, url); + if (!string.IsNullOrWhiteSpace(tag)) + { + requestMessage.Headers.IfNoneMatch.Add(new EntityTagHeaderValue(tag)); + } + + var response = await _httpClient.SendAsync(requestMessage, cancellationToken); + + if (response.StatusCode == HttpStatusCode.NotModified) + { + UpdateLastWriteTime(yamlFile); + return false; + } + + //save the new OpenAPI spec + var _ = response.EnsureSuccessStatusCode(); + await using var contentStream = await response.Content.ReadAsStreamAsync(cancellationToken); + await using var fileStream = new FileStream(yamlFile.FullName, + new FileStreamOptions { Mode = FileMode.Create, Access = FileAccess.Write, Share = FileShare.None } + ); + await contentStream.CopyToAsync(fileStream, cancellationToken); + + if (response.Headers.ETag != null) + { + await File.WriteAllTextAsync(etagFile.FullName, response.Headers.ETag.Tag, cancellationToken); + } + else + { + etagFile.Delete(); + } + + return true; } private async Task LoadOpenAPIFilesAsync(string folder, CancellationToken cancellationToken) From 783713e48956e30c48976759ac51b783117aa78b Mon Sep 17 00:00:00 2001 From: Artem Azaraev Date: Wed, 3 Sep 2025 13:13:17 +0300 Subject: [PATCH 09/14] Add debug logging for last-write-time attr update --- DevProxy.Abstractions/Data/MSGraphDb.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DevProxy.Abstractions/Data/MSGraphDb.cs b/DevProxy.Abstractions/Data/MSGraphDb.cs index 694b06b2..8f385c3d 100644 --- a/DevProxy.Abstractions/Data/MSGraphDb.cs +++ b/DevProxy.Abstractions/Data/MSGraphDb.cs @@ -71,6 +71,7 @@ public async Task GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke if (!isApiModified) { UpdateLastWriteTime(dbFileInfo); + _logger.LogDebug("Updated the last-write-time of Microsoft Graph database {File}", dbFileInfo); _logger.LogInformation("Microsoft Graph database is already updated"); return 1; } @@ -236,6 +237,7 @@ private async Task DownloadOpenAPIFileAsync(string url, FileInfo yamlFile, if (response.StatusCode == HttpStatusCode.NotModified) { UpdateLastWriteTime(yamlFile); + _logger.LogDebug("Updated the last-write-time of OpenAPI file {File}", yamlFile); return false; } From 69bf886ecb6ebb339b56b8a0e4df0c853b1e5919 Mon Sep 17 00:00:00 2001 From: Artem Azaraev Date: Wed, 3 Sep 2025 13:16:08 +0300 Subject: [PATCH 10/14] Prevent db generation if a spec update fails --- DevProxy.Abstractions/Data/MSGraphDb.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/DevProxy.Abstractions/Data/MSGraphDb.cs b/DevProxy.Abstractions/Data/MSGraphDb.cs index 8f385c3d..99ed2b3c 100644 --- a/DevProxy.Abstractions/Data/MSGraphDb.cs +++ b/DevProxy.Abstractions/Data/MSGraphDb.cs @@ -66,12 +66,18 @@ public async Task GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke return 1; } - var isApiModified = await UpdateOpenAPIGraphFilesIfNecessaryAsync(appFolder, cancellationToken); + var (isApiModified, errorCount) = await UpdateOpenAPIGraphFilesIfNecessaryAsync(appFolder, cancellationToken); + + if (errorCount > 0) + { + _logger.LogWarning("Unable to generate Microsoft Graph database"); + return 1; + } if (!isApiModified) { UpdateLastWriteTime(dbFileInfo); - _logger.LogDebug("Updated the last-write-time of Microsoft Graph database {File}", dbFileInfo); + _logger.LogDebug("Updated the last-write-time attribute of Microsoft Graph database {File}", dbFileInfo); _logger.LogInformation("Microsoft Graph database is already updated"); return 1; } @@ -184,11 +190,12 @@ private async Task FillDataAsync(CancellationToken cancellationToken) _logger.LogInformation("Inserted {EndpointCount} endpoints in the database", i); } - private async Task UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, CancellationToken cancellationToken) + private async Task<(bool isApiUpdated, int errors)> UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, CancellationToken cancellationToken) { _logger.LogInformation("Checking for updated OpenAPI files..."); var isApiUpdated = false; + var errorCount = 0; foreach (var version in graphVersions) { @@ -212,10 +219,11 @@ private async Task UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, } catch (Exception ex) { + errorCount++; _logger.LogError(ex, "Error updating OpenAPI files"); } } - return isApiUpdated; + return (isApiUpdated, errorCount); } private async Task DownloadOpenAPIFileAsync(string url, FileInfo yamlFile, FileInfo etagFile, CancellationToken cancellationToken) @@ -237,7 +245,7 @@ private async Task DownloadOpenAPIFileAsync(string url, FileInfo yamlFile, if (response.StatusCode == HttpStatusCode.NotModified) { UpdateLastWriteTime(yamlFile); - _logger.LogDebug("Updated the last-write-time of OpenAPI file {File}", yamlFile); + _logger.LogDebug("Updated the last-write-time attribute of OpenAPI file {File}", yamlFile); return false; } From 4df17357de650b915267d75b794d1bc5abc08caf Mon Sep 17 00:00:00 2001 From: Artem Azaraev Date: Wed, 10 Sep 2025 12:50:28 +0300 Subject: [PATCH 11/14] Update DevProxy.Abstractions/Data/MSGraphDb.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- DevProxy.Abstractions/Data/MSGraphDb.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/DevProxy.Abstractions/Data/MSGraphDb.cs b/DevProxy.Abstractions/Data/MSGraphDb.cs index 99ed2b3c..b470898b 100644 --- a/DevProxy.Abstractions/Data/MSGraphDb.cs +++ b/DevProxy.Abstractions/Data/MSGraphDb.cs @@ -253,8 +253,7 @@ private async Task DownloadOpenAPIFileAsync(string url, FileInfo yamlFile, var _ = response.EnsureSuccessStatusCode(); await using var contentStream = await response.Content.ReadAsStreamAsync(cancellationToken); await using var fileStream = new FileStream(yamlFile.FullName, - new FileStreamOptions { Mode = FileMode.Create, Access = FileAccess.Write, Share = FileShare.None } - ); + new FileStreamOptions { Mode = FileMode.Create, Access = FileAccess.Write, Share = FileShare.None }); await contentStream.CopyToAsync(fileStream, cancellationToken); if (response.Headers.ETag != null) From 577812fabced8752c267ca49395e81119c14ed09 Mon Sep 17 00:00:00 2001 From: Artem Azaraev Date: Wed, 10 Sep 2025 12:50:38 +0300 Subject: [PATCH 12/14] Update DevProxy.Abstractions/Data/MSGraphDb.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- DevProxy.Abstractions/Data/MSGraphDb.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DevProxy.Abstractions/Data/MSGraphDb.cs b/DevProxy.Abstractions/Data/MSGraphDb.cs index b470898b..6f5bacd4 100644 --- a/DevProxy.Abstractions/Data/MSGraphDb.cs +++ b/DevProxy.Abstractions/Data/MSGraphDb.cs @@ -249,7 +249,7 @@ private async Task DownloadOpenAPIFileAsync(string url, FileInfo yamlFile, return false; } - //save the new OpenAPI spec + // Save the new OpenAPI spec. var _ = response.EnsureSuccessStatusCode(); await using var contentStream = await response.Content.ReadAsStreamAsync(cancellationToken); await using var fileStream = new FileStream(yamlFile.FullName, From 65db30d5eb8ead482ad73e6a778102d8ce87e001 Mon Sep 17 00:00:00 2001 From: Artem Azaraev Date: Wed, 10 Sep 2025 13:00:18 +0300 Subject: [PATCH 13/14] Fix discarded variable declaration --- DevProxy.Abstractions/Data/MSGraphDb.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DevProxy.Abstractions/Data/MSGraphDb.cs b/DevProxy.Abstractions/Data/MSGraphDb.cs index 6f5bacd4..ef414422 100644 --- a/DevProxy.Abstractions/Data/MSGraphDb.cs +++ b/DevProxy.Abstractions/Data/MSGraphDb.cs @@ -250,7 +250,7 @@ private async Task DownloadOpenAPIFileAsync(string url, FileInfo yamlFile, } // Save the new OpenAPI spec. - var _ = response.EnsureSuccessStatusCode(); + _ = response.EnsureSuccessStatusCode(); await using var contentStream = await response.Content.ReadAsStreamAsync(cancellationToken); await using var fileStream = new FileStream(yamlFile.FullName, new FileStreamOptions { Mode = FileMode.Create, Access = FileAccess.Write, Share = FileShare.None }); From 811fa673407e020386346ccd9498df69427845cf Mon Sep 17 00:00:00 2001 From: waldekmastykarz Date: Thu, 11 Sep 2025 07:39:25 +0200 Subject: [PATCH 14/14] Minor fixes --- DevProxy.Abstractions/Data/MSGraphDb.cs | 42 +++++++++++-------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/DevProxy.Abstractions/Data/MSGraphDb.cs b/DevProxy.Abstractions/Data/MSGraphDb.cs index ef414422..eaced4e8 100644 --- a/DevProxy.Abstractions/Data/MSGraphDb.cs +++ b/DevProxy.Abstractions/Data/MSGraphDb.cs @@ -25,14 +25,6 @@ public sealed class MSGraphDb(HttpClient httpClient, ILogger logger) // v1 refers to v1 of the db schema, not the graph version public static string MSGraphDbFilePath => Path.Combine(ProxyUtils.AppFolder!, "msgraph-openapi-v1.db"); - private static string GetOpenApiSpecUrl(string version) => $"https://raw.githubusercontent.com/microsoftgraph/msgraph-metadata/master/openapi/{version}/openapi.yaml"; - - private static string GetBaseGraphOpenApiFileName(string version) => $"graph-{version.Replace(".", "_", StringComparison.OrdinalIgnoreCase)}-openapi"; - - private static string GetGraphOpenApiYamlFileName(string version) => $"{GetBaseGraphOpenApiFileName(version)}.yaml"; - - private static string GetGraphOpenApiEtagFileName(string version) => $"{GetBaseGraphOpenApiFileName(version)}.etag.txt"; - public SqliteConnection Connection { get @@ -66,11 +58,11 @@ public async Task GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke return 1; } - var (isApiModified, errorCount) = await UpdateOpenAPIGraphFilesIfNecessaryAsync(appFolder, cancellationToken); + var (isApiModified, hasErrors) = await UpdateOpenAPIGraphFilesIfNecessaryAsync(appFolder, cancellationToken); - if (errorCount > 0) + if (hasErrors) { - _logger.LogWarning("Unable to generate Microsoft Graph database"); + _logger.LogWarning("Unable to update Microsoft Graph database"); return 1; } @@ -78,7 +70,7 @@ public async Task GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke { UpdateLastWriteTime(dbFileInfo); _logger.LogDebug("Updated the last-write-time attribute of Microsoft Graph database {File}", dbFileInfo); - _logger.LogInformation("Microsoft Graph database is already updated"); + _logger.LogInformation("Microsoft Graph database is already up-to-date"); return 1; } @@ -90,11 +82,9 @@ public async Task GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke } await CreateDbAsync(cancellationToken); - await FillDataAsync(cancellationToken); _logger.LogInformation("Microsoft Graph database is successfully updated"); - return 0; } catch (Exception ex) @@ -102,7 +92,6 @@ public async Task GenerateDbAsync(bool skipIfUpdatedToday, CancellationToke _logger.LogError(ex, "Error generating Microsoft Graph database"); return 1; } - } private static bool IsModifiedToday(FileInfo fileInfo) => fileInfo.Exists && fileInfo.LastWriteTime.Date == DateTime.Now.Date; @@ -190,12 +179,12 @@ private async Task FillDataAsync(CancellationToken cancellationToken) _logger.LogInformation("Inserted {EndpointCount} endpoints in the database", i); } - private async Task<(bool isApiUpdated, int errors)> UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, CancellationToken cancellationToken) + private async Task<(bool isApiUpdated, bool hasErrors)> UpdateOpenAPIGraphFilesIfNecessaryAsync(string folder, CancellationToken cancellationToken) { _logger.LogInformation("Checking for updated OpenAPI files..."); var isApiUpdated = false; - var errorCount = 0; + var hasErrors = false; foreach (var version in graphVersions) { @@ -214,16 +203,14 @@ private async Task FillDataAsync(CancellationToken cancellationToken) var etagFile = new FileInfo(Path.Combine(folder, GetGraphOpenApiEtagFileName(version))); isApiUpdated |= await DownloadOpenAPIFileAsync(url, yamlFile, etagFile, cancellationToken); - - _logger.LogDebug("Downloaded OpenAPI file from {Url} to {File}", url, yamlFile); } catch (Exception ex) { - errorCount++; + hasErrors = true; _logger.LogError(ex, "Error updating OpenAPI files"); } } - return (isApiUpdated, errorCount); + return (isApiUpdated, hasErrors); } private async Task DownloadOpenAPIFileAsync(string url, FileInfo yamlFile, FileInfo etagFile, CancellationToken cancellationToken) @@ -245,7 +232,7 @@ private async Task DownloadOpenAPIFileAsync(string url, FileInfo yamlFile, if (response.StatusCode == HttpStatusCode.NotModified) { UpdateLastWriteTime(yamlFile); - _logger.LogDebug("Updated the last-write-time attribute of OpenAPI file {File}", yamlFile); + _logger.LogDebug("File {File} already up-to-date. Updated the last-write-time attribute", yamlFile); return false; } @@ -256,7 +243,7 @@ private async Task DownloadOpenAPIFileAsync(string url, FileInfo yamlFile, new FileStreamOptions { Mode = FileMode.Create, Access = FileAccess.Write, Share = FileShare.None }); await contentStream.CopyToAsync(fileStream, cancellationToken); - if (response.Headers.ETag != null) + if (response.Headers.ETag is not null) { await File.WriteAllTextAsync(etagFile.FullName, response.Headers.ETag.Tag, cancellationToken); } @@ -265,6 +252,7 @@ private async Task DownloadOpenAPIFileAsync(string url, FileInfo yamlFile, etagFile.Delete(); } + _logger.LogDebug("Downloaded OpenAPI file from {Url} to {File}", url, yamlFile); return true; } @@ -314,6 +302,14 @@ private void SetDbJournaling(bool enabled) } } + private static string GetOpenApiSpecUrl(string version) => $"https://raw.githubusercontent.com/microsoftgraph/msgraph-metadata/master/openapi/{version}/openapi.yaml"; + + private static string GetBaseGraphOpenApiFileName(string version) => $"graph-{version.Replace(".", "_", StringComparison.OrdinalIgnoreCase)}-openapi"; + + private static string GetGraphOpenApiYamlFileName(string version) => $"{GetBaseGraphOpenApiFileName(version)}.yaml"; + + private static string GetGraphOpenApiEtagFileName(string version) => $"{GetBaseGraphOpenApiFileName(version)}.etag.txt"; + public void Dispose() { _connection?.Dispose();