import json

from django.utils.six.moves.urllib.parse import urlencode
from django.utils.six.moves.urllib.request import urlopen
from import CommandError

def call(method, data, post=False):
    Calls `method` from the DISQUS API with data either in POST or GET.
    Returns deserialized JSON response.
    url = "%s%s" % ('', method)
    if post:
        # POST request
        url += "/"
        data = urlencode(data)
        # GET request
        url += "?%s" % urlencode(data)
        data = ''
    res = json.load(urlopen(url, data))
    if not res['succeeded']:
        raise CommandError("'%s' failed: %s\nData: %s" % (method, res['code'], data))
    return res['message']