diff --git a/Yavsc/Helpers/SimpleJsonPostMethod.cs b/Yavsc.Api/Helpers/SimpleJsonPostMethod.cs similarity index 55% rename from Yavsc/Helpers/SimpleJsonPostMethod.cs rename to Yavsc.Api/Helpers/SimpleJsonPostMethod.cs index 71da12c0..e92b43b3 100644 --- a/Yavsc/Helpers/SimpleJsonPostMethod.cs +++ b/Yavsc.Api/Helpers/SimpleJsonPostMethod.cs @@ -18,61 +18,48 @@ // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . -using System; using System.Net; using System.IO; +using System.Json; +using System.Threading.Tasks; using Newtonsoft.Json; +using System; namespace Yavsc.Helpers { /// /// Simple json post method. /// - public class SimpleJsonPostMethod: IDisposable + public class SimpleJsonPostMethod : IDisposable { - internal HttpWebRequest request = null; - internal HttpWebRequest Request { get { return request; } } - - string CharSet { - get { return Request.TransferEncoding; } - set { Request.TransferEncoding=value;} - } - string Method { get { return Request.Method; } } - /// - /// Gets the path. - /// - /// The path. - public string Path { - get{ return Request.RequestUri.ToString(); } - } - /// - /// Sets the credential. - /// - /// Cred. - public void SetCredential(string cred) { - Request.Headers.Set(HttpRequestHeader.Authorization,cred); - } + private HttpWebRequest request=null; /// /// Initializes a new instance of the Yavsc.Helpers.SimpleJsonPostMethod class. /// /// Path to method. - public SimpleJsonPostMethod (string pathToMethod) + public SimpleJsonPostMethod (string pathToMethod, string authorizationHeader = null) { - // ASSERT Request == null request = (HttpWebRequest) WebRequest.Create (pathToMethod); - - Request.Method = "POST"; - Request.Accept = "application/json"; - Request.ContentType = "application/json"; - Request.SendChunked = true; - Request.TransferEncoding = "UTF-8"; + request.Method = "POST"; + request.Accept = "application/json"; + request.ContentType = "application/json"; + request.SendChunked = true; + request.TransferEncoding = "UTF-8"; + if (authorizationHeader!=null) + request.Headers.Add(authorizationHeader); } - /// - /// Invoke the specified query. - /// - /// Query. - public TAnswer Invoke(TQuery query) + + public void Dispose() + { + request.Abort(); + } + + /// + /// Invoke the specified query. + /// + /// Query. + public TAnswer Invoke(object query) { using (Stream streamQuery = request.GetRequestStream()) { @@ -80,7 +67,7 @@ namespace Yavsc.Helpers writer.Write (JsonConvert.SerializeObject(query)); }} TAnswer ans = default (TAnswer); - using (WebResponse response = Request.GetResponse ()) { + using (WebResponse response = request.GetResponse ()) { using (Stream responseStream = response.GetResponseStream ()) { using (StreamReader rdr = new StreamReader (responseStream)) { ans = (TAnswer) JsonConvert.DeserializeObject (rdr.ReadToEnd ()); @@ -91,16 +78,23 @@ namespace Yavsc.Helpers return ans; } - #region IDisposable implementation - - /// - /// Releases all resource used by the Yavsc.Helpers.SimpleJsonPostMethod object. - /// - public void Dispose () + public async Task InvokeJson(object query) { - if (Request != null) Request.Abort (); + + JsonValue jsonDoc=null; + using (Stream streamQuery = request.GetRequestStream()) { + using (StreamWriter writer = new StreamWriter(streamQuery)) { + writer.Write (JsonConvert.SerializeObject(query)); + }} + using (WebResponse response = request.GetResponse ()) { + using (Stream stream = response.GetResponseStream ()) { + if (stream.Length>0) + jsonDoc = await Task.Run (() => JsonObject.Load (stream)); + } + response.Close(); + } + return jsonDoc; } - #endregion } }