Example #1
0
        private string DownloadJson(string jsonAddress, SpotifyAddressContactType spotifyAddressContactType)
        {
            using (WebClientWithShortTimeout jsonWebClient = new WebClientWithShortTimeout())
            {
                try
                {
                    // Authorization uses POST instead of GET
                    bool   usePostMethodInsteadOfGet = false;
                    string postParameters            = string.Empty;

                    // Modify HTTP headers based on what's being contacted
                    switch (spotifyAddressContactType)
                    {
                    case SpotifyAddressContactType.Authorization:
                        usePostMethodInsteadOfGet = true;
                        postParameters            = "grant_type=client_credentials";
                        jsonWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                        jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Basic {0}", ApplicationKeys.Spotify));
                        break;

                    case SpotifyAddressContactType.API:
                        jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Bearer {0}", this.authorizationToken));
                        break;

                    default:
                        break;
                    }

                    // Let's be respectful and identify ourself
                    jsonWebClient.Headers.Add("User-Agent", "Snip/" + AssemblyInformation.AssemblyVersion);

                    jsonWebClient.Encoding = Encoding.UTF8;

                    string downloadedJson = string.Empty;
                    if (usePostMethodInsteadOfGet)
                    {
                        downloadedJson = jsonWebClient.UploadString(jsonAddress, "POST", postParameters);
                    }
                    else
                    {
                        downloadedJson = jsonWebClient.DownloadString(jsonAddress);
                    }

                    if (!string.IsNullOrEmpty(downloadedJson))
                    {
                        return(downloadedJson);
                    }
                    else
                    {
                        return(string.Empty);
                    }
                }
                catch (WebException)
                {
                    return(string.Empty);
                }
            }
        }
Example #2
0
        private void SpotifyPlayerControl(SpotifyPlayerControlType controlType)
        {
            try
            {
                using (WebClientWithShortTimeout webClient = new WebClientWithShortTimeout())
                {
                    string urlAddress = "https://api.spotify.com/v1/me/player/";
                    string methodType = string.Empty;

                    switch (controlType)
                    {
                    case SpotifyPlayerControlType.Play:
                        urlAddress += "play";
                        methodType  = "PUT";
                        break;

                    case SpotifyPlayerControlType.Pause:
                        urlAddress += "pause";
                        methodType  = "PUT";
                        break;

                    case SpotifyPlayerControlType.NextTrack:
                        urlAddress += "next";
                        methodType  = "POST";
                        break;

                    case SpotifyPlayerControlType.PreviousTrack:
                        urlAddress += "previous";
                        methodType  = "POST";
                        break;

                    default:
                        break;
                    }

                    webClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Bearer {0}", this.authorizationToken));
                    webClient.Headers.Add("User-Agent", "Snip/" + AssemblyInformation.AssemblyVersion);
                    webClient.Encoding = Encoding.UTF8;

                    webClient.UploadString(urlAddress, methodType, string.Empty);
                }
            }
            catch
            {
                // If you send a request to pause the track, or play the track, when it is already paused or playing
                // it will send a 403 Forbidden. This will silently ignore the exception.
            }
        }
Example #3
0
        private string DownloadJson(string jsonAddress, SpotifyAddressContactType spotifyAddressContactType)
        {
            using (WebClientWithShortTimeout jsonWebClient = new WebClientWithShortTimeout())
            {
                try
                {
                    // Authorization uses POST instead of GET
                    bool   usePostMethodInsteadOfGet = false;
                    string postParameters            = string.Empty;

                    // Modify HTTP headers based on what's being contacted
                    switch (spotifyAddressContactType)
                    {
                    case SpotifyAddressContactType.Authorization:
                        usePostMethodInsteadOfGet = true;
                        postParameters            = string.Format(
                            CultureInfo.InvariantCulture,
                            "grant_type=authorization_code&code={0}&redirect_uri={1}",
                            this.authorizationCode,
                            this.callbackAddress);
                        jsonWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                        jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Basic {0}", ApplicationKeys.Spotify));
                        break;

                    case SpotifyAddressContactType.AuthorizationRefresh:
                        usePostMethodInsteadOfGet = true;
                        postParameters            = string.Format(
                            CultureInfo.InvariantCulture,
                            "grant_type=refresh_token&refresh_token={0}",
                            this.refreshToken);
                        jsonWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                        jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Basic {0}", ApplicationKeys.Spotify));
                        break;

                    case SpotifyAddressContactType.API:
                        jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Bearer {0}", this.authorizationToken));
                        break;

                    default:
                        break;
                    }

                    // Let's be respectful and identify ourself
                    jsonWebClient.Headers.Add("User-Agent", "Snip/" + AssemblyInformation.AssemblyVersion);

                    jsonWebClient.Encoding = Encoding.UTF8;

                    string downloadedJson = string.Empty;
                    if (usePostMethodInsteadOfGet)
                    {
                        downloadedJson = jsonWebClient.UploadString(jsonAddress, "POST", postParameters);
                    }
                    else
                    {
                        downloadedJson = jsonWebClient.DownloadString(jsonAddress);
                    }

                    if (!string.IsNullOrEmpty(downloadedJson))
                    {
                        return(downloadedJson);
                    }
                    else
                    {
                        return(string.Empty);
                    }
                }
                catch (WebException webException)
                {
                    WebResponse         webResponse         = webException.Response;
                    WebHeaderCollection webHeaderCollection = webResponse.Headers;

                    for (int i = 0; i < webHeaderCollection.Count; i++)
                    {
                        if (webHeaderCollection.GetKey(i).ToUpperInvariant() == "RETRY-AFTER")
                        {
                            // Set the timer to the retry seconds. Plus 1 for safety.
                            this.updateSpotifyTrackInformation.Enabled  = false;
                            this.updateSpotifyTrackInformation.Interval = (Double.Parse(webHeaderCollection.Get(i) + 1, CultureInfo.InvariantCulture)) * 1000;
                            this.updateSpotifyTrackInformation.Enabled  = true;
                        }
                    }

                    return(string.Empty);
                }
            }
        }