//-----------------------------------------------------------------------
//
// Company copyright tag.
//
// stubbfel
// 10.06.2013
//----------------------------------------------------------------------using System;
namespace CampusAppWP8ScheduledTaskAgent.Utility
{
using System;
using System.Collections.Generic;
using System.Net;
using CampusAppWP8ScheduledTaskAgent.Model.Utility;
///
/// Class realize the access of restful HttpRequest
///
public class HttpRequest
{
#region Member
///
/// BaseAddress of the webClient
///
private string baseAddress;
#endregion
#region Constructor
///
/// Initializes a new instance of the class.
///
public HttpRequest()
{
}
///
/// Initializes a new instance of the class.
///
/// the url of the HttpRequest base address
public HttpRequest(Uri apiBaseAddress)
{
this.baseAddress = apiBaseAddress.AbsoluteUri;
}
#endregion
#region Methods
#region public
///
/// Method realize the http-get-method resource
///
/// Url of the resource
/// callback method
public void HttpGet(Uri url, Action action)
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(action);
client.DownloadStringAsync(url);
}
/// Method realize the http-get-method resource.
/// Stubbfel, 03.09.2013.
/// Url of the resource.
/// The action.
public void HttpGet(Uri url, Action action)
{
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(action);
client.OpenReadAsync(url);
}
///
/// Method create the Url for the http-get-method
///
/// list of parameters
/// absolute API-Url include GetParameter
public Uri CreateGetUrl(List parameters)
{
string paramterStr = string.Empty;
string seperator = string.Empty;
foreach (UrlParamModel parameter in parameters)
{
if (string.Empty.Equals(seperator))
{
seperator = parameter.ParamToken;
}
paramterStr += parameter.ToString();
}
string getUrlStr = this.baseAddress + seperator + paramterStr;
return new Uri(getUrlStr, UriKind.Absolute);
}
///
/// Method realize the http-delete-method
///
///
/// is not supported by WebClient
///
/// Url of the resource
/// callback method
public void HttpDelete(Uri url, Action action)
{
throw new NotSupportedException();
}
///
/// Method realize the http-head-method
///
///
/// is not supported by WebClient
///
/// Url of the resource
/// callback method
public void HttpHead(Uri url, Action action)
{
throw new NotSupportedException();
}
///
/// Method realize the http-options-method
///
///
/// is not supported by WebClient
///
/// Url of the resource
/// callback method
public void HttpOptions(Uri url, Action action)
{
throw new NotSupportedException();
}
///
/// Method realize the http-connect-method
///
///
/// is not supported by WebClient
///
/// Url of the resource
/// callback method
public void HttpConnect(Uri url, Action action)
{
throw new NotSupportedException();
}
///
/// Method realize the http-trace-method
///
///
/// is not supported by WebClient
///
/// Url of the resource
/// callback method
public void HttpTrace(Uri url, Action action)
{
throw new NotSupportedException();
}
///
/// Method realize the http-post-method
///
/// Url of the resource
/// callback method
/// Data which are sending via post to the HttpRequest
public void HttpPost(Uri url, Action action, string postData)
{
this.UploadData(url, action, "POST", postData);
}
///
/// Method realize the http-put-method
///
/// Url of the resource
/// callback method
/// Data which are sending via put to the HttpRequest
public void HttpPut(Uri url, Action action, string putData)
{
this.UploadData(url, action, "PUT", putData);
}
///
/// Method realize the http-patch-method
///
/// Url of the resource
/// callback method
/// Data which are sending via patch to the HttpRequest
public void HttpPatch(Uri url, Action action, string patchData)
{
this.UploadData(url, action, "PATCH", patchData);
}
#endregion
#region private
///
/// Method uploaded Data to the HttpRequest
///
/// Url of the resource
/// callback method
/// name of APIMethod, how the data will be uploaded
/// Data which are sending to the HttpRequest
private void UploadData(Uri url, Action action, string method, string data)
{
WebClient client = new WebClient();
client.UploadStringCompleted += new UploadStringCompletedEventHandler(action);
client.UploadStringAsync(url, method, data);
}
#endregion
#endregion
}
}