예제 #1
0
        async static void Example()
        {
            //getting country of user
            string name         = RegionInfo.CurrentRegion.EnglishName;
            var    analysedtext = await RazerApi.AnalyseText(File.ReadAllText("test.txt"));

            var auth = new ImplicitGrantAuth(
                "7f08980f1dae4f3d98a40d44ef235b03",
                "http://localhost:4002",
                "http://localhost:4002",
                Scope.UserReadPrivate
                );

            auth.AuthReceived += async(sender, payload) =>
            {
                auth.Stop(); // `sender` is also the auth instance
                var api = new SpotifyWebAPI()
                {
                    TokenType   = payload.TokenType,
                    AccessToken = payload.AccessToken
                };
                // FeaturedPlaylists playlists = api.GetFeaturedPlaylists();
                //Console.WriteLine(playlists.Message);
                //playlists.Playlists.Items.ForEach(playlist => Console.WriteLine(playlist.Name));

                //getting playlists from categories and outputting names and links of the playlist.
                CategoryPlaylist playlists = api.GetCategoryPlaylists("party");
                playlists.Playlists.Items.ForEach(playlist => Console.WriteLine("Playlist Name: " + playlist.Name + ",\nLink: " + playlist.Uri));

                // Do requests with API client
                var newsapiresults = await NewsApi.SearchByKeyword("bitcoin", name);

                if (newsapiresults == null)
                {
                    return;
                }
                Debug.WriteLine(newsapiresults);
                Debug.WriteLine(newsapiresults.totalResults);
                foreach (var result in newsapiresults.articles)
                {
                    //Console.WriteLine();
                    Debug.WriteLine(result.title);
                }


                var spotifyresults = await api.SearchItemsAsync("drake", SearchType.All);

                if (spotifyresults == null)
                {
                    return;
                }

                Console.WriteLine(spotifyresults);
            };

            auth.Start(); // Starts an internal HTTP Server
            auth.OpenBrowser();
        }
예제 #2
0
        async static void button2_Click(EventArgs e)
        {
            string name = RegionInfo.CurrentRegion.EnglishName;
            var analysedtext = await RazerApi.AnalyseText(File.ReadAllText("test.txt"));

            var auth = new ImplicitGrantAuth(
                  "7f08980f1dae4f3d98a40d44ef235b03",
                  "http://localhost:4002",
                  "http://localhost:4002",
                  Scope.UserReadPrivate
                );
          
                // FeaturedPlaylists playlists = api.GetFeaturedPlaylists();
                //Console.WriteLine(playlists.Message);
                //playlists.Playlists.Items.ForEach(playlist => Console.WriteLine(playlist.Name));

                //getting playlists from categories and outputting names and links of the playlist.
                CategoryPlaylist playlists = api.GetCategoryPlaylists("party");
                playlists.Playlists.Items.ForEach(playlist => Console.WriteLine("Playlist Name: " + playlist.Name + ",\nLink: " + playlist.Uri));

                // Do requests with API client
                var newsapiresults = await NewsApi.SearchByKeyword(TextBox., name);
                if (newsapiresults == null)
                    return;
                Debug.WriteLine(newsapiresults);
                Debug.WriteLine(newsapiresults.totalResults);
                foreach (var result in newsapiresults.articles)
                {
                    //Console.WriteLine();
                    Debug.WriteLine(result.title);
                }


                var spotifyresults = await api.SearchItemsAsync("drake", SearchType.All);
                if (spotifyresults == null)
                    return;

                Console.WriteLine(spotifyresults);
            };
 /// <summary>
 /// Reset the connections
 /// </summary>
 private void ResetConnections()
 {
     // clear the references
     _sApiRazerInstance  = null;
     _sApiChromaInstance = null;
 }
    /// <summary>
    /// Initialize Chroma by hitting the REST server and set the API port
    /// </summary>
    /// <returns></returns>
    void PostChromaSdk()
    {
        _sConnectionIsActive = true;

        //LogOnMainThread("PostChromaSdk:");
        bool reconnect = false;

        try
        {
            if (null != _sApiRazerInstance)
            {
                return;
            }

            // use the Razer API to get the session
            _sApiRazerInstance = new RazerApi();

            if (null == _mInfo)
            {
                SetupDefaultInfo();
            }

            PostChromaSdkResponse result = null;
            DateTime timeout             = DateTime.Now + TimeSpan.FromSeconds(5);
            Thread   thread = new Thread(new ThreadStart(() =>
            {
                try
                {
                    ConnectionStatus = VERSION_CHECK;
                    GetChromaSdkResponse getResult = _sApiRazerInstance.GetChromaSdk();

                    if (null == getResult ||
                        string.IsNullOrEmpty(getResult.Version))
                    {
                        ConnectionStatus = RECONNECT_VERSION_IS_NULL;
                        reconnect        = true;
                        return;
                    }

                    string[] parts = getResult.Version.Split(".".ToCharArray());
                    if (parts.Length != 3)
                    {
                        ConnectionStatus = RECONNECT_VERSION_IS_UNKNOWN;
                        reconnect        = true;
                        return;
                    }
                    int[] version = new int[3];
                    if (int.TryParse(parts[0], out version[0]) &&
                        int.TryParse(parts[1], out version[1]) &&
                        int.TryParse(parts[2], out version[2]))
                    {
                        if (version[0] >= 2 ||
                            (version[0] == 2 &&
                             version[1] >= 3) ||
                            (version[0] == 2 &&
                             version[1] == 3 &&
                             version[2] >= 6))
                        {
                            //good!
                        }
                        else
                        {
                            ConnectionStatus = RECONNECT_SYNAPSE_OUTDATED;
                            reconnect        = true;
                        }
                    }
                    else
                    {
                        ConnectionStatus = RECONNECT_VERSION_IS_UNKNOWN;
                        reconnect        = true;
                    }
                }
                catch (Exception)
                {
                    ConnectionStatus = RECONNECT_SERVER_UNREACHABLE;
                    reconnect        = true;
                }

                if (!reconnect)
                {
                    try
                    {
                        //LogOnMainThread("Initializing...");
                        ConnectionStatus = CONNECTING;
                        result           = _sApiRazerInstance.PostChromaSdk(_mInfo);
                    }
                    catch (Exception)
                    {
                        ConnectionStatus = RECONNECT_SERVER_UNREACHABLE;
                        reconnect        = true;
                    }
                }
            }));
            thread.Start();
            while (_sWaitForExit &&
                   DateTime.Now < timeout &&
                   thread.IsAlive)
            {
                Thread.Sleep(0);
            }
            if (_sWaitForExit &&
                timeout < DateTime.Now &&
                thread.IsAlive)
            {
                //Debug.LogError("Connect: Timeout detected!");
                thread.Abort();
                reconnect        = true;
                ConnectionStatus = RECONNECT_RAZER_API_TIMEOUT;
                ThreadWaitForSecond();
            }

            if (_sWaitForExit)
            {
                if (null != result)
                {
                    //LogOnMainThread(result);

                    // setup the api instance with the session uri
                    _sApiChromaInstance = new ChromaApi(result.Uri);

                    //LogOnMainThread("Init complete.");

                    // use heartbeat to keep the REST API alive
                    DoHeartbeat();
                }
                else
                {
                    //Debug.LogError("Connect: Result is null!");
                    reconnect = true;
                }
            }
        }
        catch (Exception)
        {
            reconnect = true;
        }

        if (reconnect)
        {
            //LogErrorOnMainThread(string.Format("Exception when calling RazerApi.PostChromaSdk: {0}", e));
            _sApiRazerInstance = null;

            //attempt reconnect
            // Coroutines can only start from the main thread
            RunOnMainThread(() =>
            {
                if (_sWaitForExit)
                {
                    // retry
                    SafeStartCoroutine("Initialize", Initialize());
                }
            });
        }

        _sConnectionIsActive = false;
    }