Skip to content

Commit

Permalink
Add default route for kiboards space
Browse files Browse the repository at this point in the history
  • Loading branch information
Jandini committed Nov 7, 2023
1 parent a1c783e commit d58bb56
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/KiBoards/Models/Settings/KibanaSettingsChanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ namespace KiBoards.Models.Settings
class KibanaSettingsChanges
{
[JsonPropertyName("theme:darkMode")]
public bool? ThemeDarkMode { get; set; }
public bool? ThemeDarkMode { get; set; } = null;

[JsonPropertyName("defaultRoute")]
public string DefaultRoute { get; set; } = null;

}
}
16 changes: 13 additions & 3 deletions src/KiBoards/Services/KiBoardsKibanaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,24 @@ public KiBoardsKibanaClient(Uri kibanaUri, HttpClient httpClinet)
_httpClient.DefaultRequestHeaders.Add("kbn-xsrf", "true");
}

public async Task SetDarkModeAsync(bool darkMode, CancellationToken cancellationToken)
private string GetSpaceBaseUrl(string spaceId) => !string.IsNullOrEmpty(spaceId) ? $"/s/{spaceId.ToLower()}" : string.Empty;

public async Task SetDarkModeAsync(bool darkMode, string spaceId, CancellationToken cancellationToken)
{
var content = JsonContent.Create(new KibanaSettingsRequest() { Changes = new KibanaSettingsChanges() { ThemeDarkMode = darkMode } });
var response = await _httpClient.PostAsync("api/kibana/settings", content);
var response = await _httpClient.PostAsync($"{GetSpaceBaseUrl(spaceId)}/api/kibana/settings", content);
response.EnsureSuccessStatusCode();
}


public async Task<bool> TrySetDefaultRoute(string defaultRoute, string spaceId, CancellationToken cancellationToken)
{
var content = JsonContent.Create(new KibanaSettingsRequest() { Changes = new KibanaSettingsChanges() { DefaultRoute = defaultRoute } });
var response = await _httpClient.PostAsync($"{GetSpaceBaseUrl(spaceId)}/api/kibana/settings", content);
return response.IsSuccessStatusCode;
}


public async Task<ImportObjectsResponse> ImportSavedObjectsAsync(string ndjsonFile, string spaceId) => await ImportSavedObjectsAsync(ndjsonFile, spaceId, false, CancellationToken.None);
public async Task<ImportObjectsResponse> ImportSavedObjectsAsync(string ndjsonFile, string spaceId, bool overwrite) => await ImportSavedObjectsAsync(ndjsonFile, spaceId, overwrite, CancellationToken.None);
public async Task<ImportObjectsResponse> ImportSavedObjectsAsync(string ndjsonFile, string spaceId, bool overwrite, CancellationToken cancellationToken)
Expand All @@ -35,7 +45,7 @@ public async Task<ImportObjectsResponse> ImportSavedObjectsAsync(string ndjsonFi
var streamContent = new StreamContent(File.Open(ndjsonFile, FileMode.Open));
multipartContent.Add(streamContent, "file", ndjsonFile);

var response = await _httpClient.PostAsync($"{(spaceId != null ? $"/s/{spaceId.ToLower()}" : "")}/api/saved_objects/_import?overwrite={overwrite.ToString().ToLower()}", multipartContent);
var response = await _httpClient.PostAsync($"{GetSpaceBaseUrl(spaceId)}/api/saved_objects/_import?overwrite={overwrite.ToString().ToLower()}", multipartContent);

response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<ImportObjectsResponse>(new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }, cancellationToken);
Expand Down
7 changes: 4 additions & 3 deletions src/KiBoards/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ public Startup(IMessageSink messageSink)
var result = await kibanaClient.TryCreateSpaceAsync(Space.KiBoards);

if (result)
{
messageSink.WriteMessage($"KiBoards space created successfully.");
}

await kibanaClient.TrySetDefaultRoute("/app/dashboards", Space.KiBoards.Id, CancellationToken.None);


var ndjsonFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), attribute.SearchPattern);

Expand All @@ -73,7 +74,7 @@ public Startup(IMessageSink messageSink)
messageSink.WriteMessage("Warning: Some objects were not imported. Please ensure proper import order based on their dependencies.");
}
});
}
}
}
}
}

0 comments on commit d58bb56

Please sign in to comment.