zoukankan      html  css  js  c++  java
  • (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来举例的,其实客户端有很多语言的

  • 相关阅读:
    JUnit报错 java.lang.Exception:No tests found matching
    tomcat配置好后,启动eclipse中的server,不能出现有猫的页面,提示404
    eclipse中的项目无法添加到server下?
    将web应用部署到Tomcat的三种方式
    启动eclipse弹出提示Version 1.7.0_79 of the JVM is not suitable for this product. Version: 1.8 or greater is required怎样解决
    EXISTS 与 NOT EXISTS 的用法及返回结果
    删除具有联合主键的记录
    序列化与反序列化
    tomcat 线程池
    Hibernate的实体类中为什么要继承Serializable?
  • 原文地址:https://www.cnblogs.com/toby2chen/p/5881768.html
Copyright © 2011-2022 走看看