Ejemplo n.º 1
0
        public async Task <PostResult> EditPost(BlogPost post, INewCategoryContext newCategoryContext, bool publish)
        {
            // initialize result (for edits the id never changes)
            PostResult result         = new PostResult();
            var        editpostResult = new EditPostResult();

            result.PostId = post.Id;
            try
            {
                //apply any publishing filters and make the post
                using (new ContentFilterApplier(post, ClientOptions, ContentFilterMode.Publish))
                {
                    // make the post
                    if (post.IsPage)
                    {
                        await BlogClient.EditPage(_settings.HostBlogId, post, publish, result.ETag, result.AtomRemotePost);
                    }
                    else
                    {
                        await BlogClient.EditPost(_settings.HostBlogId, post, newCategoryContext, publish, editpostResult);
                    }
                }
                // note success
                _settings.LastPublishFailed = false;
            }
            catch (BlogClientProviderException ex)
            {
                if (ErrorIsInvalidPostId(ex))
                {
                    return(await NewPost(post, newCategoryContext, publish));
                }
                else
                {
                    throw;
                }
            }
            catch
            {
                _settings.LastPublishFailed = true;
                throw;
            }

            // determine the date-published based on whether there was an override
            if (post.HasDatePublishedOverride)
            {
                result.DatePublished = post.DatePublishedOverride;
            }
            else
            {
                result.DatePublished = DateTime.UtcNow;
            }

            // return result
            return(result);
        }
        public async Task <bool> EditPost(string blogId, BlogPost post, INewCategoryContext newCategoryContext, bool publish, EditPostResult editPostResult)
        {
            // The remote post is only meant to be used for blogs that use the Atom protocol.
            editPostResult.remotePost = null;

            if (!publish && !Options.SupportsPostAsDraft)
            {
                Debug.Fail("Post to draft not supported on this provider");
                throw new BlogClientPostAsDraftUnsupportedException();
            }

            var bloggerPost       = ConvertToGoogleBloggerPost(post);
            var updatePostRequest = (await GetService()).Posts.Update(bloggerPost, blogId, post.Id);

            updatePostRequest.Publish = publish;

            var updatedPost = await updatePostRequest.ExecuteAsync();

            editPostResult.etag = updatedPost.ETag;
            return(true);
        }
Ejemplo n.º 3
0
        public virtual async Task <bool> EditPost(string blogId, BlogPost post, INewCategoryContext newCategoryContext, bool publish, EditPostResult result)
        {
            if (!publish && !Options.SupportsPostAsDraft)
            {
                //Debug.Fail("Post to draft not supported on this provider");
                throw new BlogClientPostAsDraftUnsupportedException();
            }

            Login();

            FixupBlogId(ref blogId);

            XmlDocument doc       = post.AtomRemotePost;
            XmlElement  entryNode = doc.SelectSingleNodeNS("/atom:entry", _nsMgr.ToNSMethodFormat()) as XmlElement;

            // No documentUri is needed because we ensure xml:base is set on the root
            // when we retrieve from XmlRestRequestHelper
            Populate(post, null, entryNode, publish);
            string etagToMatch = FilterWeakEtag(post.ETag);

            try
            {
retry:
                try
                {
                    XmlRestRequestHelper.XmlRequestResult xmlResult2 = new XmlRestRequestHelper.XmlRequestResult();
                    xmlResult2.uri = PostIdToPostUri(post.Id);
                    await xmlRestRequestHelper.Put(etagToMatch, RequestFilter, ENTRY_CONTENT_TYPE, doc, _clientOptions.CharacterSet, true, xmlResult2);
                }
                catch (WebException we)
                {
                    if (we.Status == WebExceptionStatus.ProtocolError)
                    {
                        if (((HttpWebResponse)we.Response).StatusCode == HttpStatusCode.PreconditionFailed)
                        {
                            if (etagToMatch != null && etagToMatch.Length > 0)
                            {
                                HttpRequestHelper.LogException(we);

                                string currentEtag = await GetEtag(UrlHelper.SafeToAbsoluteUri(PostIdToPostUri(post.Id)));

                                if (currentEtag != null && currentEtag.Length > 0 &&
                                    currentEtag != etagToMatch)
                                {
                                    if (ConfirmOverwrite())
                                    {
                                        etagToMatch = currentEtag;
                                        goto retry;
                                    }
                                    else
                                    {
                                        throw new BlogClientOperationCancelledException();
                                    }
                                }
                            }
                        }
                    }
                    throw;
                }
            }
            catch (Exception e)
            {
                if (!AttemptEditPostRecover(e, blogId, post, newCategoryContext, publish, result))
                {
                    // convert to a provider exception if this is a 404 (allow us to
                    // catch this case explicitly and attempt a new post to recover)
                    if (e is WebException)
                    {
                        WebException    webEx    = e as WebException;
                        HttpWebResponse response = webEx.Response as HttpWebResponse;
                        if (response != null && response.StatusCode == HttpStatusCode.NotFound)
                        {
                            throw new BlogClientProviderException("404", e.Message);
                        }
                    }

                    // no special handling, just re-throw
                    throw;
                }
            }

            XmlRestRequestHelper.XmlRequestResult xmlResult = new XmlRestRequestHelper.XmlRequestResult();
            xmlResult.uri             = PostIdToPostUri(post.Id);
            xmlResult.responseHeaders = new HttpResponseMessage().Headers;
            result.remotePost         = await xmlRestRequestHelper.Get(RequestFilter, xmlResult);

            result.etag = FilterWeakEtag(xmlResult.responseHeaders["ETag"]);
            //Debug.Assert(remotePost != null, "After successful PUT, remote post could not be retrieved");

            if (Options.SupportsNewCategories)
            {
                foreach (BlogPostCategory category in post.NewCategories)
                {
                    newCategoryContext.NewCategoryAdded(category);
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
 protected virtual bool AttemptEditPostRecover(Exception e, string blogId, BlogPost post, INewCategoryContext newCategoryContext, bool publish, EditPostResult result)
 {
     result.etag       = null;
     result.remotePost = null;
     return(false);
 }
 public Task <bool> EditPost(string blogId, BlogPost post, INewCategoryContext newCategoryContext, bool publish, EditPostResult result)
 {
     result.etag       = null;
     result.remotePost = null;
     return(EditPost(blogId, post, newCategoryContext, publish));
 }