访问tryton模块的rest api

tryton-restful的Python项目详细描述


tryton模型的rest api

安装

从python包索引安装:

pip install tryton-restful

用法

在安装时,您应该能够使用 运行本地开发服务器。

tryton_restful --help

 Usage: tryton_restful [OPTIONS] HOST PORT

   Runs the application on a local development server.

 Options:
   -c, --config TEXT            Path to tryton configuration file
   --debug
   --threaded / --not-threaded  should the process handle each request in a
                                separate thread?
   --help                       Show this message and exit.

您可以通过以下方式运行服务器:

tryton_restful -c /path/to/tryton/config

提示

还可以使用环境变量指定配置文件。

导出tryton-config=/path/to/tryton/config

rest api端点:

/<;dbname>;/登录

POSTExpects login and password as form data and returns a JSON of user ID and session to be used for subsequent requests
importrequestsimportjsonDATABASE_NAME='rest'BASE_PATH='http://localhost:9000/'+DATABASE_NAMElogin_result=requests.post(BASE_PATH+'/login',data={'login':'admin','password':'admin'})tryton_session=login_result.json()printtryton_session
{u'session': u'966689963c0a4a939cb326c1451b0fe9', u'id': 1}

/<;dbname>;/model/<;model.name>;

GET

Return a list of records (Just the ID and rec_name)

Params:

  • domain: JSON serialised domain expression example: [[‘name’, ‘ilike’, ‘openlabs’]]
  • page: Integer number of the page
  • per_page: The number of records to be returned per page
  • order: JSON serialised order expression in which the records should be sorted. Ex: [(‘name’, ‘ASC’), (‘date’, ‘DESC’)]
s=requests.Session()s.auth=(tryton_session['id'],tryton_session['session'])# Use the session and get the list of usersprints.get(BASE_PATH+'/model/res.user').json()
{u'items': [{u'rec_name': u'Administrator', u'id': 1}]}
POSTCreates one or more records in the given model
# Create a new userheaders={'content-type':'application/json'}values=[{'name':'Thomas','login':'thomas','password':'password'},{'name':'Alfred','login':'alfred','password':'somethingelse'},]users=s.post(BASE_PATH+'/model/res.user',data=json.dumps(values),headers=headers).json()printusers
{u'items': [{u'rec_name': u'Thomas', u'id': 3}, {u'rec_name': u'Alfred', u'id': 4}]}
DELETEDelete all records in the given model

/<;dbname>;/model/<;model.name>;/<;id>;

GET

Return the details of the given record

Params:

  • fields_names: specify the list of fields to be returned. Default behavior is to return as much data as possible
# Get full details of the first userprints.get(BASE_PATH+'/model/res.user/1').json()
{u'create_date': u'Sat, 10 May 2014 08:51:16 GMT', ....}
# Get only a limited set of fieldsuser_url=BASE_PATH+'/model/res.user/1'prints.get(user_url+'?fields_names=name&fields_names=email').json()
{u'email': None, u'name': u'Administrator', u'id': 1}
PUTUpdate the given resource
# Change the email of the userheaders={'content-type':'application/json'}user_data=s.put(user_url,data=json.dumps({'email':'admin@example.com'}),headers=headers).json()printuser_data['email']
admin@example.com
DELETEDelete the given record
# get a new list of all usersprints.get(BASE_PATH+'/model/res.user').json()
{u'items': [{u'rec_name': u'Administrator', u'id': 1}, {u'rec_name': u'Thomas', u'id': 3}, {u'rec_name': u'Alfred', u'id': 4}]}
# delete user Alfred with ID 4prints.delete(BASE_PATH+'/model/res.user/4')
<Response [205]>
# get a new list of all usersprints.get(BASE_PATH+'/model/res.user').json()
{u'items': [{u'rec_name': u'Administrator', u'id': 1}, {u'rec_name': u'Thomas', u'id': 3}]}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java ActiveMQ 5.9.0、Glassfish 3.1.2和MDB用于长时间的消息处理   从main调用的对象数组的Java字符串表示形式   java如何在iText 7中为泰国字母上方的双标记设置GPO   编译如果Java6工件是用Java6、7或8编译的,这有关系吗?   image Java KeyListener未检测到键盘输入   java找不到符号(构造函数)   java如何使Kafka使用者从特定主题分区读取Spring Boot   Java readLine()返回null   从CSV文件计算值时出现java系统错误   java如何避免处理程序。被调用后延迟(可运行运行)?   Java Do和While验证   java如何访问父类型的ArrayList中的子方法?   java如何使用Deepfirstsearch算法获得最高级别的搜索   xml使用SAX解析器Java正确构建字符串   Android片段中的java Toast显示空指针expn   如何在java中将多个文件合并到另一个新文件中?   java在运行时在JVisualVM中更改应用程序的标题   javajavax。命名。NoInitialContextException:需要在环境或sys中指定类名