public JsonRequest(string dax, FormattingCulture culture) : this( dax, culture == FormattingCulture.EU ? ';' : ',', culture == FormattingCulture.EU ? ',' : '.' ) { }
/// <summary> /// Formats a string by calling www.daxformatter.com. /// </summary> /// <param name="inputDax">input dax text</param> /// <exception cref="FormatException">Error message from www.daxformatter.com</exception> /// <returns>formatted text</returns> public static string Format(string inputDax, FormattingCulture culture) { if (redirectUrl == null) { // www.daxformatter.com redirects request to another site. HttpWebRequest does redirect with GET. It fails, since the web service works only with POST // The following 2 requests are doing manual POST re-direct var redirectRequest = WebRequest.Create(daxFormatterUrl) as HttpWebRequest; redirectRequest.AllowAutoRedirect = false; var redirectResponse = redirectRequest.GetResponse() as HttpWebResponse; redirectUrl = redirectResponse.Headers["Location"]; } using (var client = new HttpClient()) { var content = new StringContent( new JsonRequest(inputDax, culture).ToString(), Encoding.UTF8, "application/json" ); Debug.WriteLine($"Content: {content.ReadAsStringAsync().Result}"); var responseMessage = client.PostAsync(redirectUrl, content).Result; var responseString = responseMessage.Content.ReadAsStringAsync().Result; var response = GetValidJsonFromResponse(responseString); Debug.WriteLine($"Response: {response}"); var result = new Response(response); if (!string.IsNullOrWhiteSpace(result.Error)) { Debug.WriteLine($"Error: {result.Error}"); throw new FormatException(result.Error); } Debug.WriteLine($"Formatted DAX: {result.DAX}"); return(result.DAX); } }