示例#1
0
        private static async Task <string> CallDaxFormatterAsync(string uri, string query, IGlobalOptions globalOptions)
        {
            Log.Verbose("{class} {method} {uri} {query}", "DaxFormatter", "CallDaxFormatterAsync:Begin", uri, query);
            try
            {
                DaxFormatterRequest req = new DaxFormatterRequest();
                req.Dax = query;

                var data = JsonConvert.SerializeObject(req);

                var enc   = System.Text.Encoding.UTF8;
                var data1 = enc.GetBytes(data);

                // this should allow DaxFormatter to work through http 1.0 proxies
                // see: http://stackoverflow.com/questions/566437/http-post-returns-the-error-417-expectation-failed-c
                //System.Net.ServicePointManager.Expect100Continue = false;



                await PrimeConnectionAsync(uri, globalOptions);

                Uri    originalUri = new Uri(uri);
                string actualUrl   = new UriBuilder(originalUri.Scheme, redirectHost, originalUri.Port, originalUri.PathAndQuery).ToString();

                var webRequestFactory = IoC.Get <WebRequestFactory>();
                var wr = webRequestFactory.Create(new Uri(actualUrl));

                wr.Timeout     = globalOptions.DaxFormatterRequestTimeout.SecondsToMilliseconds();
                wr.ContentType = "application/json";
                wr.Method      = "POST";
                wr.Accept      = "application/json, text/javascript, */*; q=0.01";
                wr.Headers.Add("Accept-Encoding", "gzip,deflate");
                wr.Headers.Add("Accept-Language", "en-US,en;q=0.8");
                wr.ContentType            = "application/json; charset=UTF-8";
                wr.AutomaticDecompression = DecompressionMethods.GZip;

                string output = "";
                using (var strm = await wr.GetRequestStreamAsync())
                {
                    strm.Write(data1, 0, data1.Length);

                    using (var resp = wr.GetResponse())
                    {
                        //var outStrm = new System.IO.Compression.GZipStream(resp.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                        var outStrm = resp.GetResponseStream();
                        using (var reader = new System.IO.StreamReader(outStrm))
                        {
                            output = await reader.ReadToEndAsync();
                        }
                    }
                }

                return(output);
            }
            catch (Exception ex)
            {
                Log.Error("{class} {method} {message}", "DaxFormatter", "CallDaxFormatterAsync", ex.Message);
                throw;
            }
            finally
            {
                Log.Debug("{class} {method}", "DaxFormatter", "CallDaxFormatterAsync:End");
            }
        }
示例#2
0
        private static async Task <string> CallDaxFormatterAsync(string uri, string query, ServerDatabaseInfo serverDbInfo, IGlobalOptions globalOptions, IEventAggregator eventAggregator, bool formatAlternateStyle)
        {
            Log.Verbose("{class} {method} {uri} {query}", "DaxFormatter", "CallDaxFormatterAsync:Begin", uri, query);
            try
            {
                DaxFormatterRequest req = new DaxFormatterRequest();
                req.Dax = query;
                if (globalOptions.DefaultSeparator == DaxStudio.Interfaces.Enums.DelimiterType.SemiColon)
                {
                    req.DecimalSeparator = ',';
                    req.ListSeparator    = ';';
                }
                req.ServerName                 = Crypto.SHA256(serverDbInfo.ServerName);
                req.ServerEdition              = serverDbInfo.ServerEdition;
                req.ServerType                 = serverDbInfo.ServerType;
                req.ServerMode                 = serverDbInfo.ServerMode;
                req.ServerLocation             = serverDbInfo.ServerLocation;
                req.ServerVersion              = serverDbInfo.ServerVersion;
                req.DatabaseName               = Crypto.SHA256(serverDbInfo.DatabaseName);
                req.DatabaseCompatibilityLevel = serverDbInfo.DatabaseCompatibilityLevel;
                if ((globalOptions.DefaultDaxFormatStyle == DaxStudio.Interfaces.Enums.DaxFormatStyle.ShortLine && !formatAlternateStyle)
                    ||
                    (globalOptions.DefaultDaxFormatStyle == DaxStudio.Interfaces.Enums.DaxFormatStyle.LongLine && formatAlternateStyle)
                    )
                {
                    req.MaxLineLenght = 1;
                }

                var data = JsonConvert.SerializeObject(req);

                var enc   = System.Text.Encoding.UTF8;
                var data1 = enc.GetBytes(data);

                // this should allow DaxFormatter to work through http 1.0 proxies
                // see: http://stackoverflow.com/questions/566437/http-post-returns-the-error-417-expectation-failed-c
                //System.Net.ServicePointManager.Expect100Continue = false;



                await PrimeConnectionAsync(uri, globalOptions, eventAggregator);

                Uri    originalUri = new Uri(uri);
                string actualUrl   = new UriBuilder(originalUri.Scheme, redirectHost, originalUri.Port, originalUri.PathAndQuery).ToString();

                //var webRequestFactory = IoC.Get<WebRequestFactory>();
                var webRequestFactory = await WebRequestFactory.CreateAsync(globalOptions, eventAggregator);

                var wr = webRequestFactory.Create(new Uri(actualUrl));

                wr.Timeout     = globalOptions.DaxFormatterRequestTimeout.SecondsToMilliseconds();
                wr.ContentType = "application/json";
                wr.Method      = "POST";
                wr.Accept      = "application/json, text/javascript, */*; q=0.01";
                wr.Headers.Add("Accept-Encoding", "gzip,deflate");
                wr.Headers.Add("Accept-Language", "en-US,en;q=0.8");
                wr.ContentType            = "application/json; charset=UTF-8";
                wr.AutomaticDecompression = DecompressionMethods.GZip;

                string output = "";
                using (var strm = await wr.GetRequestStreamAsync())
                {
                    strm.Write(data1, 0, data1.Length);

                    using (var resp = wr.GetResponse())
                    {
                        //var outStrm = new System.IO.Compression.GZipStream(resp.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                        var outStrm = resp.GetResponseStream();
                        using (var reader = new System.IO.StreamReader(outStrm))
                        {
                            output = await reader.ReadToEndAsync();
                        }
                    }
                }

                return(output);
            }
            catch (Exception ex)
            {
                Log.Error("{class} {method} {message}", "DaxFormatter", "CallDaxFormatterAsync", ex.Message);
                throw;
            }
            finally
            {
                Log.Debug("{class} {method}", "DaxFormatter", "CallDaxFormatterAsync:End");
            }
        }