Files
speckle-sharp-connectors/Sdk/Speckle.Connectors.Common/Operations/AccountService.cs
T
Oğuzhan Koral bed7376982 Feat(gh): workspaces and search wizard (#820)
* WIP

* Menu handlers for projects and models

* Extract all handlers outside

* Remove account from handlers

* Reset version count

* Add workspaces

* Introduce SpeckleOperationWizard

* Move sync ops to dev section

* Fix sdk remainings

* Organize files and namespaces

* Remove context menu setters

* Bump sdk to 3.3.0

* Get the last selected account id from config

* Add workspaces to select model component

* Handle workspaces

* Add separator at ctor

* Get rid of from last fetched collections in wizard

* Reorder public privy functions

* Bump sdk 3.3.3 for project with permissions

* Remove expire solution on account change

* Move SolveInstanceWithUrlInput to wizard

* Better state handling on search

* handle account switch

* close menu explicitly when reset

* Have workspace logo

* Don't populate menu completely after search

* Logo as prop in workspaces

* Create a workspace flow

* Bump sdk back to 3.3.3

* Fixed DI

* Fix unplug URL state

* Set icon null when reset workspace

* personal projects as workspace

* Handle project permissions over pasted URL

* Clean up the main component

* handle all errors at the top level for wizard component

* Set last used account id to config

* Change the message level to warning for accounts

---------

Co-authored-by: Adam Hathcock <adam@hathcock.uk>
2025-05-13 00:23:09 +03:00

77 lines
2.4 KiB
C#

using System.Runtime.Serialization;
using Speckle.InterfaceGenerator;
using Speckle.Newtonsoft.Json;
using Speckle.Sdk.Credentials;
using Speckle.Sdk.SQLite;
namespace Speckle.Connectors.Common.Operations;
/// <summary>
/// Service that responsible to get account for DUI3 from account id otherwise from server url if any.
/// Note: Be sure it is registered on refactorings. Otherwise, we won't be able to do any send/receive ops.
/// This can safely be registered as singleton.
/// </summary>
[GenerateAutoInterface]
public class AccountService(
IAccountManager accountManager,
ISqLiteJsonCacheManagerFactory sqLiteJsonCacheManagerFactory
) : IAccountService
{
/// <summary>
/// Account to retrieve with its id, if not exist try to retrieve from matching serverUrl.
/// </summary>
/// <param name="accountId">Id of the account.</param>
/// <param name="serverUrl">Server url to search matching account.</param>
/// <returns></returns>
/// <exception cref="SpeckleAccountManagerException"> Throws if server url doesn't match with any account.</exception>
public Account GetAccountWithServerUrlFallback(string accountId, Uri serverUrl)
{
try
{
return accountManager.GetAccount(accountId);
}
catch (SpeckleAccountManagerException)
{
var accounts = accountManager.GetAccounts(serverUrl);
return accounts.First()
?? throw new SpeckleAccountManagerException($"No any account found that matches with server {serverUrl}");
}
}
public string? GetUserSelectedAccountId()
{
var jsonCacheManager = sqLiteJsonCacheManagerFactory.CreateForUser("DUI3Config");
var rawConfig = jsonCacheManager.GetObject("accounts");
if (rawConfig is null)
{
return null;
}
try
{
var config = JsonConvert.DeserializeObject<AccountsConfig>(rawConfig);
if (config is null)
{
throw new SerializationException("Failed to deserialize accounts config");
}
return config.UserSelectedAccountId;
}
catch (SerializationException)
{
return null;
}
}
public void SetUserSelectedAccountId(string userSelectedAccountId)
{
var jsonCacheManager = sqLiteJsonCacheManagerFactory.CreateForUser("DUI3Config");
var str = JsonConvert.SerializeObject(new AccountsConfig() { UserSelectedAccountId = userSelectedAccountId });
jsonCacheManager.UpdateObject("accounts", str);
}
}
public class AccountsConfig
{
public string? UserSelectedAccountId { get; set; }
}