使用pyArango重命名集合

时间:2018-07-16 12:49:58

标签: python arangodb pyarango

我正在尝试使用pyArango重命名ArangoDB集合。这是我到目前为止的内容:

connection = pyArango.Connection('http://random-address', username='random-username', password='random-password')
test_db = Database(connection, 'test-db')
collection = test_db["new"]
collection.action("PUT", "rename", name="newname")

代码在第4行中失败:

  

{'error':True,'code':400,'errorNum':1208,'errorMessage':'name   必须为非空'}

我可能错误地使用了action方法,但是文档没有提供任何示例。有人知道吗?

2 个答案:

答案 0 :(得分:0)

需要将JSON对象{"name": "newname"}作为请求主体传递。新名称不能作为URL路径参数传递。问题是collection.action()的实现:

def action(self, method, action, **params) :
    "a generic fct for interacting everything that doesn't have an assigned fct"
    fct = getattr(self.connection.session, method.lower())
    r = fct(self.URL + "/" + action, params = params)
    return r.json()

关键字参数最终以称为params的字典形式结束。该对象作为命名参数fct()传递到请求函数params。此参数接收字典并将其转换为URL路径参数,例如服务器的HTTP API不支持的?name=newname

很遗憾,没有办法通过action()传递有效负载。但是,您可以编写一些自定义代码:

from pyArango.connection import *
connection = Connection('http://localhost:8529', username='root', password='')

try:
    connection.createDatabase('test-db')
except CreationError:
    pass
test_db = Database(connection, 'test-db')

try:
    test_db.createCollection(name='new')
except CreationError:
    pass
collection = test_db['new']

r = connection.session.put(collection.URL + '/rename', data='{"name":"newname"}')
print(r.text)
collection = test_db['newname']

如果需要,您还可以对有效负载使用字典并将其转换为JSON:

import json
...put(..., data=json.dumps({"name": "newname"}))

答案 1 :(得分:0)

我已将其固定为这样:

def rename_collection(arango_uri, username, password, database, collection, new_name):
    url = '{}/_db/{}/_api/collection/{}/rename'.format(arango_uri, database, collection)
    params = {"name": new_name}
    response = requests.put(url, data=json.dumps(params), auth=HTTPBasicAuth(username, password))
    return response