/// <summary>
        /// Try perform authorization
        /// </summary>
        /// <param name="yandexConfig"></param>
        /// <param name="allowRunWithoutAuth">If false will throw error if we cant authorize</param>
        /// <returns>Task represent current async operation</returns>
        /// <exception cref="AuthenticationException">Will be thrown if we cant authorize and allowRunWithoutAuth is false</exception>
        public static async Task AuthorizeAsync(this IYandexConfig yandexConfig, bool allowRunWithoutAuth = true)
        {
            if (yandexConfig.YandexToken != null)
            {
                if (await YandexMusicAuth.ValidateTokenAsync(yandexConfig.YandexToken, yandexConfig))
                {
                    return;
                }
            }

            if (yandexConfig.YandexLogin == null || yandexConfig.YandexPassword == null)
            {
                if (allowRunWithoutAuth)
                {
                    return;
                }
                throw new AuthenticationException("Unable to obtain token. Credentials are null.");
            }

            if (YandexMusicAuth.Login(yandexConfig.YandexLogin, yandexConfig.YandexPassword, out string?token, yandexConfig))
            {
                yandexConfig.YandexToken = token;
                yandexConfig.Save();
            }
            else
            {
                if (allowRunWithoutAuth)
                {
                    return;
                }
                throw new AuthenticationException("Unable to obtain token. Credentials are wrong.");
            }
        }
        public YandexTestBase()
        {
            if (File.Exists("TestData.json"))
            {
                Config = new FileYandexConfig("TestData.json");
            }
            else
            {
                Config = new EnvironmentConfig();
            }

            MainResolver = new YandexMusicMainResolver(Config);
        }
        /// <summary>
        /// Try perform authorization
        /// </summary>
        /// <param name="yandexConfig"></param>
        /// <param name="allowRunWithoutAuth">If false will throw error if we cant authorize</param>
        /// <returns>Task represent current async operation</returns>
        /// <exception cref="AuthenticationException">Will be thrown if we cant authorize and allowRunWithoutAuth is false</exception>
        public static async Task AuthorizeAsync(this IYandexConfig yandexConfig, bool allowRunWithoutAuth = true)
        {
            if (yandexConfig.YandexToken != null)
            {
                if (await YandexMusicAuth.ValidateTokenAsync(yandexConfig.YandexToken, yandexConfig))
                {
                    return;
                }
            }

            if (yandexConfig.YandexLogin == null || yandexConfig.YandexPassword == null)
            {
                if (allowRunWithoutAuth)
                {
                    return;
                }
                throw new AuthenticationException("Unable to obtain token. Credentials are null.");
            }

            try {
                yandexConfig.YandexToken = await YandexMusicAuth.LoginAsync(yandexConfig.YandexLogin, yandexConfig.YandexPassword, yandexConfig);

                yandexConfig.Save();
            }
            catch (InvalidCredentialException e) {
                if (allowRunWithoutAuth)
                {
                    return;
                }
                throw new AuthenticationException("Unable to obtain token. Credentials are wrong.", e);
            }
            catch (Exception) {
                if (!allowRunWithoutAuth)
                {
                    throw;
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="YandexMusicTrackLoader"/> class.
 /// </summary>
 /// <param name="config">Config instance for performing requests</param>
 public YandexMusicTrackLoader(IYandexConfig config)
 {
     config.Load();
     Config = config;
 }
Exemple #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="YandexMusicDirectUrlLoader"/> class.
 /// </summary>
 /// <param name="config">Config instance for performing requests</param>
 public YandexMusicDirectUrlLoader(IYandexConfig config)
 {
     config.Load();
     _config = config;
 }
Exemple #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="YandexMusicPlaylistLoader"/> class.
 /// </summary>
 /// <param name="config">Config instance for performing requests</param>
 /// <param name="trackLoader">Instance of <see cref="YandexMusicTrackLoader"/> for resolving some strange playlists</param>
 public YandexMusicPlaylistLoader(IYandexConfig config, IYandexMusicTrackLoader trackLoader)
 {
     _trackLoader = trackLoader;
     _config      = config;
 }
Exemple #7
0
 public YandexMusicPlaylistLoader(IYandexConfig config)
 {
     _config = config;
 }
Exemple #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="YandexMusicSearchResultLoader"/> class.
 /// </summary>
 /// <param name="config">Config instance for performing requests</param>
 /// <param name="playlistLoader">Playlist loader instance for resolving albums and playlists</param>
 public YandexMusicSearchResultLoader(IYandexConfig config, IYandexMusicPlaylistLoader playlistLoader)
 {
     _playlistLoader = playlistLoader;
     config.Load();
     _config = config;
 }