-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Evans Aboge (from Dev Box)
committed
Jan 15, 2025
1 parent
a99c4a8
commit a96a7e8
Showing
6 changed files
with
195 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.OpenApi.Models; | ||
using Microsoft.OpenApi.Services; | ||
|
||
namespace Kiota.Builder.Settings; | ||
/// <summary> | ||
/// A service that manages the settings file for http language snippets. | ||
/// </summary> | ||
public interface ISettingsManagementService | ||
{ | ||
/// <summary> | ||
/// Gets the settings file for a Kiota project by crawling the directory tree. | ||
/// </summary> | ||
/// <param name="searchDirectory"></param> | ||
/// <returns></returns> | ||
string GetDirectoryContainingSettingsFile(string searchDirectory); | ||
|
||
/// <summary> | ||
/// Gets the settings from a directory. | ||
/// </summary> | ||
/// <param name="directoryPath"></param> | ||
/// <returns></returns> | ||
Task<SettingsFile> GetSettingsFromDirectoryAsync(string directoryPath, CancellationToken cancellationToken); | ||
|
||
/// <summary> | ||
/// Gets the settings from a stream. | ||
/// </summary> | ||
/// <param name="stream"></param> | ||
/// <returns></returns> | ||
Task<SettingsFile> GetSettingsFromStreamAsync(Stream stream); | ||
|
||
/// <summary> | ||
/// Writes the settings file to a directory. | ||
/// </summary> | ||
/// <param name="directoryPath"></param> | ||
/// <param name="openApiDocument">OpenApi document</param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
Task WriteSettingsFileAsync(string directoryPath, OpenApiDocument openApiDocument, CancellationToken cancellationToken); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System.Text.Json.Serialization; | ||
using Kiota.Builder.Configuration; | ||
|
||
namespace Kiota.Builder.Settings; | ||
public class SettingsFile | ||
{ | ||
[JsonPropertyName("rest-client.environmentVariables")] | ||
public EnvironmentVariables EnvironmentVariables | ||
{ | ||
get; set; | ||
} | ||
|
||
public SettingsFile() | ||
{ | ||
EnvironmentVariables = new EnvironmentVariables(); | ||
} | ||
} | ||
|
||
public class EnvironmentVariables | ||
{ | ||
[JsonPropertyName("$shared")] | ||
public SharedAuth Shared | ||
{ | ||
get; set; | ||
} | ||
|
||
[JsonPropertyName("remote")] | ||
public AuthenticationSettings Remote | ||
{ | ||
get; set; | ||
} | ||
|
||
[JsonPropertyName("development")] | ||
public AuthenticationSettings Development | ||
{ | ||
get; set; | ||
} | ||
|
||
public EnvironmentVariables() | ||
{ | ||
Shared = new SharedAuth(); | ||
Remote = new AuthenticationSettings(); | ||
Development = new AuthenticationSettings(); | ||
} | ||
} | ||
|
||
public class SharedAuth | ||
{ | ||
|
||
} | ||
|
||
public class AuthenticationSettings | ||
{ | ||
public string HostAddress | ||
{ | ||
get; set; | ||
} | ||
public string? BasicAuth | ||
{ | ||
get; set; | ||
} | ||
public string? Bearer | ||
{ | ||
get; set; | ||
} | ||
|
||
public AuthenticationSettings() | ||
{ | ||
HostAddress = ""; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Kiota.Builder/Settings/SettingsFileGenerationContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json.Serialization.Metadata; | ||
using Kiota.Builder.Lock; | ||
|
||
namespace Kiota.Builder.Settings; | ||
|
||
[JsonSerializable(typeof(SettingsFile))] | ||
internal partial class SettingsFileGenerationContext : JsonSerializerContext | ||
{ | ||
} |
60 changes: 60 additions & 0 deletions
60
src/Kiota.Builder/Settings/SettingsFileManagementService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace Kiota.Builder.Settings; | ||
|
||
public class SettingsFileManagementService : ISettingsManagementService | ||
{ | ||
internal const string SettingsFileName = "settings.json"; | ||
public string GetDirectoryContainingSettingsFile(string searchDirectory) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task<SettingsFile> GetSettingsFromDirectoryAsync(string directoryPath, CancellationToken cancellationToken) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task<SettingsFile> GetSettingsFromStreamAsync(Stream stream) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task WriteSettingsFileAsync(string directoryPath, OpenApiDocument openApiDocument, CancellationToken cancellationToken) | ||
{ | ||
ArgumentException.ThrowIfNullOrEmpty(directoryPath); | ||
ArgumentNullException.ThrowIfNull(openApiDocument); | ||
var settings = GenerateSettingsFile(openApiDocument); | ||
return WriteSettingsFileInternalAsync(directoryPath, settings, cancellationToken); | ||
} | ||
|
||
private static SettingsFile GenerateSettingsFile(OpenApiDocument openApiDocument) | ||
{ | ||
var settings = new SettingsFile(); | ||
settings.EnvironmentVariables.Development.HostAddress = openApiDocument.Servers[0].Url; | ||
settings.EnvironmentVariables.Remote.HostAddress = openApiDocument.Servers[0].Url; | ||
return settings; | ||
} | ||
|
||
private static readonly JsonSerializerOptions options = new() | ||
{ | ||
WriteIndented = true, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
}; | ||
|
||
private static readonly SettingsFileGenerationContext context = new(options); | ||
|
||
private static async Task WriteSettingsFileInternalAsync(string directoryPath, SettingsFile settings, CancellationToken cancellationToken) | ||
{ | ||
var filePath = Path.Combine(directoryPath, SettingsFileName); | ||
#pragma warning disable CA2007 // Dispose objects before losing scope | ||
await using var fileStream = File.Open(filePath, FileMode.Create); | ||
#pragma warning disable CA2007 | ||
await JsonSerializer.SerializeAsync(fileStream, settings, context.SettingsFile, cancellationToken).ConfigureAwait(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters