using Cysharp.Threading.Tasks; using System; using System.Collections.Generic; using System.Threading; using MoralisUnity.Platform; using MoralisUnity.Platform.Abstractions; using MoralisUnity.Platform.Objects; using MoralisUnity.Platform.Queries; using MoralisUnity.Platform.Services; using MoralisUnity.Platform.Services.ClientServices; using MoralisUnity.Platform.Services.Infrastructure; using MoralisUnity.SolanaApi.Interfaces; using MoralisUnity.Web3Api.Interfaces; namespace MoralisUnity { public class MoralisClient where TUser : MoralisUser { string serverAuthToken = String.Empty; public MoralisClient(ServerConnectionData connectionData, IWeb3Api web3Api = null, ISolanaApi solanaApi = null, IJsonSerializer jsonSerializer = null) { if (jsonSerializer == null) { throw new ArgumentException("jsonSerializer cannot be null."); } connectionData.Key = connectionData.Key != null ? connectionData.Key : ""; moralisService = new MoralisService(connectionData.ApplicationID, connectionData.ServerURI, connectionData.Key, jsonSerializer); moralisService.ServerConnectionData.Key = connectionData.Key; moralisService.ServerConnectionData.ServerURI = connectionData.ServerURI; moralisService.ServerConnectionData.ApplicationID = connectionData.ApplicationID; moralisService.ServerConnectionData.LocalStoragePath = connectionData.LocalStoragePath; // Make sure local folder for Unity apps is used if defined. MoralisCacheService.BaseFilePath = connectionData.LocalStoragePath; // Make sure singleton instance is available. moralisService.Publicize(); this.Web3Api = web3Api; if (this.Web3Api is { }) { if (connectionData.ApiKey is { }) { this.Web3Api.Initialize(); } else { this.Web3Api.Initialize(connectionData.ServerURI); } } this.SolanaApi = solanaApi; if (this.SolanaApi is { }) { if (connectionData.ApiKey is { }) { this.SolanaApi.Initialize(); } else { this.SolanaApi.Initialize(connectionData.ServerURI); } } } public string EthAddress { get; } public IServiceHub ServiceHub => moralisService.Services; MoralisService moralisService; public void SetLocalDatastoreController() { throw new NotImplementedException(); } public string ApplicationId { get => moralisService.ServerConnectionData.ApplicationID; set { moralisService.ServerConnectionData.ApplicationID = value; } } public string Key { get => moralisService.ServerConnectionData.Key; set { moralisService.ServerConnectionData.Key = value; } } public string MasterKey { get => moralisService.ServerConnectionData.MasterKey; set { moralisService.ServerConnectionData.MasterKey = value; } } public string ServerUrl { get => moralisService.ServerConnectionData.ServerURI; set { moralisService.ServerConnectionData.ServerURI = value; } } public void SetServerAuthToken(string value) { serverAuthToken = value; } public string GetServerAuthToken() { return serverAuthToken; } public void SetServerAuthType(string value) { serverAuthToken = value; } public string GetServerAuthType() { return serverAuthToken; } public void SetLiveQueryServerURL(string value) { throw new NotImplementedException(); } public string GetLiveQueryServerURL() { throw new NotImplementedException(); } public void SetEncryptedUser(string value) { throw new NotImplementedException(); } public string GetEncryptedUser() { throw new NotImplementedException(); } public void SetSecret(string value) { throw new NotImplementedException(); } public string GetSecret() { throw new NotImplementedException(); } public void SetIdempotency(string value) { throw new NotImplementedException(); } public string GetIdempotency() { throw new NotImplementedException(); } public IFileService File => moralisService.FileService; public IInstallationService InstallationService => moralisService.InstallationService; public IQueryService QueryService => moralisService.QueryService; public ISessionService Session => moralisService.SessionService; public IUserService UserService => moralisService.UserService; public MoralisCloud Cloud => moralisService.Cloud; public async UniTask GetInstallationIdAsync() => await InstallationService.GetAsync(); public async UniTask> Query() where T : MoralisObject { TUser user = await GetCurrentUserAsync(); return new MoralisQuery(this.QueryService, InstallationService, moralisService.ServerConnectionData, moralisService.JsonSerializer, user.sessionToken); //, logger); } public T Create(object[] parameters = null) where T : MoralisObject { return this.ServiceHub.Create(parameters); } public async UniTask GetCurrentUserAsync() => await this.ServiceHub.GetCurrentUserAsync(); public void Dispose() { #if UNITY_WEBGL MoralisLiveQueryManager.DisposeService(); #endif } /// /// Retrieve user object by sesion token. /// /// /// Task public UniTask UserFromSession(string sessionToken) { return this.ServiceHub.BecomeAsync(sessionToken); } /// /// Provid async user login. /// data: /// id: Address /// signature: Signature from wallet /// data: Message that was signed /// /// Authentication data /// Task public async UniTask LogInAsync(IDictionary data) { return await this.LogInAsync(data, CancellationToken.None); } /// Provid async user login. /// data: /// id: Address /// signature: Signature from wallet /// data: Message that was signed /// /// Authentication data /// /// Task public async UniTask LogInAsync(IDictionary data, CancellationToken cancellationToken) { return await this.ServiceHub.LogInWithAsync("moralisEth", data, cancellationToken); } /// /// Logs out the current user. /// /// public async UniTask LogOutAsync() { await this.ServiceHub.LogOutAsync(); } /// /// Constructs a query that is the and of the given queries. /// /// The type of MoralisObject being queried. /// /// An initial query to 'and' with additional queries. /// The list of MoralisQueries to 'and' together. /// A query that is the and of the given queries. public MoralisQuery BuildAndQuery(MoralisQuery source, params MoralisQuery[] queries) where T : MoralisObject { return ServiceHub.ConstructAndQuery(source, queries); } /// /// Construct a query that is the and of two or more queries. /// /// /// /// The list of MoralisQueries to 'and' together. /// A MoralisQquery that is the 'and' of the passed in queries. public MoralisQuery BuildAndQuery(IEnumerable> queries) where T : MoralisObject { return ServiceHub.ConstructAndQuery(queries); } /// /// Constructs a query that is the or of the given queries. /// /// The type of MoralisObject being queried. /// An initial query to 'or' with additional queries. /// The list of MoralisQueries to 'or' together. /// A query that is the or of the given queries. public MoralisQuery BuildOrQuery(MoralisQuery source, params MoralisQuery[] queries) where T : MoralisObject { return ServiceHub.ConstructOrQuery(source, queries); } /// /// Construct a query that is the 'or' of two or more queries. /// /// /// /// The list of MoralisQueries to 'and' together. /// A MoralisQquery that is the 'or' of the passed in queries. public MoralisQuery BuildOrQuery(IEnumerable> queries) where T : MoralisObject { return ServiceHub.ConstructOrQuery(queries); } /// /// Constructs a query that is the nor of the given queries. /// /// The type of MoralisObject being queried. /// An initial query to 'or' with additional queries. /// The list of MoralisQueries to 'or' together. /// A query that is the nor of the given queries. public MoralisQuery BuildNorQuery(MoralisQuery source, params MoralisQuery[] queries) where T : MoralisObject { return ServiceHub.ConstructNorQuery(source, queries); } /// /// Construct a query that is the 'nor' of two or more queries. /// /// /// /// The list of MoralisQueries to 'and' together. /// A MoralisQquery that is the 'nor' of the passed in queries. public MoralisQuery BuildNorQuery(IEnumerable> queries) where T : MoralisObject { return ServiceHub.ConstructNorQuery(queries); } /// /// Deletes target object from the Moralis database. /// /// /// /// public UniTask DeleteAsync(T target) where T : MoralisObject { return target.DeleteAsync(); } /// /// Provide an object hook for Web3Api incase developer supplies a /// web3api client at initialize /// public IWeb3Api Web3Api { get; private set; } /// /// Provide an object hook for SolanaApi /// public ISolanaApi SolanaApi { get; private set; } /// /// Included so that this can be set prior to initialization for systems /// (Unity, Xamarin, etc.) that may not have Assembly Attributes available. /// public static HostManifestData ManifestData { get => ServiceHub.ManifestData; set { ServiceHub.ManifestData = value; MutableServiceHub.ManifestData = value; } } } }