summaryrefslogtreecommitdiff
path: root/wikibania/api/Query.py
blob: 092847823dbd3a574a4c1d919ba791a92be0100b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import urllib.parse
import urllib.request
import json


class Query:
    def __init__(self, base_url="", params=None, encoding="utf8"):
        if params is None:
            params = {}

        self.base_url = base_url
        self.params = params
        self.encoding = encoding

    def fetch_raw_result(self):
        post_query = urllib.parse.urlencode(self.params)
        post_query = post_query.encode(self.encoding)
        document = urllib.request.urlopen(self.base_url, post_query)
        return document.read().decode(self.encoding)


class JSONQuery(Query):
    def fetch_result(self):
        return json.loads(self.fetch_raw_result())