예제 #1
0
        /// <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));
        }
예제 #2
0
        /// <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));
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        /// <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);
        }
예제 #6
0
        /// <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);
        }
예제 #7
0
        /// <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]);
        }
예제 #8
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));
        }
예제 #9
0
        /// <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
            }));
        }
예제 #10
0
        /// <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
            }));
        }
예제 #11
0
        /// <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);
        }
예제 #12
0
        /// <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));
        }
예제 #13
0
        /// <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));
            }
        }
예제 #14
0
 /// <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));
 }