public static GitHubFollowingResponse ParseResponse(SocialHttpResponse response) {

            switch (response.StatusCode) {

                case HttpStatusCode.NoContent:
                    return new GitHubFollowingResponse(response) {
                        IsFollowing = true
                    };

                case HttpStatusCode.NotFound:
                    return new GitHubFollowingResponse(response) {
                        IsFollowing = false
                    };

                default:

                    // Parse the raw JSON response
                    JsonObject obj = response.GetBodyAsJsonObject();

                    string message = obj.GetString("message");
                    string url = obj.GetString("documentation_url");
                    throw new GitHubHttpException(response, message, url);
                    
            }

        }
        /// <summary>
        /// Validates the specified <code>response</code>.
        /// </summary>
        /// <param name="response">The response to be validated.</param>
        public static void ValidateResponse(SocialHttpResponse response) {

            // Skip error checking if the server responds with an OK status code
            if (response.StatusCode == HttpStatusCode.OK) return;

            throw new Exception("WTF?\n\n" + response.Body);

        }
Exemplo n.º 3
0
        /// <summary>
        /// Validates the specified <code>response</code>.
        /// </summary>
        /// <param name="response">The response to be validated.</param>
        public static void ValidateResponse(SocialHttpResponse response) {

            // Skip error checking if the server responds with an OK status code
            if (response.StatusCode == HttpStatusCode.OK) return;

            // Otherwise throw an exception
            throw new BitBucketException(response);

        }
        /// <summary>
        /// Validates the specified <code>response</code>.
        /// </summary>
        /// <param name="response">The response to be validated.</param>
        public static void ValidateResponse(SocialHttpResponse response) {

            // Skip error checking if the server responds with an OK status code
            if (response.StatusCode == HttpStatusCode.OK) return;

            // Parse the JSON response
            PinterestError error = SocialUtils.ParseJsonObject(response.Body, PinterestError.Parse);

            // Throw the exception
            throw new PinterestException(response, error);

        }
Exemplo n.º 5
0
        /// <summary>
        /// Validates the specified <code>response</code>.
        /// </summary>
        /// <param name="response">The response to be validated.</param>
        /// <param name="obj">The object representing the response object.</param>
        public static void ValidateResponse(SocialHttpResponse response, JsonObject obj) {

            // Skip error checking if the server responds with an OK status code
            if (response.StatusCode == HttpStatusCode.OK) return;
            
            // Throw an exception based on the error message from the API
            JsonObject error = obj.GetObject("error");
            int code = error.GetInt32("code");
            string type = error.GetString("type");
            string message = error.GetString("message");
            int subcode = error.HasValue("error_subcode") ? error.GetInt32("error_subcode") : 0;
            throw new FacebookException(response, code, type, message, subcode);

        }
Exemplo n.º 6
0
        public static void ValidateResponse(SocialHttpResponse response) {

            // Skip error checking if the server responds with an OK status code
            if (response.StatusCode == HttpStatusCode.OK) return;

            // Get the "meta" object
            JsonObject obj = response.GetBodyAsJsonObject();

            // Now throw some exceptions
            string message = obj.GetString("message");
            string url = obj.GetString("documentation_url");
            throw new GitHubHttpException(response, message, url);

        }
Exemplo n.º 7
0
        /// <summary>
        /// Validates the specified <code>response</code>.
        /// </summary>
        /// <param name="response">The response to be validated.</param>
        /// <param name="obj">The object representing the response object.</param>
        public static void ValidateResponse(SocialHttpResponse response, JsonObject obj) {

            // Skip error checking if the server responds with an OK status code
            if (response.StatusCode == HttpStatusCode.OK) return;

            JsonObject error = obj.GetObject("error");

            int code = error.GetInt32("code");
            string message = error.GetString("message");

            // TODO: Parse "errors"

            throw new AnalyticsException(response, code, message);

        }
Exemplo n.º 8
0
        /// <summary>
        /// Validates the specified <code>response</code>.
        /// </summary>
        /// <param name="response">The response to be validated.</param>
        public static void ValidateResponse(SocialHttpResponse response) {

            // The Slack API will always return a "200 OK" status even when an error is returned, so we need to check
            // the "ok" property in the boolean instead

            // Get root object
            JsonObject obj = response.GetBodyAsJsonObject();

            // Is the request/response successful?
            bool isOk = obj.GetBoolean("ok");
                
            // Now throw some exceptions
            if (!isOk) throw new SlackException(response, obj.GetString("error"));

        }
Exemplo n.º 9
0
        /// <summary>
        /// Validates the specified <code>response</code>.
        /// </summary>
        /// <param name="response">The response to be validated.</param>
        public static void ValidateResponse(SocialHttpResponse response) {

            // Skip error checking if the server responds with an OK status code
            if (response.StatusCode == HttpStatusCode.OK) return;

            // Get the "meta" object
            JsonObject obj = response.GetBodyAsJsonObject();

            // Now throw some exceptions
            throw new Exception("WTF?");
            //int code = obj.GetInt32("code");
            //string type = obj.GetString("error_type");
            //string message = obj.GetString("error_message") ?? obj.GetString("error");
            //throw new BasecampException(response, code, type, message);

        }
Exemplo n.º 10
0
        public static TwitterRateLimiting GetFromResponse(SocialHttpResponse response) {

            int limit;
            int remaining;
            int reset;

            if (!Int32.TryParse(response.Headers["x-rate-limit-limit"] ?? "", out limit)) {
                limit = -1;
            }

            if (!Int32.TryParse(response.Headers["x-rate-limit-remaining"] ?? "", out remaining)) {
                remaining = -1;
            }

            if (!Int32.TryParse(response.Headers["x-rate-limit-reset"] ?? "", out reset)) {
                reset = 0;
            }

            return new TwitterRateLimiting(limit, remaining, reset);

        }
Exemplo n.º 11
0
        public static void ValidateResponse(SocialHttpResponse response) {

            // Skip error checking if the server responds with an OK status code
            if (response.StatusCode == HttpStatusCode.OK) return;

            JsonObject obj = response.GetBodyAsJsonObject();

            // For some types of errors, Twitter will only respond with an error message
            if (obj.HasValue("error")) {
                throw new TwitterException(response, obj.GetString("error"), 0);
            }

            // However in most cases, Twitter responds with an array of errors
            JsonArray errors = obj.GetArray("errors");

            // Get the first error (don't remember ever seeing multiple errors in the same response)
            JsonObject error = errors.GetObject(0);

            // Throw the exception
            throw new TwitterException(response, error.GetString("message"), error.GetInt32("code"));

        }
Exemplo n.º 12
0
 internal AnalyticsException(SocialHttpResponse response, int code, string message) : base(message) {
     Response = response;
     Code = code;
 }
Exemplo n.º 13
0
 protected SocialResponse(SocialHttpResponse response) {
     Response = response;
 }
Exemplo n.º 14
0
 protected FacebookResponse(SocialHttpResponse response) : base(response) { }
Exemplo n.º 15
0
 public FacebookException(SocialHttpResponse response, int code, string type, string message, int subcode = 0) : base(message) {
     Response = response;
     Code = code;
     Type = type;
     Subcode = subcode;
 }
 protected DropboxResponse(SocialHttpResponse response) : base(response) { }
Exemplo n.º 17
0
 public BitBucketException(SocialHttpResponse response) : base("Invalid response received from the BitBucket API (Status: " + ((int) response.StatusCode) + ")") {
     Response = response;
     StatusCode = response.StatusCode;
 }
Exemplo n.º 18
0
 internal InstagramException(SocialHttpResponse response, InstagramMetaData meta) : base(meta.ErrorMessage) {
     Response = response;
     RateLimiting = InstagramRateLimiting.GetFromResponse(response);
     Meta = meta;
 }
        /// <summary>
        /// Executes the request and returns the corresponding response as an instance of <see cref="SocialHttpResponse"/>.
        /// </summary>
        /// <param name="callback">Lets you specify a callback method for modifying the underlying <see cref="HttpWebRequest"/>.</param>
        /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the response.</returns>
        public SocialHttpResponse GetResponse(Action <HttpWebRequest> callback)
        {
            // Build the URL
            string url = Url;

            if (QueryString != null && !QueryString.IsEmpty)
            {
                url += (url.Contains("?") ? "&" : "?") + QueryString;
            }

            // Initialize the request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            // Misc
            request.Method      = Method.ToString().ToUpper();
            request.Credentials = Credentials;
            request.Headers     = Headers.Headers;
            request.Accept      = Accept;
            request.Referer     = Referer;
            request.UserAgent   = UserAgent;
            request.Timeout     = (int)Timeout.TotalMilliseconds;
            if (!String.IsNullOrWhiteSpace(Host))
            {
                request.Host = Host;
            }

            // Add the request body (if a POST request)
            if (Method == SocialHttpMethod.Post)
            {
                if (IsMultipart)
                {
                    string boundary = Guid.NewGuid().ToString().Replace("-", "");
                    request.ContentType = "multipart/form-data; boundary=" + boundary;
                    using (Stream stream = request.GetRequestStream()) {
                        PostData.WriteMultipartFormData(stream, boundary);
                    }
                }
                else
                {
                    string dataString = PostData.ToString();
                    request.ContentType   = "application/x-www-form-urlencoded";
                    request.ContentLength = dataString.Length;
                    using (Stream stream = request.GetRequestStream()) {
                        stream.Write(Encoding.UTF8.GetBytes(dataString), 0, dataString.Length);
                    }
                }
            }

            // Call the callback
            if (callback != null)
            {
                callback(request);
            }

            // Get the response
            try {
                return(SocialHttpResponse.GetFromWebResponse(request.GetResponse() as HttpWebResponse, this));
            } catch (WebException ex) {
                if (ex.Status != WebExceptionStatus.ProtocolError)
                {
                    throw;
                }
                return(SocialHttpResponse.GetFromWebResponse(ex.Response as HttpWebResponse, this));
            }
        }
Exemplo n.º 20
0
 protected YouTubeResponse(SocialHttpResponse response) {
     Response = response;
 }
 /// <summary>
 /// Initializes a new instance from the specified <code>response</code> and <code>body</code>.
 /// </summary>
 /// <param name="response">The raw response.</param>
 /// <param name="body">The object representing the response body.</param>
 protected SocialOAuthAccessTokenResponse(SocialHttpResponse response, SocialOAuthAccessToken body) : base(response) {
     Body = body;
 }
Exemplo n.º 22
0
 public GitHubHttpException(SocialHttpResponse response, string message)
     : base(message) {
     Response = response;
 }
Exemplo n.º 23
0
 protected GitHubResponse(SocialHttpResponse response) {
     RateLimit = Int32.Parse(response.Headers["X-RateLimit-Limit"]);
     RateLimitRemaining = Int32.Parse(response.Headers["X-RateLimit-Remaining"]);
     RateLimitReset = SocialUtils.GetDateTimeFromUnixTime(response.Headers["X-RateLimit-Reset"]);
     Response = response;
 }
 /// <summary>
 /// Initializes a new instance from the specified <code>response</code> and <code>body</code>.
 /// </summary>
 /// <param name="response">The raw response.</param>
 /// <param name="body">The object representing the response body.</param>
 protected SocialOAuthRequestTokenResponse(SocialHttpResponse response, SocialOAuthRequestToken body) : base(response) {
     Body = body;
 }
 public InstagramOAuthException(SocialHttpResponse response, InstagramMetaData meta) : base(response, meta) { }
 /// <summary>
 /// Initializes a new instance from the specified <code>response</code> and <code>body</code>.
 /// </summary>
 /// <param name="response">The raw response.</param>
 /// <param name="body">The object representing the response body.</param>
 /// <returns>Returns an instance of <see cref="SocialOAuthRequestTokenResponse"/>.</returns>
 public static SocialOAuthRequestTokenResponse ParseResponse(SocialHttpResponse response, SocialOAuthRequestToken body) {
     return response == null ? null : new SocialOAuthRequestTokenResponse(response, body);
 }
Exemplo n.º 27
0
 protected AnalyticsResponse(SocialHttpResponse response) : base(response) { }
 private GitHubFollowingResponse(SocialHttpResponse response) : base(response) { }
Exemplo n.º 29
0
 public GitHubHttpException(SocialHttpResponse response) : base("Invalid response received from the GitHub API (Status: " + ((int) response.StatusCode) + ")") {
     Response = response;
 }
Exemplo n.º 30
0
 internal TwitterException(SocialHttpResponse response, string message, int code) : base(message) {
     Response = response;
     Code = code;
 }
Exemplo n.º 31
0
 public GitHubHttpException(SocialHttpResponse response, string message, string documentationUrl) : base(message) {
     Response = response;
     DocumentationUrl = documentationUrl;
 }