PUT API - The MDN project 编辑

Draft

This page is not complete.

The MDN wiki provides an experimental HTTP PUT API for updating documents in whole or by section. This can be handy for things such as:

  • You can create a page for your project and update content in certain sections from automated build, testing, and deployment scripts. This can help you keep your community up to date with your project's progress.
  • If your project offers documentation alongside source code, you can push HTML renderings into a subsection of MDN. This lets you maintain docs in a way that's appropriate for your team's workflow, while still contributing to MDN and allowing localizers to translate the content.

Testing your application

In developing the software that runs MDN, we host instances of the site on servers in various stages of readiness:

To keep from cluttering up the Production site with throwaway content, you should try developing your application against Staging first. Then, when you are reasonably sure that it'll do what you want, reconfigure it to work against Production. You can also try working against Development, but you may run into issues.

Creating an API key

An API key allows an application to act on your behalf, without requiring your intervention to sign in with Persona every time. It provides a username and password for use with HTTP Basic authentication over SSL. It collects basic usage tracking, so you can see how it's been used. And, you can delete an API key to revoke access, in case it has been accidentally released to parties who shouldn't have it.

If you have the correct privileges to do so, to create an API key, sign into MDN and visit the API keys management page. This page lets you create and delete API keys, as well as inspect recent usage history.  Only Mozillians in good standing can currently get API keys, since they grant abilities to automate changes to content rapidly, so unprivileged users must request the ability by filing a bug.

Note: The above link goes to the Production site, and the same keys do not work between Production and Staging. You can also get to this page by visiting your profile on the respective site: Click on your username in the upper right of the site. On your profile page, you should see a "Manage API Keys" button.

From there, clicking on the "Create a new API key" button should take you to an entry form so you can submit a request for an API key.

After filling out and submitting the form, you will receive a key ID and secret. These are your username and password, respectively. Copy these down somewhere safe (eg. to your application's configuration settings); the site will never display them again, and there is no recovery method. If you lose them, delete the API key and create another.

Making a PUT request

Since the PUT API works by way of HTTP, it should be compatible with the application environment and libraries of your choice. This first example uses the command-line tool cURL and a UNIX shell to demonstrate how to issue a simple PUT request to MDN.

Request

# Base URL and API key from staging (example only; substitute your own)
MDN_BASE_URL="https://developer.allizom.org"
MDN_KEY_ID="frsNFFR3w0yEALRE9IA9oN1KwoDno8vVGrzsBNvCofI"
MDN_SECRET="423PdCvnvraH0FkCDTKnizTmKGNkEdgQTi6RlEFTiWs"

# Document-specific details
DOC_USERNAME="lmorchard"  # Change this to your name
DOC_PATH="/en-US/docs/User:$DOC_USERNAME/PutExample"
DOC_TYPE="text/html"
DOC_DATA="<b>HELLO WORLD</b>"

# Putting it all together...
curl -si -X PUT -H"Content-Type: $DOC_TYPE" -d"$DOC_DATA" -u"$MDN_KEY_ID:$MDN_SECRET" "$MDN_BASE_URL$DOC_PATH"

Since there's a lot going on in this cURL invocation, the example is broken into variables:

  • MDN_BASE_URL - as mentioned before, you should plan to switch your application between staging and production servers on MDN. This variable allows for that.
  • MDN_KEY_ID - the key ID from the API key you created. Note that these are server-specific - the same keys do not work between staging and production.
  • MDN_SECRET - the secret from the API key that corresponds with the key ID.
  • DOC_USERNAME - change this to your MDN username.
  • DOC_PATH - the URL path to the document with content you want to manipulate.
  • DOC_TYPE - the content in the request will be text/html
  • DOC_DATA - the content sent in the PUT request body; this is the content that will be used in a new revision to the document

So, along with the variables, here are some general notes on the example and its use of the PUT API:

  • The key ID and secret are supplied as username and password, respectively, in HTTP Basic authentication over SSL.
  • The DOC_PATH for this example includes a username - presumably yours - but that's just for the sake of example and ensuring you have your own sample document to play with. You can use any URL path to any document on the wiki.
  • A Content-Type header is required, and lets MDN know how to process the content sent in the PUT request. Several content types are supported, and this feature will be described in greater detail shortly.
  • Content intended for the document is sent in the request body, using the representation promised in the Content-Type header

Response

There are several responses you may see if you try this example: 403, 404, 201, or 205. (You may see others, but those suggest something has gone wrong with the site. That will, hopefully, be rare.)

403 Forbidden

If either the key ID or secret are incorrect, you'll see a 403 Forbidden response. Double check your key details and that you're using the right pair for the right server. Create a new API key, if necessary.

404 Not Found

If you've never created a document at the URL path /en-US/docs/User:$MDN_USERNAME, you'll see a 404 Not Found response.

Note: The PUT API will not automatically create parent documents. If you're creating a number of documents intended to comprise a subsection of MDN, make sure to create parent documents first from the top down in the hierarchy.

201 Created

If the parent document exists, but the path itself doesn't, you should see a 201 Created response. This signifies that a new document was created, as opposed to an existing one having been updated.

205 Reset Content

In the case of an updated document, you'll see a 205 Reset Content response. This means that the document content has been updated, and that you should reload the document if you happen to need to see the results.

Note: MDN performs certain filtering and processing steps on content, so what you put in may not be exactly what gets served back.

Supported Content Types

The PUT API accepts one of several content types in the request body.

text/html

There are actually two forms of text/html accepted: fragment and document.

Fragment

An HTML fragment is just an arbitrary chunk of markup, and is used as-is to revise document content. This is the simplest way to update documents.

Document

However, if the request body consists of an <html> element containing <head> and <body> elements, it's treated as a full HTML document. In this case, the following processing happens:

  • From the <head> element, the contents of <title> is extracted and used as the title for the document on MDN.
  • The contents of <body> is extracted as the content for a new revision. 

This is a more complex way to update documents, but is intended as a convenience to accommodate submission of existing HTML pages.

application/json

Although the text/html content type is handy, there are more fields belonging to documents that are useful to manage. These include the following:

  • title - the document title
  • content - the content intended for the new revision
  • tags - tags used to organize documents: this is given as a single string, with tags separated by commas
  • review_tags - tags used to request content reviews: this is given as a single string, with tags separated by commas
  • summary - a comment describing the revision to be made
  • show_toc - a flag (0/1) indicating whether the table of contents should be shown for this document

These fields can be supplied as string values in a JSON-encoded object with the application/json content-type in a PUT request.

# Auth Stuff
DOC_USERNAME="lmorchard"  # Change this to your name
MDN_KEY_ID="frsNFFR3w0yEALRE9IA9oN1KwoDno8vVGrzsBNvCofI"
MDN_SECRET="423PdCvnvraH0FkCDTKnizTmKGNkEdgQTi6RlEFTiWs"

# Base Settings (for Staging Env)
MDN_BASE_URL="https://developer.allizom.org"
DOC_PATH="/en-US/docs/User:$DOC_USERNAME/PutExample"
DOC_TYPE="application/json"

# Doc Content
echo '{"content": "<b>Hello World</b>", "title": "New Sample Title", "show_toc": 1, "tags": "Beginner, Tutorial", "review_tags": "editorial, technical", "summary": "Sample JSON update from the API"}' > /tmp/mdn.json

# Submitting Content
curl -X PUT -H "Content-Type: $DOC_TYPE" -d @/tmp/mdn.json -u"$MDN_KEY_ID:$MDN_SECRET" "$MDN_BASE_URL$DOC_PATH"

multipart/form-data

This content type is handled basically like application/json - the same fields are accepted. But, it might be less useful than JSON and is supported mainly for testing purposes.

Updating a single section

Normally, an HTTP PUT request replaces the entirety of a document with the submitted content in a new revision. However, you can use the query parameter ?section to constrain revision to a single section of the document and leave the rest of the content as-is. This is handy for automating changes to one part of a document that is otherwise managed by hand, or even for aggregating changes from many sources or scripts into one document.

Creating document sections

Documents on MDN can be broken up into sections. These sections are useful for building a table of contents, linking to specific parts, and editing subsets of document content.

Using headers

Headers (ie. <h2> .. <h6>) make sections in MDN documents. The text of each header is transformed automatically into an ID, and that's used for anchor links in the table of contents sidebar on most documents. Those auto-generated IDs can be overridden with the name attribute on headers. Either way, looking at the table of contents is the easiest way to see how a document is broken up into sections, and to discover the IDs for those sections.

The contents of a section include its header and everything following the header up to (but not including) another header of the same or higher level. So, a section that starts with an <h2> continues until the next <h2>, including any subsections started by <h3> .. <h6>. That also means sections can be nested: An <h3> appearing after an <h2> creates a subsection, including any further nested subsections started by <h4> .. <h6>, up to the next <h3> or <h2>.

@@TODO: Show an HTML example with headers, here. This is a bit confusing.

Using container elements

Setting an id attribute on a container element (eg. a <div> or <span> or <section>) in the source editor also creates a section, at least with respect to the PUT API. This is a bit more advanced and requires manual changes to raw HTML, rather than using the WYSIWYG editor. But, if you want to update a chunk of the page without the need for headers, this is how to do it.

Specifying a section

  • Look at the table of contents, note the anchor ID for the link (ie. the #hash part of the URL).
    Example: /wiki/en-US/docs/User:lmorchard/PUT-API#Specifying_a_section
  • Take everything after the "#" character, and you have the section ID.
    Example: Specifying_a_section
  • Add ?section={ID} to the URL for the document, substituting the section ID for {ID}.
    Example: https://developer.mozilla.org/wiki/en-US/docs/User:lmorchard/PUT-API?section=Specifying_a_section
  • If you view that URL in a browser (ie. HTTP GET), you'll see just that section of the document.
    (For more details on what you can do with HTTP GET, see also: Project:The_Kuma_API)
  • If you issue a PUT request to that URL, you'll modify just that section of the document.
    (But, don't do that to the example URL, or you'll clobber the page you're reading right now!)

Automating content updates

A common use for the PUT API is for automatically updating MDN's contents based on external data sources, or to automate applying the same kind of fix to a large set of pages. This can be done safely if your script is written carefully.

Warning: Be sure to test any content updating script against a local copy of Kuma or the staging server before running it against the production site.

An automatic content update is performed step by step; for each page that needs to be updated, the following steps are taken:

  1. Fetch the page's HTML.  The safest way to do this is by appending "$api" to the page URL, then fetch the contents of that URL. This gets you content as the API expects to see it if you write it back later, without added processing.
  2. Make your changes to the content.
  3. Write it back using the PUT API.

The reason to use "$api" instead of "?raw" when loading the page to get the content source HTML: the API and the site rendering runtime handle cleaning the content differently. The content returned by "?raw" is not processed through the bleach engine that cleans the content, removing undesirable HTML tags, improving HTML structure, etc. However, loading from "$api" returns content in its "bleached" state, and when writing with the PUT API expects that the content needs to be bleached, and does so.

As a result, if you fetch content from "?raw" and write it back immediately, without making changes, to the same URL plus "$api", the result is a page that looks the same but whose HTML may be unexpectedly changed, with certain tags removed, element attributes rearranged into a predefined order, and so forth.

As a result, the page now looks quite different when run through a comparison engine for diff purposes, and therefore changes look much more substantial than they were, which can be quite confusing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:123 次

字数:18626

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文