Skip to content
This repository has been archived by the owner on Oct 13, 2019. It is now read-only.

Commit

Permalink
Base class for all Mollom API classes.
Browse files Browse the repository at this point in the history
- Added MollomBase.
- Moved __service to MollomBase.
- Fixed imports.
  • Loading branch information
itkovian committed Apr 25, 2012
1 parent cd78edb commit eff3ed7
Showing 1 changed file with 53 additions and 15 deletions.
68 changes: 53 additions & 15 deletions Mollom.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,23 +36,61 @@
import urllib import urllib
from ConfigParser import ConfigParser from ConfigParser import ConfigParser


import Util from PyMollom import *


class MollomFault(object): class MollomBase(object):
"""MollomFault encapsulates values returned by the Mollom service that indicate a fault.""" """Base class for the Mollom API classes.

"""
CONNECTION_ERROR = 1000 def __init__(self, public_key, private_key):
UNAUTHORISED_ERROR = 401 """Initialise."""
FORBIDDEN_ERROR = 403 self.public_key = public_key
NOT_FOUND_ERROR = 404 self.private_key = private_key

self.mollom_headers = ['Accept':'application/json;q=0.8, */*;q=0.5']
def __init__(self, code): self.mollom_uri = "%s/%s" % (MOLLOM_SERVER, MOLLOM_VERSION)
super(MollomFault, self).__init__()
self.faultCode = code
def __service(method, path, data=None, maxRetries=0, depth=0):
"""The service method makes the actual call to the Mollom service
on behalf of the public API method.
@type method: the HTTP method (POST, GET, ...)
@type path: the URL path
@type data: dictionary with the data to pass in case of a POST
@type maxRetries: int
@type depth:int
@returns:
- The result of the call, if a server is available.
- None after MOLLOM_RETRIES successive failed retries.
"""


def serverBusy(self): if depth > maxRetries:
# XXX: FIXME return None
return self.faultCode == MollomFault.MOLLOM_CONNECTION_ERROR
# superfluous?
#params = { 'oauth_version': 1.0
# , 'oauth_timestamp': int(time.time())
# , 'oauth_nonce': oauth2.generate_nonce()
# , 'oauth_signature_method': "HMAC-SHA1"
# , 'oauth_consumer_key': self.publicKey
# }

body = urllib.urlencode(data)
consumer = oauth2.Consumer(key=self.public_key, secret=self.private_key)
client = oauth2.Client(consumer)

#request = oauth2.Request( method='GET'
# , url= self.mollomServer + path
# , parameters = params
# , body = body)

resp, content = client.request(self.mollom_uri + path, method, headers=self.mollom_headers, body=body)

if resp.status == 200:
return content
else:
# FIXME: do some error checking here
return None




class MollomAPI(object): class MollomAPI(object):
Expand Down

0 comments on commit eff3ed7

Please sign in to comment.