/// <summary> /// Get a PowerBI instance corresponding to a configuration. /// </summary> /// static PowerBIClient Get(IPowerBIConfiguration powerBIConfiguration) { lock (SyncRoot) { return(new PowerBIClient(powerBIConfiguration)); } }
/// <summary> /// Authenticate. /// </summary> void Authenticate() { #if !PCL if (RootConfiguration == null) { RootConfiguration = (PowerBIConfiguration)ConfigurationManager.GetSection(typeof(PowerBIConfiguration).Name); } #endif if (AuthenticationContext == null) { var tokenCache = new TokenCache(); AuthenticationContext = new AuthenticationContext(Configuration.Authority, tokenCache); } //For PCL we need to use the ADAL 3.O alpha. Because this version isn't a release we use compilation //condition to not use it in the classic version (PCL version of the PowerBI.Api.Client is also a Pre-Release) //We use synchronous call (We be change in the next version of the library) #if !PCL var authResult = string.IsNullOrEmpty(AccessToken) ? AuthenticationContext.AcquireToken(Configuration.Resource, Configuration.Client, new UserCredential(Configuration.User, Configuration.Password)) : AuthenticationContext.AcquireTokenSilent(Configuration.Resource, Configuration.Client); AccessToken = authResult.AccessToken; #else var task = string.IsNullOrEmpty(AccessToken) ? AuthenticationContext.AcquireTokenAsync(Configuration.Resource, Configuration.Client, new UserCredential(Configuration.User, Configuration.Password)) : AuthenticationContext.AcquireTokenSilentAsync(Configuration.Resource, Configuration.Client); Task.WaitAll(task); AccessToken = task.Result.AccessToken; #endif }
/// <summary> /// Do the specified function for a specific configuration. /// </summary> /// <param name="function">Function.</param> /// <param name="configuration">Configuration</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static T Do <T>(IPowerBIConfiguration configuration, Func <PowerBIClient, T> function) { var api = Get(configuration); api.Authenticate(); return(function(api)); }
/// <summary> /// Do the specified action for a specific configuration. /// </summary> /// <param name="action">Action.</param> /// <param name="configuration">Configuration</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static void Do(IPowerBIConfiguration configuration, Action <PowerBIClient> action) { var api = Get(configuration); api.Authenticate(); action(api); }
/// <summary> /// Initialize the specified powerBIConfiguration. /// </summary> /// <param name="powerBIConfiguration">Power BI configuration.</param> public static void Initialize(IPowerBIConfiguration powerBIConfiguration) { if (powerBIConfiguration == null) { throw new ArgumentNullException("powerBIConfiguration"); } RootConfiguration = powerBIConfiguration; }
/// <summary> /// Initializes a new instance of the PowerBI class. /// </summary> /// <param name="configuration">Configuration.</param> PowerBIClient(IPowerBIConfiguration configuration) { Configuration = configuration; }
/// <summary> /// Do the specified function for a specific configuration. /// </summary> /// <param name="function">Function.</param> /// <param name="configuration">Configuration</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public async static Task <T> DoAsync <T>(IPowerBIConfiguration configuration, Func <PowerBIClient, T> function) { //Lazy implementation... return(await Task.Run <T>(() => Do <T>(configuration, function))); }
/// <summary> /// Do the specified action for a specific configuration. /// </summary> /// <param name="action">Action.</param> /// <param name="configuration">Configuration</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public async static Task DoAsync(IPowerBIConfiguration configuration, Action <PowerBIClient> action) { //Lazy implementation... await Task.Run(() => Do(configuration, action)); }