コード例 #1
0
 protected static void OnApiActionExecuted(SoundCloudEventArgs e)
 {
     if (ApiActionExecuted != null)
     {
         ApiActionExecuted(null, e);
     }
 }
コード例 #2
0
 protected static void OnApiActionExecuted(SoundCloudEventArgs e)
 {
     if (ApiActionExecuted != null)
         ApiActionExecuted(null, e);
 }
コード例 #3
0
        /// <summary>
        /// Executes an api action.
        /// </summary>
        ///
        /// <param name="uri">Uri of the api command</param>
        /// <param name="method">Http method. <seealso cref="HttpMethod"/>.</param>
        /// <param name="requireAuthentication">The action requires an authentication or not.</param>
        ///
        /// <returns>An object returned back from the api action.</returns>
        public static T ApiAction <T>(Uri uri, HttpMethod method = HttpMethod.Get, bool requireAuthentication = true)
        {
            Uri api = uri;

            if (requireAuthentication)
            {
                if (SoundCloudAccessToken == null)
                {
                    if (string.IsNullOrEmpty(SoundCloudClientID))
                    {
                        throw new SoundCloudException("Please authenticate using the SoundCloudClient class contructor before making an API call");
                    }
                    // try an unauthenticated request
                    api = uri.UriWithClientID(SoundCloudClientID);
                }
                else
                {
                    api = uri.UriWithAuthorizedUri(SoundCloudAccessToken.AccessToken);
                }
            }

            var request = WebRequest.Create(api);

            request.Method = method.ToString().ToUpperInvariant();

            // Force returned type to JSON
            request.ContentType = "application/json";

            if (method == HttpMethod.Put)
            {
                request.ContentLength = 0;
            }

            HttpWebResponse response = null;

            try
            {
                //OnApiActionExecuting Event
                OnApiActionExecuting(EventArgs.Empty);

                response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created)
                {
                    var stream = response.GetResponseStream();

                    string json;

                    using (var reader = new StreamReader(stream))
                    {
                        json = reader.ReadToEnd();
                    }

                    var args = new SoundCloudEventArgs {
                        RawResponse = json, ReturnedType = typeof(T)
                    };

                    //OnApiActionExecuted Event
                    OnApiActionExecuted(args);

                    return(JsonSerializer.Deserialize <T>(json));
                }
                else if (response.StatusCode == HttpStatusCode.NotFound || response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    //OnApiActionError Event
                    OnApiActionError(EventArgs.Empty);
                }
            }

            catch (SoundCloudException e)
            {
                throw new SoundCloudException(string.Format("{0} : {1}", response.StatusCode, response.StatusDescription));
            }

            return(default(T));
        }
コード例 #4
0
        /// <summary>
        /// Executes an api action.
        /// </summary>
        ///
        /// <param name="uri">Uri of the api command</param>
        /// <param name="method">Http method. <seealso cref="HttpMethod"/>.</param>
        /// <param name="requireAuthentication">The action requires an authentication or not.</param>
        /// <returns>An object returned back from the api action.</returns>
        public static T ApiAction <T>(Uri uri, HttpMethod method = HttpMethod.Get, bool requireAuthentication = true)
        {
            Uri api = uri;

            if (requireAuthentication)
            {
                if (SoundCloudAccessToken == null)
                {
                    if (string.IsNullOrEmpty(SoundCloudClientID))
                    {
                        throw new SoundCloudException("Please authenticate using the SoundCloudClient class contructor before making an API call");
                    }
                    // try an unauthenticated request
                    api = uri.UriWithClientID(SoundCloudClientID);
                }
                else
                {
                    api = uri.UriWithAuthorizedUri(SoundCloudAccessToken.AccessToken);
                }
            }

            var request = WebRequest.Create(api);

            request.Method = method.ToString().ToUpperInvariant();

            // Force returned type to JSON
            request.ContentType   = "application/json";
            request.ContentLength = 0;

            //add gzip enabled header
            if (EnableGZip)
            {
                request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
            }
            request.ContentLength = 0;

            HttpWebResponse response = null;

            try
            {
                //OnApiActionExecuting Event
                OnApiActionExecuting(EventArgs.Empty);

                response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created)
                {
                    var stream = response.GetResponseStream();

                    //check for gzipped response and unzip it
                    try
                    {
                        if (response.Headers[HttpResponseHeader.ContentEncoding].Equals("gzip") ||
                            response.Headers[HttpResponseHeader.ContentEncoding].Equals("deflate"))
                        {
                            stream = new GZipStream(stream, CompressionMode.Decompress);
                        }
                    }
                    catch (Exception) { /* no ziped response found, return to normal */ }


                    string json;

                    using (var reader = new StreamReader(stream))
                    {
                        json = reader.ReadToEnd();
                    }

                    //close stream
                    stream.Close();

                    var args = new SoundCloudEventArgs {
                        RawResponse = json, ReturnedType = typeof(T)
                    };

                    //OnApiActionExecuted Event
                    OnApiActionExecuted(args);

                    return(JsonSerializer.Deserialize <T>(json));
                }
                else if (response.StatusCode == HttpStatusCode.NotFound || response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    //OnApiActionError Event
                    OnApiActionError(EventArgs.Empty);
                }
            }

            catch (SoundCloudException e)
            {
                throw new SoundCloudException(string.Format("{0} : {1}", response.StatusCode, response.StatusDescription));
            }

            return(default(T));
        }