Skip to content

Instantly share code, notes, and snippets.

@kosmikko
Created September 29, 2011 13:14
Show Gist options
  • Select an option

  • Save kosmikko/1250710 to your computer and use it in GitHub Desktop.

Select an option

Save kosmikko/1250710 to your computer and use it in GitHub Desktop.
Mollom API Python (GAE) wrapper
import urllib
from google.appengine.api import urlfetch
from gdata import oauth
class MollomRequest(object):
API_URL = 'http://rest.mollom.com/v1/'
# mollom public API key
CONSUMER_KEY = 'xxx'
# mollom private API key
CONSUMER_SECRET = 'xxx'
def do_request(self, url, http_method='GET', post_data=None):
url = "%s%s" % (self.API_URL, url)
consumer = oauth.OAuthConsumer(self.CONSUMER_KEY, self.CONSUMER_SECRET)
request = oauth.OAuthRequest.from_consumer_and_token(
consumer,
http_method=http_method,
http_url=url,
parameters=post_data)
request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(),
consumer,
None)
headers = request.to_header()
headers['Accept'] = 'application/json;q=0.8, */*;q=0.5'
result = urlfetch.fetch(url,
payload=None if post_data is None else urllib.urlencode(post_data),
method=urlfetch.GET if request.http_method == "GET" else urlfetch.POST,
headers=headers,
)
if result.status_code == 200:
return result.content
raise RuntimeError("fail to access resource, status=%d, url=%s" % (result.status_code, url))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment