From bdf9099df8c2a4636b0ad0e710b73330877eef37 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Fri, 24 Oct 2014 19:59:05 +0200 Subject: Very cleaner, much class, such readable, wow --- wikibania/api/Query.py | 24 ++++++++++++++++++++++++ wikibania/api/__init__.py | 1 + 2 files changed, 25 insertions(+) create mode 100644 wikibania/api/Query.py create mode 100644 wikibania/api/__init__.py (limited to 'wikibania/api') diff --git a/wikibania/api/Query.py b/wikibania/api/Query.py new file mode 100644 index 0000000..0928478 --- /dev/null +++ b/wikibania/api/Query.py @@ -0,0 +1,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()) diff --git a/wikibania/api/__init__.py b/wikibania/api/__init__.py new file mode 100644 index 0000000..792d600 --- /dev/null +++ b/wikibania/api/__init__.py @@ -0,0 +1 @@ +# -- cgit v1.2.3