예제 #1
0
 public bool Connect()
 {
     try
     {
         logger.Info($"Connect to: {ConnectUri.AbsoluteUri}");
         var global = GetGlobelContext();
         var task   = global.IsDesktopModeAsync();
         task.Wait(2500);
         if (!task.IsCompleted)
         {
             throw new Exception("No connection to qlik.");
         }
         if (task.Result)
         {
             Mode = QlikAppMode.DESKTOP;
         }
         logger.Debug($"Use connection mode: {Mode}");
         if (IsSharedSession)
         {
             try
             {
                 CurrentApp = global.GetActiveDocAsync().Result;
             }
             catch (Exception ex)
             {
                 logger.Error(ex, "No existing shared session found. Please open the app in the browser.");
                 return(false);
             }
         }
         else
         {
             var appName = String.Empty;
             if (Mode == QlikAppMode.DESKTOP)
             {
                 appName = SenseUtilities.GetFullAppName(Config.App);
             }
             else
             {
                 appName = GetAppId(global);
             }
             logger.Debug($"Connect with app name: {appName}");
             CurrentApp = global.OpenDocAsync(appName).Result;
         }
         logger.Debug("The Connection to Qlik was successfully");
         return(true);
     }
     catch (Exception ex)
     {
         logger.Error(ex, $"The connection to Qlik Sense with uri \"{ConnectUri}\" app \"{Config.App}\" could not be established.");
         return(false);
     }
 }
예제 #2
0
        public Connection(string identity, ConnectionConfig config)
        {
            if (config.ServerUri.Scheme.EndsWith("s"))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls |
                                                       SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
                ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate certificate,
                                                                                    X509Chain chain, SslPolicyErrors sslPolicyErrors)
                {
                    return(true); // **** Always accept
                };
            }

            Mode            = QlikAppMode.SERVER;
            IsSharedSession = true;
            Config          = config;
            Identity        = identity;

            var connectUrl = SwitchScheme(Config.ServerUri.AbsoluteUri);
            var appurl     = Uri.EscapeDataString(SenseUtilities.GetFullAppName(Config.App).TrimStart('/'));

            connectUrl = $"{connectUrl}/app/{appurl}";

            if (identity == null)
            {
                connectUrl      = $"{connectUrl}/identity/{Guid.NewGuid().ToString()}";
                IsSharedSession = false;
            }
            else if (!String.IsNullOrEmpty(identity))
            {
                connectUrl = $"{connectUrl}/identity/{identity}";
            }

            ConnectUri = new Uri(connectUrl);
            logger.Info($"Create Qlik connection {ConnId} to {connectUrl} with app {Config.App} and identity {identity}.");
        }
예제 #3
0
        static void Main(string[] args)
        {
            SetLoggerSettings("App.config");
            logger.Info("Start");

            Session session = null;
            IDoc    app     = null;

            //Result exception in TryConvert.
            try
            {
                var config = new EnigmaConfigurations()
                {
                    Url = $"ws://127.0.0.1:4848/app/engineData/identity/{Guid.NewGuid()}",
                    //Url = $"wss://127.0.0.1/app/engineData/identity/{Guid.NewGuid()}",

                    // if you want to create your own Connection with for example header / cookies, just inject this in line
                    CreateSocket = async(url) =>
                    {
                        var ws = new ClientWebSocket();
#if NETCOREAPP2_1
                        var ck = new CookieContainer();
                        ck.Add(new Uri(url), new Cookie("X-Qlik-Session", "xxxxxxxxx"));
                        ws.Options.Cookies = ck;
                        ws.Options.RemoteCertificateValidationCallback
                            += new RemoteCertificateValidationCallback((object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return(true); });
#endif
                        //!!!!!!!!!here you can inject your cookie, header authentification,...
                        await ws.ConnectAsync(new Uri(url), CancellationToken.None);

                        return(ws);
                    }
                };

                session = Enigma.Create(config);
                // connect to the engine
                var globalTask = session.OpenAsync();
                globalTask.Wait();

                IGlobal global  = Impromptu.ActLike <IGlobal>(globalTask.Result);
                var     appName = SenseUtilities.GetFullAppName("Executive Dashboard");
                app         = global.OpenDocAsync(appName).Result;
                app.Closed += App_Closed;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            var mytasks = new List <Task>();
            var ce1     = new CalculationExample(app);

            ce1.CalcRandom(120);

            Task.WaitAll(mytasks.ToArray());

            var count = mytasks.Count;

            //global.EngineVersionAsync()
            //    .ContinueWith((engVer) =>
            //    {
            //        Console.WriteLine("CastedEngineVer:" + engVer.Result.qComponentVersion);
            //    });

            //global.OpenDocAsync(appName)
            //    .ContinueWith((newApp) =>
            //    {

            //        Console.WriteLine("Object " + (newApp.Result).ToString());

            //        var app = newApp.Result;

            //        // test the changed notification of the opend app
            //        app.Changed += App_Changed;

            //        // just a normal get script
            //        app.GetScriptAsync()
            //            .ContinueWith((script) =>
            //            {
            //                Console.WriteLine("Script" + script.Result.ToString().Substring(1, 100));
            //            });

            //        // change the script, so that the app changed is triggered
            //        app.SetScriptAsync("HALLO")
            //            .ContinueWith((res) =>
            //            {
            //                // read the changed script
            //                app.GetScriptAsync()
            //                    .ContinueWith((script) =>
            //                    {
            //                        Console.WriteLine("Script2" + script.Result.ToString());
            //                    });
            //            });

            //    });

            //Thread.Sleep(3000);
            var example = new ChangeEventsExample(app);

            example.RunExample();

            var tasks = new List <Task>();
            //Set bookmark test
            var bookmark = app.GetBookmarkAsync("demobookmark").Result;

            //evaluate request
            var request = JObject.FromObject(new
            {
                qExpression = "'$(vCurrentYear)'"
            });

            //Use this Overload from EvaluateExAsync it works fine.
            var result = app.EvaluateExAsync(request).Result;

            //Use this Overload it crashes!!!
            result = app.EvaluateExAsync("'$(vCurrentYear)'").Result;

            //Caluculation Test
            var calc = new CalculationExample(app);

            calc.CalcRandom(1);

            //find the bookmark with type
            var bookmarkExample = new BookmarkExample(app);

            tasks.Add(bookmarkExample.ListBookmarksAsync());

            //find dimensions
            var dimensionExample = new DimensionExample(app);

            tasks.Add(dimensionExample.ListDimensionsAsync());

            //find current selections
            var selectionExample = new SelectionExample(app);

            tasks.Add(selectionExample.ListCurrentSelectionsAsync());

            ////find list object data
            var listObjectExample = new ListObjectExample(app);

            tasks.Add(listObjectExample.ListListObjectDataAsync());

            ////Fire Multiple Requests
            var multipleRequestsExample = new MultipleRequests(app);

            tasks.Add(multipleRequestsExample.FireMultipleRequestsAsync());

            Task.WaitAll(tasks.ToArray());

            var task5 = listObjectExample.GetGenericObjectAsync("Region");

            var task6 = listObjectExample.GetListObjectDataAsync(task5.Result);

            dynamic jsonObject = task6.Result;

            foreach (var item in jsonObject[0].qMatrix)
            {
                Console.WriteLine(item[0]?.qText + "");
            }

            Console.WriteLine("Finish");
            var _ = session.CloseAsync();

            Console.ReadLine();
        }