# -*- coding: utf-8 -*- import urllib import httplib import json import uritemplate headers = {'Authorization': '<OAuth >'} connection = httplib.HTTPSConnection('cloud-api.yandex.net') resource_url = '/v1/disk/resources' def request(method, url, query=None): if query: qs = urllib.urlencode(query) url = '%s?%s' % (url, qs) connection.request(method, url, headers=headers) resp = connection.getresponse() content = resp.read() obj = json.loads(content) if content else None status = resp.status if status == 201: # obj = request(obj['method'], obj['href']) return obj if __name__ == '__main__': # path = '/foo' folder = request('PUT', resource_url, {'path': path}) # new_path = '/bar' folder = request('POST', '%s/move' % resource_url, {'path': new_path, 'from': path}) # copy_path = '/foobar' folder_copy = request('POST', '%s/copy' % resource_url, {'path': copy_path, 'from': new_path}) # request('DELETE', resource_url, {'path': new_path}) request('DELETE', resource_url, {'path': copy_path})
# -*- coding: utf-8 -*- import urllib import httplib import json import uritemplate headers = {'Authorization': '<OAuth >', 'Accept': 'application/hal+json'} connection = httplib.HTTPSConnection('cloud-api.yandex.net') resource_url = '/v1/disk/resources?path={path}' def request(method, url, params=None): url = uritemplate.expand(url, params or {}) connection.request(method, url, headers=headers) resp = connection.getresponse() content = resp.read() obj = json.loads(content) if content else None status = resp.status if status == 201: # status, obj = request(obj['method'], obj['href']) return status, obj def do(resource, action, params=None): link = resource['_links'][action] _, obj = request(link['method'], link['href'], params) return obj if __name__ == '__main__': # _, folder = request('PUT', uritemplate.expand(resource_url, {'path': '/foo'})) # folder = do(folder, 'move', {'path': '/bar'}) # folder_copy = do(folder, 'copy', {'path': '/foobar'}) # do(folder, 'delete') do(folder_copy, 'delete')
Source: https://habr.com/ru/post/227377/
All Articles