Difference between revisions of "Python client - OpenKM 5.1"
From OpenKM Documentation
Ranoble.djn (talk | contribs) (→Authentication) |
m (moved Python client to Python client - OpenKM 5.1) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 55: | Line 55: | ||
# Login | # Login | ||
− | token = sAuth.login( | + | token = sAuth.login(user='okmAdmin', password='admin') |
print 'Token: '+token | print 'Token: '+token | ||
# Create document | # Create document | ||
− | newDoc = sDocument.create( | + | newDoc = sDocument.create(token=token, doc={'path':'/okm:root/hosts_1.txt', 'mimeType': None, |
'actualVersion': None, 'author': None, 'checkedOut': False, | 'actualVersion': None, 'author': None, 'checkedOut': False, | ||
'created': None, 'language': None, | 'created': None, 'language': None, | ||
Line 65: | Line 65: | ||
'permissions': 0, 'size': 0, 'subscribed': False, 'uuid': None, | 'permissions': 0, 'size': 0, 'subscribed': False, 'uuid': None, | ||
'convertibleToPdf': False, 'convertibleToSwf': False, | 'convertibleToPdf': False, 'convertibleToSwf': False, | ||
− | 'compactable': False, 'training': False}, | + | 'compactable': False, 'training': False}, content='Hello, world!') |
+ | print newDoc | ||
+ | |||
+ | # Logout | ||
+ | sAuth.logout(token=token) | ||
+ | </source> | ||
+ | |||
+ | <source lang="python"> | ||
+ | from SOAPpy import WSDL | ||
+ | |||
+ | # Register WSDL | ||
+ | sAuth = WSDL.Proxy('http://localhost:8080/OpenKM/OKMAuth?wsdl') | ||
+ | sDocument = WSDL.Proxy('http://localhost:8080/OpenKM/OKMDocument?wsdl') | ||
+ | |||
+ | # Login | ||
+ | token = sAuth.login(user='okmAdmin', password='admin') | ||
+ | print 'Token: '+token | ||
+ | |||
+ | # Create document | ||
+ | data = open(archi,'rb').read() | ||
+ | data64 = base64.encodestring(data) | ||
+ | newDoc = sDocument.createSimple(token=token, path='/okm:root/hosts_1.txt', content=data64) | ||
print newDoc | print newDoc | ||
# Logout | # Logout | ||
− | sAuth.logout( | + | sAuth.logout(token=token) |
</source> | </source> | ||
Line 93: | Line 114: | ||
# Logout | # Logout | ||
sAuth.logout(arg0=token) | sAuth.logout(arg0=token) | ||
+ | </source> | ||
+ | |||
+ | ==Search using find()== | ||
+ | <source lang="python"> | ||
+ | from SOAPpy import WSDL | ||
+ | |||
+ | # Register WSDL (note you may need to change the port if you have modified it to not use 8080) | ||
+ | sAuth = WSDL.Proxy('http://localhost:8080/OpenKM/OKMAuth?wsdl') | ||
+ | sSearch = WSDL.Proxy('http://localhost:8080/OpenKM/OKMSearch?wsdl') | ||
+ | |||
+ | # Login | ||
+ | token = sAuth.login(user='okmAdmin', password='admin') | ||
+ | print 'Token: '+token | ||
+ | |||
+ | # Search | ||
+ | queryResultArray = sSearch.find(token=token, params='Hello') | ||
+ | |||
+ | # Starting with OpenKM 5.0 should be queryResultArray['item'] | ||
+ | for results in queryResultArray: | ||
+ | for result in results: | ||
+ | print 'uuid: ' + result['document']['uuid'] | ||
+ | print 'mimeType: ' + result['document']['mimeType'] | ||
+ | print 'path: ' + result['document']['path'] | ||
+ | print 'author: ' + result['document']['author'] | ||
+ | print 'created: ' + result['document']['created'] | ||
+ | |||
+ | # Logout | ||
+ | sAuth.logout(token=token) | ||
</source> | </source> | ||
Line 124: | Line 173: | ||
[[Category: Webservices Guide]] | [[Category: Webservices Guide]] | ||
− |
Latest revision as of 18:34, 1 December 2012
These sample code is using the SOAPpy library. If you are in a Debian based distro you can install it this way:
$ sudo aptitude install python-soappy
More info about Python and webservices at:
- http://diveintopython.org/soap_web_services/index.html
- http://wiki.python.org/moin/WebServices.
- http://users.skynet.be/pascalbotte/rcx-ws-doc/python.htm
- http://www.ibm.com/developerworks/webservices/library/ws-pyth14
Remote method info
from SOAPpy import WSDL
# Register WSDL
sAuth = WSDL.Proxy('http://localhost:8080/OpenKM/OKMAuth?wsdl')
print sAuth.methods.keys()
print sAuth.soapproxy
callInfo = sAuth.methods['login']
print callInfo.inparams[0].name
print callInfo.inparams[0].type
print callInfo.inparams[1].name
print callInfo.inparams[1].type
Authentication
from SOAPpy import WSDL
# Register WSDL
sAuth = WSDL.Proxy('http://localhost:8080/OpenKM/OKMAuth?wsdl')
# Login
token = sAuth.login(user='okmAdmin', password='admin')
print 'Token: '+token
# Logout
sAuth.logout(token=token)
Create document
from SOAPpy import WSDL
# Register WSDL
sAuth = WSDL.Proxy('http://localhost:8080/OpenKM/OKMAuth?wsdl')
sDocument = WSDL.Proxy('http://localhost:8080/OpenKM/OKMDocument?wsdl')
# Login
token = sAuth.login(user='okmAdmin', password='admin')
print 'Token: '+token
# Create document
newDoc = sDocument.create(token=token, doc={'path':'/okm:root/hosts_1.txt', 'mimeType': None,
'actualVersion': None, 'author': None, 'checkedOut': False,
'created': None, 'language': None,
'lastModified': None, 'lockInfo': None, 'locked': False,
'permissions': 0, 'size': 0, 'subscribed': False, 'uuid': None,
'convertibleToPdf': False, 'convertibleToSwf': False,
'compactable': False, 'training': False}, content='Hello, world!')
print newDoc
# Logout
sAuth.logout(token=token)
from SOAPpy import WSDL
# Register WSDL
sAuth = WSDL.Proxy('http://localhost:8080/OpenKM/OKMAuth?wsdl')
sDocument = WSDL.Proxy('http://localhost:8080/OpenKM/OKMDocument?wsdl')
# Login
token = sAuth.login(user='okmAdmin', password='admin')
print 'Token: '+token
# Create document
data = open(archi,'rb').read()
data64 = base64.encodestring(data)
newDoc = sDocument.createSimple(token=token, path='/okm:root/hosts_1.txt', content=data64)
print newDoc
# Logout
sAuth.logout(token=token)
Perform search
from SOAPpy import WSDL
# Register WSDL
sAuth = WSDL.Proxy('http://localhost:8080/OpenKM/OKMAuth?wsdl')
sSearch = WSDL.Proxy('http://localhost:8080/OpenKM/OKMSearch?wsdl')
# Login
token = sAuth.login(arg0='okmAdmin', arg1='admin')
print 'Token: '+token
# Search
queryResultArray = sSearch.findByContent(arg0=token, arg1='grial')
# Starting with OpenKM 5.0 should be queryResultArray['item']
for queryResult in queryResultArray['value']:
print queryResult['document']['path']
# Logout
sAuth.logout(arg0=token)
Search using find()
from SOAPpy import WSDL
# Register WSDL (note you may need to change the port if you have modified it to not use 8080)
sAuth = WSDL.Proxy('http://localhost:8080/OpenKM/OKMAuth?wsdl')
sSearch = WSDL.Proxy('http://localhost:8080/OpenKM/OKMSearch?wsdl')
# Login
token = sAuth.login(user='okmAdmin', password='admin')
print 'Token: '+token
# Search
queryResultArray = sSearch.find(token=token, params='Hello')
# Starting with OpenKM 5.0 should be queryResultArray['item']
for results in queryResultArray:
for result in results:
print 'uuid: ' + result['document']['uuid']
print 'mimeType: ' + result['document']['mimeType']
print 'path: ' + result['document']['path']
print 'author: ' + result['document']['author']
print 'created: ' + result['document']['created']
# Logout
sAuth.logout(token=token)
Download document
#!/usr/bin/python
# -*- coding: utf-8 -*-
import warnings, base64
warnings.simplefilter('ignore', DeprecationWarning)
from SOAPpy import WSDL
# Register WSDL
sAuth = WSDL.Proxy('http://localhost:8080/OpenKM/OKMAuth?wsdl')
sDocument = WSDL.Proxy('http://localhost:8080/OpenKM/OKMDocument?wsdl')
# Login
token = sAuth.login(user='okmAdmin', password='admin')
print 'Token: '+token
docPath = u'/okm:root/path/to/document.pdf'
content = sDocument.getContent(token=token, docPath=docPath, checkout=False);
file = open('document.pdf', 'wb');
file.write(base64.decodestring(content));
file.close();
# Logout
sAuth.logout(token=token)