|
0
|
1 |
# Create your views here. |
|
67
|
2 |
from core import settings |
|
|
3 |
from django.http.response import HttpResponseNotFound, HttpResponse,\ |
|
|
4 |
HttpResponseBadRequest |
|
|
5 |
from django.views.generic import View |
|
|
6 |
import requests |
|
|
7 |
from requests.exceptions import HTTPError |
|
|
8 |
|
|
|
9 |
class SparqlEndpointProxy(View): |
|
|
10 |
|
|
|
11 |
def get(self, request, wp_lang): |
|
|
12 |
params = request.GET.dict() |
|
|
13 |
if not wp_lang or wp_lang not in settings.WIKIPEDIA_URLS: |
|
|
14 |
return HttpResponseBadRequest("The wp_lang parameter is compulsory and must be in %s" % repr(settings.WIKIPEDIA_URLS.keys())) # @UndefinedVariable |
|
|
15 |
|
|
|
16 |
url = settings.WIKIPEDIA_URLS.get(wp_lang, {}).get('dbpedia_sparql_url', None) # @UndefinedVariable |
|
|
17 |
if not url or not url.startswith("http"): |
|
|
18 |
return HttpResponseNotFound("No or bad url for %s: %s" % (wp_lang,repr(url))) |
|
|
19 |
|
|
|
20 |
try: |
|
|
21 |
resp = requests.get(url, params=params) |
|
|
22 |
return HttpResponse(content=resp.text, content_type=resp.headers.get('content-type')) |
|
|
23 |
except HTTPError as e: |
|
|
24 |
return HttpResponse(unicode(e), status=e.status_code) |
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|