/// <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()); }
/// <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 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> /// 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(); }
/// <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(); }
/// <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(); }