0

我正在尝试使用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 must be non-empty'}

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

4

2 回答 2

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()

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

不幸的是,没有办法通过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']

如果需要,您还可以将 dict 用于有效负载并将其转换为 JSON:

import json
...put(..., data=json.dumps({"name": "newname"}))
于 2018-07-20T14:38:26.763 回答
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
于 2018-08-30T11:58:58.600 回答