/// <summary> /// Gets the raw response of an OData single content request from the server. /// </summary> /// <param name="path">Content path.</param> /// <param name="actionName">Action name.</param> /// <param name="method">HTTP method (SenseNet.Client.HttpMethods class has a few predefined methods).</param> /// <param name="body">Request body.</param> /// <param name="server">Target server.</param> /// <returns>Raw HTTP response.</returns> public static async Task <string> GetResponseStringAsync(string path, string actionName = null, HttpMethod method = null, string body = null, ServerContext server = null) { var requestData = new ODataRequest(server) { Path = path, ActionName = actionName }; return(await GetResponseStringAsync(requestData.GetUri(), server, method, body).ConfigureAwait(false)); }
/// <summary> /// Gets the raw response of an OData single content request from the server. /// </summary> /// <param name="contentId">Content id.</param> /// <param name="actionName">Action name.</param> /// <param name="method">HTTP method (SenseNet.Client.HttpMethods class has a few predefined methods).</param> /// <param name="body">Request body.</param> /// <param name="server">Target server.</param> /// <returns>Raw HTTP response.</returns> public static async Task <string> GetResponseStringAsync(int contentId, string actionName = null, HttpMethod method = null, string body = null, ServerContext server = null) { var requestData = new ODataRequest() { SiteUrl = ServerContext.GetUrl(server), ContentId = contentId, ActionName = actionName }; return(await GetResponseStringAsync(requestData.GetUri(), server, method, body)); }
private static async Task <dynamic> PostContentInternalAsync(int contentId, object postData, HttpMethod method, ServerContext server = null) { var reqData = new ODataRequest() { SiteUrl = ServerContext.GetUrl(server), ContentId = contentId }; var rs = await GetResponseStringAsync(reqData.GetUri(), server, method, JsonHelper.GetJsonPostModel(postData)); return(JsonHelper.Deserialize(rs).d); }
private static async Task <dynamic> PostContentInternalAsync(string path, object postData, HttpMethod method, ServerContext server = null) { var reqData = new ODataRequest(server) { Path = path }; var rs = await GetResponseStringAsync(reqData.GetUri(), server, method, JsonHelper.GetJsonPostModel(postData)) .ConfigureAwait(false); return(JsonHelper.Deserialize(rs).d); }
/// <summary> /// Undo all modifications on the content since the last checkout operation. /// </summary> public async Task UndoCheckOutAsync() { var requestData = new ODataRequest(Server) { ContentId = this.Id, Path = this.Path, ActionName = "UndoCheckOut", Select = new [] { "Id" } }; await RESTCaller.GetResponseStringAsync(requestData.GetUri(), Server, HttpMethod.Post).ConfigureAwait(false); }
/// <summary> /// Check in the content. /// </summary> public async Task CheckInAsync() { var requestData = new ODataRequest() { SiteUrl = Server.Url, ContentId = this.Id, Path = this.Path, ActionName = "CheckIn", Select = new[] { "Id" } }; await RESTCaller.GetResponseStringAsync(requestData.GetUri(), Server, HttpMethod.Post); }
/// <summary> /// Queries the server for content items using the provided request data. /// </summary> /// <param name="requestData">OData request parameters, for example select or expand.</param> /// <param name="server">Target server.</param> public static async Task <IEnumerable <Content> > GetCollectionAsync(ODataRequest requestData, ServerContext server = null) { requestData.SiteUrl = ServerContext.GetUrl(server); // just to make sure requestData.IsCollectionRequest = true; var rs = await GetResponseStringAsync(requestData.GetUri(), server); var items = JsonHelper.Deserialize(rs).d.results as JArray; return(items?.Select(c => Content.CreateFromResponse(c, server)) ?? new Content[0]); }
/// <summary> /// Loads a content from the server. /// </summary> /// <param name="requestData">OData request parameters, for example select or expand.</param> /// <param name="server">Target server.</param> public static async Task <Content> GetContentAsync(ODataRequest requestData, ServerContext server = null) { // just to make sure requestData.IsCollectionRequest = false; var rs = await GetResponseStringAsync(requestData.GetUri(), server); if (rs == null) { return(null); } return(Content.CreateFromResponse(JsonHelper.Deserialize(rs).d, server)); }
/// <summary> /// Creates a copy of the content to the target location. /// </summary> /// <param name="targetPath">Target path.</param> public async Task CopyToAsync(string targetPath) { var requestData = new ODataRequest() { SiteUrl = Server.Url, ContentId = this.Id, Path = this.Path, ActionName = "CopyTo" }; await RESTCaller.GetResponseStringAsync(requestData.GetUri(), Server, HttpMethod.Post, JsonHelper.GetJsonPostModel(new { targetPath })); }
/// <summary> /// Deletes the content. /// </summary> /// <param name="permanent">Delete the content permanently or into the Trash.</param> public async Task DeleteAsync(bool permanent = true) { var requestData = new ODataRequest() { SiteUrl = Server.Url, ContentId = this.Id, Path = this.Path, ActionName = "Delete" }; await RESTCaller.GetResponseStringAsync(requestData.GetUri(), Server, HttpMethod.Post, JsonHelper.GetJsonPostModel(new { permanent })); }
/// <summary> /// Moves the content to the target location. /// </summary> /// <param name="targetPath">Target path.</param> public async Task MoveToAsync(string targetPath) { var requestData = new ODataRequest(Server) { ContentId = this.Id, Path = this.Path, ActionName = "MoveTo" }; await RESTCaller.GetResponseStringAsync(requestData.GetUri(), Server, HttpMethod.Post, JsonHelper.GetJsonPostModel(new { targetPath })) .ConfigureAwait(false); }
/// <summary> /// Executes a count-only query on the server. /// </summary> /// <param name="requestData">OData request parameters, most importantly the query.</param> /// <param name="server">Target server.</param> public static async Task <int> GetCountAsync(ODataRequest requestData, ServerContext server) { // just to make sure requestData.IsCollectionRequest = true; requestData.CountOnly = true; var rs = await GetResponseStringAsync(requestData.GetUri(), server); int count; if (int.TryParse(rs, out count)) { return(count); } throw new ClientException(string.Format("Invalid count response. Request: {0}. Response: {1}", requestData.GetUri(), rs)); }
/// <summary> /// Gets the response of an OData request as a dynamic JSON object. /// </summary> /// <param name="requestData">OData request parameters, for example select or expand.</param> /// <param name="server">Target server.</param> /// <param name="method">Http method (e.g. Post). Default is Get.</param> /// <param name="postData">An object containing properties to be sent in the body. It will be serialized to JSON.</param> /// <returns>A dynamic JSON object deserialized from the response.</returns> public static async Task <dynamic> GetResponseJsonAsync(ODataRequest requestData, ServerContext server = null, HttpMethod method = null, object postData = null) { // it wouldn't work if we tried to post some data with the default GET verb if (postData != null && method == null) { method = HttpMethod.Post; } var rs = await GetResponseStringAsync(requestData.GetUri(), server, method, postData == null?null : JsonHelper.Serialize(postData)); try { return(JsonHelper.Deserialize(rs)); } catch (Exception) { throw new ClientException(string.Format("Invalid response. Request: {0}. Response: {1}", requestData.GetUri(), rs)); } }
/// <summary> /// Gets the raw response of an OData request from the server. /// </summary> /// <param name="requestData">OData request parameters, for example select or expand.</param> /// <param name="method">HTTP method (SenseNet.Client.HttpMethods class has a few predefined methods).</param> /// <param name="body">Request body.</param> /// <param name="server">Target server.</param> /// <returns>Raw HTTP response.</returns> public static async Task <string> GetResponseStringAsync(ODataRequest requestData, HttpMethod method = null, string body = null, ServerContext server = null) { return(await GetResponseStringAsync(requestData.GetUri(), server, method, body).ConfigureAwait(false)); }