using System;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using Speckle.Core.Api;
using Speckle.Core.Credentials;
using Speckle.Core.Logging;
namespace Speckle.ConnectorUnity.Ops
{
[Serializable]
public sealed class SpeckleUnityClient : IDisposable, IShouldValidate
{
AccountAdapter _accountAdapter;
public Account account
{
get => _accountAdapter?.source;
}
public CancellationToken token { get; set; }
public Client source { get; private set; }
public SpeckleUnityClient(Account obj)
{
if (obj == null) return;
_accountAdapter = new AccountAdapter(obj);
source = new Client(account);
}
public void Dispose()
{
source?.Dispose();
}
///
/// Loads a new speckle stream by fetching the stream with a valid url
///
///
public async UniTask LoadStreamByUrl(string url)
{
Stream res = null;
SpeckleUnityClient c = null;
try
{
var s = new StreamWrapper(url);
if (s.IsValid)
{
c = new SpeckleUnityClient(await s.GetAccount());
res = await c.StreamGet(url);
}
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Warn(e.Message);
c?.Dispose();
}
return res;
}
///
/// Checks if this this linked with an account and speckle this
///
///
public bool IsValid() => account != null && source != null;
#region object operations
///
///
///
///
///
///
public async UniTask ObjectGet(string streamId, string objectId)
{
SpeckleObject res = null;
try
{
if (IsValid())
{
res = await source.ObjectGet(token, streamId, objectId);
}
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return res;
}
#endregion
#region branch operations
///
///
///
///
///
public async UniTask BranchCreate(BranchCreateInput input)
{
var res = string.Empty;
try
{
if (IsValid())
res = await source.BranchCreate(token, input);
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return res;
}
///
///
///
///
///
public async UniTask BranchDelete(BranchDeleteInput input)
{
var res = false;
try
{
if (IsValid())
res = await source.BranchDelete(token, input);
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return res;
}
///
///
///
///
///
///
///
public async UniTask> BranchesGet(string streamId, int branchLimit = 10, int commitLimit = 5)
{
var items = new List();
if (IsValid() && streamId.Valid())
items = await source.StreamGetBranches(token, streamId, branchLimit, commitLimit);
return items;
}
///
/// Loads a branch and its commits from a speckle stream
///
///
///
///
///
public async UniTask BranchGet(string streamId, string branchName, int commitLimit = 10)
{
Branch res = null;
try
{
if (IsValid())
{
res = await source.BranchGet(token, streamId, branchName, commitLimit);
}
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return res;
}
#endregion
#region commit operations
///
///
///
///
///
///
public async UniTask CommitGet(string streamId, string commitId)
{
Commit res = null;
try
{
if (IsValid())
res = await source.CommitGet(token, streamId, commitId);
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return res;
}
public async UniTask CommitExists(string streamId, string commitId)
{
var res = false;
try
{
if (IsValid())
res = await source.CommitGet(token, streamId, commitId) != null;
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return res;
}
///
///
///
///
///
public async UniTask CommitCreate(CommitCreateInput input)
{
var res = string.Empty;
try
{
if (IsValid())
res = await source.CommitCreate(token, input);
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return res;
}
///
///
///
///
///
public async UniTask CommitUpdate(CommitUpdateInput input)
{
var res = false;
try
{
if (IsValid())
res = await source.CommitUpdate(token, input);
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return res;
}
///
///
///
///
///
public async UniTask CommitDelete(CommitDeleteInput input)
{
var res = false;
try
{
if (IsValid())
res = await source.CommitDelete(token, input);
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return res;
}
///
///
///
///
///
public async UniTask CommitReceived(CommitReceivedInput input)
{
var res = false;
try
{
if (IsValid())
res = await source.CommitReceived(token, input);
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return res;
}
#endregion
#region stream operations
///
/// Creates a new stream with the account associated with the this
/// If no valid account is found it will cancel the task
///
///
///
///
///
public async UniTask StreamCreate(string name, string description, bool isPublic) => await StreamCreate
(new StreamCreateInput
{
name = name, isPublic = isPublic, description = description
}
);
///
/// Creates a new stream with the account associated with the this
/// If no valid account is found it will cancel the task
///
///
///
public async UniTask StreamCreate(StreamCreateInput input)
{
var streamId = "invalid";
try
{
if (IsValid())
streamId = await source.StreamCreate(token, input);
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return streamId;
}
///
///
///
///
///
public async UniTask StreamDelete(string streamId)
{
var res = false;
try
{
if (IsValid())
res = await source.StreamDelete(token, streamId);
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return res;
}
///
///
///
///
///
public async UniTask StreamUpdate(StreamUpdateInput input)
{
var res = false;
try
{
if (IsValid())
res = await source.StreamUpdate(token, input);
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return res;
}
///
///
///
///
///
///
///
///
///
///
public async UniTask> StreamActivity(
string streamId,
DateTime? before = null, DateTime? after = null, DateTime? cursor = null,
string actionType = null,
int limit = 10
)
{
var res = new List();
try
{
if (IsValid())
res = await source.StreamGetActivity(token, streamId, before, after, cursor, actionType, limit);
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return res;
}
///
///
///
///
///
///
public async UniTask> StreamSearch(string streamId, int branchLimit = 10)
{
var res = new List();
try
{
if (IsValid())
res = await source.StreamSearch(token, streamId, branchLimit);
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return res;
}
///
/// An Operation call to read in a speckle Stream. This will retrieve the stream information and populate the available branches
///
///
///
///
public async UniTask StreamGet(string streamId, int branchLimit = 10)
{
Stream res = null;
try
{
if (IsValid())
res = await source.StreamGet(token, streamId, branchLimit);
}
catch (SpeckleException e)
{
SpeckleUnity.Console.Log(e.Message);
}
return res;
}
public async UniTask StreamExists(string streamId) => await StreamGet(streamId, 0) != null;
#endregion
}
}