|
Revision 1083, 1.3 kB
(checked in by piyawat, 7 months ago)
|
fix doc for python binding
|
| Line | |
|---|
| 1 |
"""Python library to interface with KnowledgeVolution Application Programming Interface (API)""" |
|---|
| 2 |
|
|---|
| 3 |
VERSION = 0.01 |
|---|
| 4 |
|
|---|
| 5 |
import httplib, urllib, base64 |
|---|
| 6 |
|
|---|
| 7 |
class Journal(object): |
|---|
| 8 |
|
|---|
| 9 |
def __init__(self, username, password, server, port=80): |
|---|
| 10 |
self.username = username |
|---|
| 11 |
self.password = password |
|---|
| 12 |
self.server = server |
|---|
| 13 |
self.port = port |
|---|
| 14 |
|
|---|
| 15 |
def post_journal_entry(self, content, visibility=2, verbose=False): |
|---|
| 16 |
params = urllib.urlencode({'journal_entry[content]': content, 'journal_entry[visibility]': visibility}) |
|---|
| 17 |
headers = {'Content-type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain', |
|---|
| 18 |
'Authorization': 'Basic %s' % base64.b64encode('%s:%s' % (self.username, self.password))} |
|---|
| 19 |
|
|---|
| 20 |
conn = httplib.HTTPConnection('%s:%s' % (self.server, self.port)) |
|---|
| 21 |
conn.request('POST', '/journals/%s/entries' % self.username, params, headers) |
|---|
| 22 |
|
|---|
| 23 |
resp = conn.getresponse() |
|---|
| 24 |
if resp.status == 400: |
|---|
| 25 |
return False |
|---|
| 26 |
elif resp.status == 302: |
|---|
| 27 |
new_location = resp.getheader('Location', None) |
|---|
| 28 |
if verbose: |
|---|
| 29 |
print '%s: %s' % (resp.status, new_location) |
|---|
| 30 |
if new_location.find('/journals/%s' % self.username) >= 0: |
|---|
| 31 |
return True |
|---|
| 32 |
else: |
|---|
| 33 |
return False |
|---|
| 34 |
else: |
|---|
| 35 |
return False |
|---|