-
Notifications
You must be signed in to change notification settings - Fork 10.8k
[IMP] content/developer/webservices #2481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| ============ | ||
| Web Services | ||
| ============ | ||
|
|
||
| The web-service module offer a common interface for all web-services : | ||
|
|
||
| - XML-RPC | ||
| - JSON-RPC | ||
|
|
||
| Business objects can also be accessed via the distributed object | ||
| mechanism. They can all be modified via the client interface with contextual | ||
| views. | ||
|
|
||
| Odoo is accessible through XML-RPC/JSON-RPC interfaces, for which libraries | ||
| exist in many languages. | ||
|
|
||
| XML-RPC Library | ||
| --------------- | ||
|
|
||
| The following example is a Python 3 program that interacts with an Odoo | ||
| server with the library ``xmlrpc.client``:: | ||
|
|
||
| import xmlrpc.client | ||
|
|
||
| root = 'http://%s:%d/xmlrpc/' % (HOST, PORT) | ||
|
|
||
| uid = xmlrpc.client.ServerProxy(root + 'common').login(DB, USER, PASS) | ||
| print("Logged in as %s (uid: %d)" % (USER, uid)) | ||
|
|
||
| # Create a new note | ||
| sock = xmlrpc.client.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) | ||
|
|
||
| .. exercise:: Add a new service to the client | ||
|
|
||
| Write a Python program able to send XML-RPC requests to a PC running | ||
| Odoo (yours, or your instructor's). This program should display all | ||
| the sessions, and their corresponding number of seats. It should also | ||
| create a new session for one of the courses. | ||
|
|
||
| .. only:: solutions | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import functools | ||
| import xmlrpc.client | ||
| HOST = 'localhost' | ||
| PORT = 8069 | ||
| DB = 'openacademy' | ||
| USER = 'admin' | ||
| PASS = 'admin' | ||
| ROOT = 'http://%s:%d/xmlrpc/' % (HOST,PORT) | ||
|
|
||
| # 1. Login | ||
| uid = xmlrpc.client.ServerProxy(ROOT + 'common').login(DB,USER,PASS) | ||
| print("Logged in as %s (uid:%d)" % (USER,uid)) | ||
|
|
||
| call = functools.partial( | ||
| xmlrpc.client.ServerProxy(ROOT + 'object').execute, | ||
| DB, uid, PASS) | ||
|
|
||
| # 2. Read the sessions | ||
| sessions = call('openacademy.session','search_read', [], ['name','seats']) | ||
| for session in sessions: | ||
| print("Session %s (%s seats)" % (session['name'], session['seats'])) | ||
| # 3.create a new session | ||
| session_id = call('openacademy.session', 'create', { | ||
| 'name' : 'My session', | ||
| 'course_id' : 2, | ||
| }) | ||
|
|
||
| Instead of using a hard-coded course id, the code can look up a course | ||
| by name:: | ||
|
|
||
| # 3.create a new session for the "Functional" course | ||
| course_id = call('openacademy.course', 'search', [('name','ilike','Functional')])[0] | ||
| session_id = call('openacademy.session', 'create', { | ||
| 'name' : 'My session', | ||
| 'course_id' : course_id, | ||
| }) | ||
|
|
||
| .. seealso:: | ||
| - :doc:`../api/external_api`: The in-depth tutorial on XML-RPC with examples spanning multiple programming languages. | ||
|
|
||
| JSON-RPC Library | ||
| ---------------- | ||
|
|
||
| The following example is a Python 3 program that interacts with an Odoo server | ||
| with the standard Python libraries ``urllib.request`` and ``json``. This | ||
| example assumes the **Productivity** app (``note``) is installed:: | ||
|
|
||
| import json | ||
| import random | ||
| import urllib.request | ||
|
|
||
| HOST = 'localhost' | ||
| PORT = 8069 | ||
| DB = 'openacademy' | ||
| USER = 'admin' | ||
| PASS = 'admin' | ||
|
|
||
| def json_rpc(url, method, params): | ||
| data = { | ||
| "jsonrpc": "2.0", | ||
| "method": method, | ||
| "params": params, | ||
| "id": random.randint(0, 1000000000), | ||
| } | ||
| req = urllib.request.Request(url=url, data=json.dumps(data).encode(), headers={ | ||
| "Content-Type":"application/json", | ||
| }) | ||
| reply = json.loads(urllib.request.urlopen(req).read().decode('UTF-8')) | ||
| 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) | ||
|
|
||
| Examples can be easily adapted from XML-RPC to JSON-RPC. | ||
|
|
||
| .. note:: | ||
|
|
||
| There are a number of high-level APIs in various languages to access Odoo | ||
| systems without *explicitly* going through XML-RPC or JSON-RPC, such as: | ||
|
|
||
| * https://github.com/akretion/ooor | ||
| * https://github.com/OCA/odoorpc | ||
| * https://github.com/nicolas-van/openerp-client-lib | ||
| * http://pythonhosted.org/OdooRPC | ||
| * https://github.com/abhishek-jaiswal/php-openerp-lib | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this is useless. This requires building the documentation with the tag
solutions, which nobody does (ever). At some point, we should replace theseonlydirectives with collapsable code-blocks or something else (any ideas @edi-odoo?)