• (44) odoo中的WebService


    * 前言
       erp系统会和其它系统进行对接,这时就要接口,官方给出的是两解决方案
      
    * XML-RPCLibrary
      举例
        import xmlrpclib

        root = 'http://%s:%d/xmlrpc/' % (HOST, PORT) #构建访问的路径

        uid = xmlrpclib.ServerProxy(root + 'common').login(DB, USER, PASS) # 用户登录
        print "Logged in as %s (uid: %d)" % (USER, uid)

        # Create a new note
        sock = xmlrpclib.ServerProxy(root + 'object') #得到一个模型对象
        args = {
            'color' : 8,
            'memo' : 'This is a note',
            'create_uid': uid,
        }
        note_id = sock.execute(DB, uid, PASS, 'note.note', 'create', args) #给对象增加一条记录
       
       
    * JSON-RPC Library
       这个目前相对流行,轻量级别的
        import json
        import random
        import urllib2

        # 封装数据处理响应
        def json_rpc(url, method, params):
            data = {
                "jsonrpc": "2.0",
                "method": method,
                "params": params,
                "id": random.randint(0, 1000000000),
            }
            req = urllib2.Request(url=url, data=json.dumps(data), headers={
                "Content-Type":"application/json",
            })
            reply = json.load(urllib2.urlopen(req))
            if reply.get("error"):
                raise Exception(reply["error"])
            return reply["result"]
           
       # 封装请求
        def call(url, service, method, *args):
            return json_rpc(url, "call", {"service": service, "method": method, "args": args})
           
       
        # log in the given database
        url = "http://%s:%s/jsonrpc" % (HOST, PORT)
        uid = call(url, "common", "login", DB, USER, PASS)

        # create a new note
        args = {
            'color' : 8,
            'memo' : 'This is another note',
            'create_uid': uid,
        }
        note_id = call(url, "object", "execute", DB, uid, PASS, 'note.note', 'create', args)
        #可以看出通用性很强
        -----------
        import jsonrpclib # https://pypi.python.org/pypi/jsonrpclib

        # server proxy object
        url = "http://%s:%s/jsonrpc" % (HOST, PORT)
        server = jsonrpclib.Server(url)

        # log in the given database
        uid = server.call(service="common", method="login", args=[DB, USER, PASS])

        # helper function for invoking model methods
        def invoke(model, method, *args):
            args = [DB, uid, PASS, model, method] + list(args)
            return server.call(service="object", method="execute", args=args)

        # create a new note
        args = {
            'color' : 8,
            'memo' : 'This is another note',
            'create_uid': uid,
        }
        note_id = invoke('note.note', 'create', args)
       
        # 上面例子,都是以客户端也是python来举例的,其实客户端有很多语言的

  • 相关阅读:
    端口
    log4j常用配置以及日志文件保存位置
    jbpm node signal
    JBPM3.2 TABLE
    JBPM TaskInstance 对象创建过程
    【转】链接脚本
    快速平方根倒数
    GPS开发之知识储备(NMEA0183)
    HEX文件格式和其校验算法
    NRF51822之IIC(MEMS_LIS2DH12)
  • 原文地址:https://www.cnblogs.com/toby2chen/p/5881768.html
Copyright © 2020-2023  润新知