private static async Task <OAuthCredentials> GetSpotifyCredentials(string PostBody, string clientId, string clientSecret, string refreshToken = null)
        {
            string             b64     = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{clientId}:{clientSecret}"));
            HttpRequestMessage request = WebRequestHelpers.SetupRequest(SpotifyAPITokenUrl, "Basic", b64, HttpMethod.Post);
            HttpContent        content = new StringContent(PostBody, Encoding.UTF8, "application/x-www-form-urlencoded");

            request.Content = content;
            HttpResponseMessage response = await WebRequestHelpers.Client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                string jsonstring = await response.Content.ReadAsStringAsync();

                JObject jobj        = Newtonsoft.Json.Linq.JObject.Parse(jsonstring);
                string  accessToken = jobj.GetValue("access_token").ToString();
                int     expiresIn   = Convert.ToInt32(jobj.GetValue("expires_in").ToString());
                if (string.IsNullOrWhiteSpace(refreshToken))
                {
                    refreshToken = jobj.GetValue("refresh_token").ToString();
                }
                return(new OAuthCredentials("unknown user", accessToken, expiresIn, refreshToken));
            }
            else
            {
                return(OAuthCredentials.CrendentialError);
            }
        }
        /// <summary>
        /// JToken Constructor
        /// </summary>
        /// <param name="token"></param>
        public CursorBasedPaging(JToken token)
        {
            Href  = token.Value <string>("href") ?? string.Empty;
            Limit = token.Value <int?>("limit") ?? 20;
            Next  = token.Value <string>("next") ?? string.Empty;
            Total = token.Value <int?>("total") ?? 0;

            JToken cobj = token.Value <JToken>("cursors");

            if (cobj != null)
            {
                Cursors = new Cursor(cobj);
            }

            Func <JObject, ISpotifyObject> generator = WebRequestHelpers.CreateSpotifyObjectGenerator(typeof(T));

            JArray jarr = token.Value <JArray>("items");

            if (jarr != null)
            {
                List <T> lst = new List <T>();
                foreach (JObject jobj in jarr)
                {
                    T item = (T)generator(jobj);
                    lst.Add(item);
                }
                Items = lst;
            }
        }
示例#3
0
        /// <summary>
        /// Download data list from its parameter type
        /// </summary>
        /// <typeparam name="T">The data type</typeparam>
        /// <param name="converter">JsonConverter for data type</param>
        /// <returns>Downloaded data list</returns>
        private List <T> GetData <T>(JsonConverter converter)
        {
            string url  = GetUrl <T>();
            string data = WebRequestHelpers.DownloadString(url);

            return(JsonConvert.DeserializeObject <List <T> >(data, new[] { converter }));
        }
示例#4
0
        /// <summary>
        /// Download data from its type
        /// </summary>
        /// <typeparam name="T">The data type</typeparam>
        /// <param name="args">Arguments for data type</param>
        /// <returns>Downloaded data</returns>
        private T GetData <T>(params object[] args)
            where T : class
        {
            string url  = GetUrl <T>(args);
            string data = WebRequestHelpers.DownloadString(url);

            return(JsonConvert.DeserializeObject <T>(data));
        }
示例#5
0
 public IStatusMessage GetStatus()
 {
     try
     {
         WebRequestHelpers.DownloadString("http://www.perdu.com");
     }
     catch (Exception ex)
     {
         return(new ConnectionStatus(false, ex.Message));
     }
     return(new ConnectionStatus(true, "Connection OK"));
 }
示例#6
0
        /// <summary>
        /// Returns a new paging object with the previous {limit} number of items
        /// Throws an IndexOutOfRangeException if there are no more items
        /// </summary>
        /// <returns></returns>
        public async Task <Paging <T> > GetPrevious(string accessToken)
        {
            if (string.IsNullOrWhiteSpace(Previous) || Offset <= 0)
            {
                throw new IndexOutOfRangeException($"Offset({Offset}) already at or below 0. There are no more items left to get");
            }
            else   /* There are items left to get, so return a new paging object with that offset*/
            {
                HttpRequestMessage  message  = WebRequestHelpers.SetupRequest(this.Previous, accessToken);
                HttpResponseMessage response = await WebRequestHelpers.Client.SendAsync(message);

                if (response.IsSuccessStatusCode)
                {
                    JToken token = await WebRequestHelpers.ParseJsonResponse(response.Content);

                    Paging <T> paging = new Paging <T>(token);
                    return(paging);
                }
            }
            return(null);
        }
        /// <summary>
        /// Returns a new Cursor-Based Paging object with the next {Limit} number of items,
        /// starting after the {After} fields
        /// Throws an IndexOutOfRangeException if there are no more items
        /// </summary>
        /// <returns></returns>
        public async Task <CursorBasedPaging <T> > GetNext(string accessToken)
        {
            /* Offset is past the total - therefore there's nothing left to get */
            if (string.IsNullOrWhiteSpace(Next))
            {
                throw new IndexOutOfRangeException($"There are no more items left to get");
            }
            else   /* There are items left to get, so return a new paging object with that offset*/
            {
                HttpRequestMessage  message  = WebRequestHelpers.SetupRequest(this.Next, accessToken);
                HttpResponseMessage response = await WebRequestHelpers.Client.SendAsync(message);

                if (response.IsSuccessStatusCode)
                {
                    JToken token = await WebRequestHelpers.ParseJsonResponse(response.Content);

                    CursorBasedPaging <T> paging = new CursorBasedPaging <T>(token);
                    return(paging);
                }
            }
            return(null);
        }
示例#8
0
 public string ProvideData(string source)
 {
     return(WebRequestHelpers.DownloadString(source));
 }
示例#9
0
        private dataprovidercache CacheData(string source)
        {
            string data = WebRequestHelpers.DownloadString(source);

            return(new dataprovidercache(source, data));
        }