// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using Microsoft.Extensions.Configuration; using Squidex.ClientLibrary; using Squidex.ClientLibrary.Configuration; using Squidex.ClientLibrary.Management; using TestSuite.Utils; namespace TestSuite { public sealed class ClientManagerWrapper { private readonly Lazy apps; private readonly Lazy assets; private readonly Lazy comments; private readonly Lazy backups; private readonly Lazy diagnostics; private readonly Lazy history; private readonly Lazy languages; private readonly Lazy ping; private readonly Lazy plans; private readonly Lazy rules; private readonly Lazy schemas; private readonly Lazy search; private readonly Lazy templates; private readonly Lazy translations; public SquidexClientManager ClientManager { get; } public IAppsClient Apps { get => apps.Value; } public IAssetsClient Assets { get => assets.Value; } public IBackupsClient Backups { get => backups.Value; } public ICommentsClient Comments { get => comments.Value; } public IDiagnosticsClient Diagnostics { get => diagnostics.Value; } public IHistoryClient History { get => history.Value; } public ILanguagesClient Languages { get => languages.Value; } public IPingClient Ping { get => ping.Value; } public IPlansClient Plans { get => plans.Value; } public IRulesClient Rules { get => rules.Value; } public ISchemasClient Schemas { get => schemas.Value; } public ISearchClient Search { get => search.Value; } public ITemplatesClient Templates { get => templates.Value; } public ITranslationsClient Translations { get => translations.Value; } public ClientManagerWrapper() { var appName = TestHelpers.GetAndPrintValue("config:app:name", "integration-tests"); var clientId = TestHelpers.GetAndPrintValue("config:client:id", "root"); var clientSecret = TestHelpers.GetAndPrintValue("config:client:secret", "xeLd6jFxqbXJrfmNLlO2j1apagGGGSyZJhFnIuHp4I0="); var serverUrl = TestHelpers.GetAndPrintValue("config:server:url", "https://localhost:5001"); ClientManager = new SquidexClientManager(new SquidexOptions { AppName = appName, ClientId = clientId, ClientSecret = clientSecret, ClientFactory = null, Configurator = AcceptAllCertificatesConfigurator.Instance, ReadResponseAsString = true, Url = serverUrl }); apps = new Lazy(() => { return ClientManager.CreateAppsClient(); }); assets = new Lazy(() => { return ClientManager.CreateAssetsClient(); }); backups = new Lazy(() => { return ClientManager.CreateBackupsClient(); }); comments = new Lazy(() => { return ClientManager.CreateCommentsClient(); }); diagnostics = new Lazy(() => { return ClientManager.CreateDiagnosticsClient(); }); history = new Lazy(() => { return ClientManager.CreateHistoryClient(); }); languages = new Lazy(() => { return ClientManager.CreateLanguagesClient(); }); ping = new Lazy(() => { return ClientManager.CreatePingClient(); }); plans = new Lazy(() => { return ClientManager.CreatePlansClient(); }); rules = new Lazy(() => { return ClientManager.CreateRulesClient(); }); schemas = new Lazy(() => { return ClientManager.CreateSchemasClient(); }); search = new Lazy(() => { return ClientManager.CreateSearchClient(); }); templates = new Lazy(() => { return ClientManager.CreateTemplatesClient(); }); translations = new Lazy(() => { return ClientManager.CreateTranslationsClient(); }); } public async Task ConnectAsync() { var waitSeconds = TestHelpers.Configuration.GetValue("config:wait"); if (waitSeconds > 10) { Console.WriteLine("Waiting {0} seconds to access server", waitSeconds); var pingClient = ClientManager.CreatePingClient(); try { using (var cts = new CancellationTokenSource(waitSeconds * 1000)) { while (!cts.IsCancellationRequested) { try { await pingClient.GetPingAsync(cts.Token); break; } catch { await Task.Delay(100, cts.Token); } } } } catch (OperationCanceledException) { throw new InvalidOperationException("Cannot connect to test system."); } Console.WriteLine("Connected to server."); } else { Console.WriteLine("Waiting for server is skipped."); } return this; } } }