Beispiel #1
0
        /// <summary>
        /// Makes a GET request to the specified <code>url</code>.
        /// </summary>
        /// <param name="url">The URL of the request.</param>
        /// <param name="query">The query string of the request.</param>
        /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the response.</returns>
        public virtual SocialHttpResponse DoHttpGetRequest(string url, SocialQueryString query)
        {
            // Some input validation
            if (String.IsNullOrWhiteSpace(url))
            {
                throw new ArgumentNullException("url");
            }

            // Initialize the request
            SocialHttpRequest request = new SocialHttpRequest {
                Url         = url,
                QueryString = query
            };

            // Do something extra for the request
            PrepareHttpRequest(request);

            // Make the request to the specified URL
            return(request.GetResponse());
        }
Beispiel #2
0
        /// <summary>
        /// Makes a POST request to the specified <code>url</code>.
        /// </summary>
        /// <param name="url">The URL of the request.</param>
        /// <param name="query">The query string of the request.</param>
        /// <param name="postData">The POST data of the request.</param>
        /// <param name="isMultipart">Indicates the request should be encoded as <code>multipart/form-data</code>.</param>
        /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the response.</returns>
        public virtual SocialHttpResponse DoHttpPostRequest(string url, SocialQueryString query, SocialPostData postData, bool isMultipart)
        {
            // Some input validation
            if (String.IsNullOrWhiteSpace(url))
            {
                throw new ArgumentNullException("url");
            }

            // Initialize the request
            SocialHttpRequest request = new SocialHttpRequest {
                Method      = SocialHttpMethod.Post,
                Url         = url,
                QueryString = query,
                PostData    = postData,
                IsMultipart = isMultipart
            };

            // Do something extra for the request
            PrepareHttpRequest(request);

            // Make the request to the specified URL
            return(request.GetResponse());
        }
        /// <summary>
        /// Makes an authenticated GET request to the specified URL.
        /// </summary>
        /// <param name="url">The URL to call.</param>
        /// <param name="query">The query string for the call.</param>
        public SocialHttpResponse DoAuthenticatedGetRequest(string url, SocialQueryString query) {

            // Initialize a new SocialQueryString if NULL
            if (query == null) query = new SocialQueryString();
            
            // Configure the request
            SocialHttpRequest request = new SocialHttpRequest {
                Method = "GET",
                Url = url,
                QueryString = query
            };

            // Add an authorization header with the access token
            if (!query.ContainsKey("access_token") && !String.IsNullOrWhiteSpace(AccessToken)) {

                request.QueryString.Add("access_token", AccessToken);

                // Apparently not all methods support a bearer token (getting the pins of a board)
                //request.Headers.Authorization = "Bearer " + AccessToken;
            }

            // Set headers of the request

            // Make a call to the API
            return request.GetResponse();

        }
        /// <summary>
        /// Makes an authenticated GET request to the specified URL. The access token is automatically appended to the query string.
        /// </summary>
        /// <param name="url">The URL to call.</param>
        /// <param name="query">The query string for the call.</param>
        public SocialHttpResponse DoAuthenticatedGetRequest(string url, SocialQueryString query) {

            // Initialize a new NameValueCollection if NULL
            if (query == null) query = new SocialQueryString();

            // Append the access token to the query string if present in the client and not already
            // specified in the query string
            if (!query.ContainsKey("token") && !String.IsNullOrWhiteSpace(AccessToken)) {
                query.Add("token", AccessToken);
            }

            // Configure the request
            SocialHttpRequest request = new SocialHttpRequest {
                Method = "GET",
                Url = url,
                QueryString = query,
                UserAgent = "Skybrud.Social"
            };

            // Make a call to the API
            return request.GetResponse();

        }
Beispiel #5
0
 private SocialHttpResponse(SocialHttpRequest request, HttpWebResponse response)
 {
     Request  = request;
     Response = response;
     Encoding = DetectResponseEncoding();
 }
Beispiel #6
0
 /// <summary>
 /// Creates a new instance based on the specified <paramref name="response"/>.
 /// </summary>
 /// <param name="response">The instance of <see cref="HttpWebResponse"/> to be parsed.</param>
 /// <param name="request">The instance of <see cref="HttpWebRequest"/> that resulted in the response.</param>
 /// <returns>A new instance of <see cref="HttpWebResponse"/> based on the specified <paramref name="response"/>.</returns>
 public static SocialHttpResponse GetFromWebResponse(HttpWebResponse response, SocialHttpRequest request)
 {
     return(response == null ? null : new SocialHttpResponse(request, response));
 }
        /// <summary>
        /// Makes a POST request to the MailChimp API. If the <code>AccessToken</code> property has been specified, the
        /// access token will added as an authorization header of the request.
        /// </summary>
        /// <param name="url">The URL to call.</param>
        /// <param name="query">The query string of the request.</param>
        /// <param name="postData">The POST data.</param>
        /// <param name="isMultipart">If <code>true</code>, the content type of the request will be <code>multipart/form-data</code>, otherwise <code>application/x-www-form-urlencoded</code>.</param>
        /// <returns>Returns an instance of <code>SocialHttpResponse</code> wrapping the response from the MailChimp API.</returns>
        public SocialHttpResponse DoHttpPostRequest(string url, SocialQueryString query, SocialPostData postData, bool isMultipart) {

            // Throw an exception if the URL is empty
            if (String.IsNullOrWhiteSpace(url)) throw new ArgumentNullException("url");

            // Initialize a new instance of SocialQueryString if the one specified is NULL
            if (query == null) query = new SocialQueryString();

            // Append the access token to the query string if present in the client and not already
            // specified in the query string
            if (!query.ContainsKey("access_token") && !String.IsNullOrWhiteSpace(AccessToken)) {
                query.Add("access_token", AccessToken);
            }

            // Append the query string to the URL
            if (!query.IsEmpty) url += (url.Contains("?") ? "&" : "?") + query;

            // Initialize a new HTTP request
            SocialHttpRequest request = new SocialHttpRequest {
                Url = url,
                Method = "POST"
            };

            // Add the authorization header if the "AccessToken" property is specified
            if (!String.IsNullOrWhiteSpace(AccessToken)) {
                request.Authorization = "OAuth " + AccessToken;
            }

            // Get the HTTP response
            return request.GetResponse();

        }
        /// <summary>
        /// Makes a GET request to the MailChimp API. If the <code>AccessToken</code> property has been specified, the
        /// access token will added as an authorization header of the request.
        /// </summary>
        /// <param name="url">The URL to call.</param>
        /// <param name="query">The query string of the request.</param>
        /// <returns>Returns an instance of <code>SocialHttpResponse</code> wrapping the response from the MailChimp API.</returns>
        public SocialHttpResponse DoHttpGetRequest(string url, SocialQueryString query) {

            // Throw an exception if the URL is empty
            if (String.IsNullOrWhiteSpace(url)) throw new ArgumentNullException("url");

            // Initialize a new instance of SocialQueryString if the one specified is NULL
            if (query == null) query = new SocialQueryString();

            if (!String.IsNullOrWhiteSpace(ApiKey) && !query.ContainsKey("apikey")) {
                query.Add("apikey", ApiKey);
            }

            // Append the query string to the URL
            if (!query.IsEmpty) url += (url.Contains("?") ? "&" : "?") + query;

            // Append the API endpoint
            if (url.StartsWith("/")) {
                if (String.IsNullOrWhiteSpace(ApiEndpoint)) throw new PropertyNotSetException("ApiEndpoint");
                url = ApiEndpoint + url;
            }

            // Initialize a new HTTP request
            SocialHttpRequest request = new SocialHttpRequest {
                Url = url
            };

            // Add the authorization header if the "AccessToken" property is specified
            if (!String.IsNullOrWhiteSpace(AccessToken) && !query.ContainsKey("apikey")) {
                request.Authorization = "OAuth " + AccessToken;
            }

            // Get the HTTP response
            return request.GetResponse();

        }
Beispiel #9
0
 /// <summary>
 /// Virtual method that can be used for configuring a request.
 /// </summary>
 /// <param name="request">The request.</param>
 protected virtual void PrepareHttpRequest(SocialHttpRequest request)
 {
 }
 /// <summary>
 /// Creates a new instance based on the specified <code>response</code>.
 /// </summary>
 /// <param name="response">The instance of <see cref="HttpWebResponse"/> to be parsed.</param>
 /// <param name="request">The instance of <see cref="HttpWebRequest"/> that resulted in the response.</param>
 /// <returns>Returns a new instance of <see cref="HttpWebResponse"/> based on the specified <code>response</code>.</returns>
 public static SocialHttpResponse GetFromWebResponse(HttpWebResponse response, SocialHttpRequest request) {
     return response == null ? null : new SocialHttpResponse(request, response);
 }
        /// <summary>
        /// Adds the OAuth 1.0a authorization header to the request
        /// </summary>
        /// <param name="request"></param>
        protected override void PrepareHttpRequest(SocialHttpRequest request) {

            // Generate the signature
            string signature = GenerateSignature(request);

            // Generate the header
            string header = GenerateHeaderString(signature);

            // Add the authorization header
            request.Headers.Headers.Add("Authorization", header);

            // Make sure we reset the client (timestamp and nonce)
            if (AutoReset) Reset();

        }
 /// <summary>
 /// Helper method for generating the OAuth signature for an instance of <see cref="SocialHttpRequest"/>.
 /// </summary>
 /// <param name="request">The instance of <see cref="SocialHttpRequest"/> the signature should be based on.</param>
 /// <returns>Returns the generated OAuth signature.</returns>
 protected virtual string GenerateSignature(SocialHttpRequest request) {
     if (request == null) throw new ArgumentNullException("request");
     if (String.IsNullOrWhiteSpace(request.Url)) throw new PropertyNotSetException("request.Url");
     return GenerateSignature(request.Method, request.Url, request.QueryString, request.PostData);
 }
Beispiel #13
0
 private SocialHttpResponse(SocialHttpRequest request, HttpWebResponse response)
 {
     Request  = request;
     Response = response;
 }
Beispiel #14
0
            /// <summary>
            /// Makes a HTTP request using the specified <code>url</code> and <code>method</code>.
            /// </summary>
            /// <param name="url">The URL of the request.</param>
            /// <param name="method">The HTTP method of the request.</param>
            /// <param name="queryString">The query string of the request.</param>
            /// <param name="postData">The POST data of the request.</param>
            /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the response.</returns>
            private static SocialHttpResponse DoHttpRequest(SocialHttpMethod method, string url, IHttpQueryString queryString, IHttpPostData postData) {

                // Initialize the request
                SocialHttpRequest request = new SocialHttpRequest {
                    Url = url,
                    Method = method,
                    QueryString = queryString,
                    PostData = postData
                };

                // Make the call to the URL
                return request.GetResponse();

            }
        /// <summary>
        /// Virtual method that can be used for configuring a request.
        /// </summary>
        /// <param name="request">The instance of <see cref="SocialHttpRequest"/> representing the request.</param>
        protected override void PrepareHttpRequest(SocialHttpRequest request) {

            // Append the access token to the query string if present in the client and not already
            // specified in the query string
            if (!request.QueryString.ContainsKey("access_token") && !String.IsNullOrWhiteSpace(AccessToken)) {
                request.QueryString.Add("access_token", AccessToken);
            }

        }
        /// <summary>
        /// Makes a HTTP request to the underlying API based on the specified parameters.
        /// </summary>
        /// <param name="method">The HTTP method of the request.</param>
        /// <param name="url">The base URL of the request (no query string).</param>
        /// <param name="queryString">The query string.</param>
        /// <param name="postData">The POST data.</param>
        /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the raw response.</returns>
        public virtual SocialHttpResponse DoHttpRequest(SocialHttpMethod method, string url, IHttpQueryString queryString, IHttpPostData postData) {

            // Some input validation
            if (String.IsNullOrWhiteSpace(url)) throw new ArgumentNullException("url");
            if (queryString == null) queryString = new SocialHttpQueryString();
            if (postData == null) postData = new SocialHttpPostData();

            // Initialize the request
            SocialHttpRequest request = new SocialHttpRequest {
                Method = method,
                Url = url,
                QueryString = queryString,
                PostData = postData
            };

            PrepareHttpRequest(request);

            // Make the call to the URL
            return request.GetResponse();

        }
 /// <summary>
 /// Virtual method that can be used for configuring a request.
 /// </summary>
 /// <param name="request">The request.</param>
 protected virtual void PrepareHttpRequest(SocialHttpRequest request) { }
 private SocialHttpResponse(SocialHttpRequest request, HttpWebResponse response) {
     Request = request;
     Response = response;
     Encoding = DetectResponseEncoding();
 }