API Ya.ru (beta)

  • From RSS
Attention: post for developers! Representatives of other professions may be incompatible with the text and exhibit behavioral signs of boredom when read.
We, the developers of Yandex, listen carefully to what users and other developers are asking us to do. Of course, we are not omnipotent and cannot provide happiness to everyone at once, but we are very pleased when it turns out to do something good. Here's how today. We open in public beta testing not one, but two services at once:
  • API for our beloved blogohosting - Ya.ru .
  • OAuth authorization service for this and our other APIs
While developing them, we not only adhered to the principles of openness and compliance with standards, but also gave ourselves the freedom to significantly increase the “degree of geekiness” - the API is based on the most fashionable technological principles.

Authorization is the beginning of work with the API, and we will begin with it. As a standard, we chose OAuth 2.0 . Despite the fact that it is only a draft so far , we decided to implement it for a very simple reason - this is an open standard that the most developed technology companies in the world are going to support. Its previous versions are implemented, for example, on Google and Twitter . We hope to support this type of authorization in our other APIs in the future, for example, in Photos(yes, we heard that you complained about their complicated authorization!)

After authorization using the Ya.ru API, you can programmatically view and edit the user profile, share links, change the mood, create blog posts and comment. In a word - almost everything that can be done on the service itself.

Structurally, the API is built on the basis of REST ideology:
  • the entire service is presented as resources with state
  • each resource has a standard access interface based on HTTP methods and error codes
  • resources use URIs to navigate related parts of the system
  • where possible, standard data presentation formats and protocols are used - in particular, Atom and AtomPub
We chose REST because it reflects as much as possible our views on what the web service API should be. A standardized interface and open formats give developers the opportunity to use their best practices and third-party libraries for different services, instead of writing completely unique code for each. On the part of the service, this greatly simplifies documentation support, and also, for example, makes it possible to more conveniently scale services and combine them with each other.

This may seem rather complicated, therefore, to give a feeling that in practice everything is much simpler, here is a short example of Python code that changes the user's mood:

# - * - coding: utf-8 - * -
from urllib2 import urlopen, Request
import elementflow
import lxml.etree
ACCESS_TOKEN = 'f46606d61b9249078945599fb7192eb2'
NAMESPACES = {
  '': 'http://www.w3.org/2005/Atom',
  'y': 'yandex: data',
}
HOST = 'api-yaru.yandex.ru'
def auth_request (url, body = None):
    '' 'Creates an authorized request object.' ''
    return urlopen (Request (url, data = body, headers = {
        'Authorization': 'OAuth% s'% ACCESS_TOKEN
    }))
def get_link (rel):
    '' 'Returns the URL of the desired resource from an authorized user profile.' ''
    f = auth_request ('https: //% s / me /'% HOST)
    xml = lxml.etree.parse (f)
    namespaces = {'y': NAMESPACES ['y']}
    links = xml.xpath ('/ y: person / y: link [@rel = "% s"]'% rel, namespaces = namespaces)
    return links [0] .attrib ['href']
def change_mood (mood):
    '' 'Creates a post such as "change of mood."' ''
    with elementflow.xml (elementflow.Queue (), 'entry', namespaces = NAMESPACES) as xml:
        xml.element ('category', {'scheme': 'urn: wow.ya.ru: posttypes', 'term': 'status'})
        xml.element ('content', text = mood)
        xml.element ('y: comments-disabled')
    auth_request (get_link ('posts'), xml.file.pop ())
if __name__ == '__main__':
    change_mood (u 'Test mood')

Read more in the documentation .

Releasing the API not as a complete service, but as a beta version, we invite all interested developers to test it. We are interested in receiving from you wishes for functionality and error messages, write to the Ya.ru Service Club . Both the API and the OAuth protocol, which is also in the draft stage, will definitely change, be prepared for this.
To the cause!

Ivan Sagalaev and Grigory Bakunov, experienced geeks
.

Also popular now: