示例#1
0
        /// <summary>
        /// gets a single blog post from the WordPress site by Id
        /// </summary>
        /// <param name="baseUrl">
        /// the web site's address
        /// </param>
        /// <param name="postId">
        /// id of the post to fetch
        /// </param>
        public async Task <WordPressEntity <BlogPost> > GetPostAsync(string baseUrl, int postId)
        {
            if (string.IsNullOrWhiteSpace(baseUrl))
            {
                throw new NullReferenceException($"parameter {nameof(baseUrl)} MUST not be null or empty");
            }

            if (postId == -1 || postId == default)
            {
                throw new ArgumentException($"parameter {nameof(postId)} MUST be a valid post id");
            }

            var response = await HttpClientInstance.GetAsync(baseUrl.GetEntityApiUrl(postId)).ConfigureAwait(false);

            WordPressEntity <BlogPost> result;

            if (response.IsSuccessStatusCode)
            {
                result = new WordPressEntity <BlogPost>(response.Content, false, this.ThrowSerializationExceptions);
            }
            else
            {
                result = new WordPressEntity <BlogPost>(response.Content, true, this.ThrowSerializationExceptions);
            }

            return(result);
        }
示例#2
0
        /// <summary>
        /// creates a new comment with an anonymous user. Requires the site to be set up for this
        /// </summary>
        /// <param name="baseUrl">the web site's address</param>
        /// <param name="authorName">name of the comment author</param>
        /// <param name="email">email of the comment authro</param>
        /// <param name="content">comment text</param>
        /// <param name="postId">the post id this comment should be posted to</param>
        /// <param name="parentId">id of the comment that receives the response</param>
        /// <returns></returns>
        public async Task <WordPressEntity <Comment> > CreateAnonymousComment(string baseUrl, string authorName, string email, string content, long postId, long parentId)
        {
            WordPressEntity <Comment> result = null;

            var postCommentUrl = baseUrl.GetPostApiUrl(Resource.Comments)
                                 .AddParameterToUrl("post", postId.ToString())
                                 .AddParameterToUrl("author_name", WebUtility.UrlEncode(authorName))
                                 .AddParameterToUrl("author_email", WebUtility.UrlEncode(email))
                                 .AddParameterToUrl("author_name", WebUtility.UrlEncode(authorName))
                                 .AddParameterToUrl("content", WebUtility.UrlEncode(content));

            if (parentId != 0)
            {
                postCommentUrl = postCommentUrl.AddParameterToUrl("parent", parentId.ToString());
            }

            var response = await HttpClientInstance.PostAsync(postCommentUrl, null).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                result = new WordPressEntity <Comment>(response.Content, false, ThrowSerializationExceptions);
            }
            else
            {
                result = new WordPressEntity <Comment>(response.Content, true, ThrowSerializationExceptions);
            }

            return(result);
        }
示例#3
0
        /// <summary>
        /// creates a new comment with an anonymous user. Requires the site to be set up for this
        /// </summary>
        /// <param name="baseUrl">
        /// the web site's address
        /// </param>
        /// <param name="authorName">
        /// name of the comment author
        /// </param>
        /// <param name="email">
        /// email of the comment authro
        /// </param>
        /// <param name="content">
        /// comment text
        /// </param>
        /// <param name="postId">
        /// the post id this comment should be posted to
        /// </param>
        /// <param name="parentId">
        /// id of the comment that receives the response
        /// </param>
        /// <returns>
        /// </returns>
        public async Task <WordPressEntity <Comment> > CreateAnonymousCommentAsync(string baseUrl, string authorName, string email, string content, long postId, long parentId)
        {
            if (string.IsNullOrWhiteSpace(baseUrl))
            {
                throw new NullReferenceException($"parameter {nameof(baseUrl)} MUST not be null or empty");
            }

            if (string.IsNullOrWhiteSpace(authorName))
            {
                throw new NullReferenceException($"parameter {nameof(authorName)} MUST not be null or empty");
            }

            if (string.IsNullOrWhiteSpace(email))
            {
                throw new NullReferenceException($"parameter {nameof(email)} MUST not be null or empty");
            }

            if (string.IsNullOrWhiteSpace(content))
            {
                throw new NullReferenceException($"parameter {nameof(content)} MUST not be null or empty");
            }

            if (postId == -1 || postId == default)
            {
                throw new ArgumentException($"parameter {nameof(postId)} MUST be a valid post id");
            }

            var postCommentUrl = baseUrl.GetPostApiUrl(Resource.Comments)
                                 .AddParameterToUrl("post", postId.ToString())
                                 .AddParameterToUrl("author_name", WebUtility.UrlEncode(authorName))
                                 .AddParameterToUrl("author_email", WebUtility.UrlEncode(email))
                                 .AddParameterToUrl("author_name", WebUtility.UrlEncode(authorName))
                                 .AddParameterToUrl("content", WebUtility.UrlEncode(content));

            if (parentId != 0)
            {
                postCommentUrl = postCommentUrl.AddParameterToUrl("parent", parentId.ToString());
            }

            var response = await HttpClientInstance.PostAsync(postCommentUrl, null).ConfigureAwait(false);

            WordPressEntity <Comment> result;

            if (response.IsSuccessStatusCode)
            {
                result = new WordPressEntity <Comment>(response.Content, false, this.ThrowSerializationExceptions);
            }
            else
            {
                result = new WordPressEntity <Comment>(response.Content, true, this.ThrowSerializationExceptions);
            }

            return(result);
        }
示例#4
0
        /// <summary>
        /// gets a medium by id
        /// </summary>
        /// <param name="baseUrl">the web site's address</param>
        /// <param name="mediaId">the id of the medium to fetch</param>
        public async Task <WordPressEntity <Media> > GetMediumAsync(string baseUrl, long mediaId)
        {
            WordPressEntity <Media> result = null;

            var response = await HttpClientInstance.GetAsync(baseUrl.GetEntityApiUrl(mediaId, Resource.Media));

            if (response.IsSuccessStatusCode)
            {
                result = new WordPressEntity <Media>(response.Content, false, ThrowSerializationExceptions);
            }
            else
            {
                result = new WordPressEntity <Media>(response.Content, true, ThrowSerializationExceptions);
            }

            return(result);
        }
        /// <summary>
        /// gets a single blog post from the WordPress site by Id
        /// </summary>
        /// <param name="baseUrl">the web site's address</param>
        /// <param name="postId">id of the post to fetch</param>
        public async Task <WordPressEntity <BlogPost> > GetPostAsync(string baseUrl, int postId)
        {
            WordPressEntity <BlogPost> result = null;

            var response = await HttpClientInstance.GetAsync(baseUrl.GetEntityApiUrl(postId)).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                result = new WordPressEntity <BlogPost>(response.Content, false, ThrowSerializationExceptions);
            }
            else
            {
                result = new WordPressEntity <BlogPost>(response.Content, true, ThrowSerializationExceptions);
            }

            return(result);
        }
示例#6
0
        /// <summary>
        /// gets a single blog page from the WordPress site by Id
        /// </summary>
        /// <param name="baseUrl">the web site's address</param>
        /// <param name="postId">id of the page to fetch</param>
        public async Task <WordPressEntity <Page> > GetPageAsync(string baseUrl, int id)
        {
            WordPressEntity <Page> result = null;

            var response = await HttpClientInstance.GetAsync(baseUrl.GetEntityApiUrl(id, Resource.Pages));

            if (response.IsSuccessStatusCode)
            {
                result = new WordPressEntity <Page>(response.Content, false, ThrowSerializationExceptions);
            }
            else
            {
                result = new WordPressEntity <Page>(response.Content, true, ThrowSerializationExceptions);
            }

            return(result);
        }
        /// <summary>
        /// gets a single category from the WordPress site by Id
        /// </summary>
        /// <param name="baseUrl">the web site's address</param>
        /// <param name="postId">id of the post to fetch</param>
        public async Task <WordPressEntity <Category> > GetCategoryAsync(string baseUrl, int categoryId)
        {
            WordPressEntity <Category> result = null;

            var response = await HttpClientInstance.GetAsync(baseUrl.GetEntityApiUrl(categoryId)).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                var responseJson = await response.Content.ReadAsStringAsync();

                result = new WordPressEntity <Category>(response.Content, false, ThrowSerializationExceptions);
            }
            else
            {
                var errorJson = await response.Content.ReadAsStringAsync();

                result = new WordPressEntity <Category>(response.Content, true, ThrowSerializationExceptions);
            }

            return(result);
        }