def this_ks_subscribes_to(request, UKCL): ''' This ks is subscribing to a data set in another ks First I store the subscription locally Then I invoke remotely api_subscribe If it works I commit locally ''' UKCL = str(urllib.parse.unquote(UKCL).replace("%2F", "/")) other_ks_uri = KsUrl(UKCL).home() KnowledgeServer.get_remote_ks(other_ks_uri) try: with transaction.atomic(): encoded_UKCL = urllib.parse.urlencode({'': UKCL})[1:] # invoke remote API to subscribe this_ks = KnowledgeServer.this_knowledge_server() url_to_invoke = urllib.parse.urlencode( {'': this_ks.url() + reverse('api_notify')})[1:] ar = ApiResponse() ar.invoke_oks_api(other_ks_uri, 'api_subscribe') + ( "?UKCL=%s&remote_url=%s" % ((encoded_UKCL, url_to_invoke))) if ar.status == ApiResponse.success: # save locally sto = SubscriptionToOther() sto.URL = UKCL sto.first_version_UKCL = ar.content # it contains the UKCL of the first version sto.save() return render(request, 'knowledge_server/export.json', {'json': ar.response}, content_type="application/json") else: return render(request, 'knowledge_server/export.json', {'json': ar.response}, content_type="application/json") except Exception as ex: return HttpResponse(str(ex))
def api_subscribe(request, UKCL, remote_url): ''' #35 parameters: UKCL is the one to which I want to subscribe remote_url the URL this KS has to invoke to notify NEVER CACHED ''' # check the client KS has already subscribed; the check is done using the remote_ks (if any), remote URL otherwise UKCL = urllib.parse.unquote(UKCL) remote_url = urllib.parse.unquote(remote_url) # I want to check the request comes from the same domain as the remote_url # I do this checking that the IP address is the same x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: ip = x_forwarded_for.split(',')[0] else: ip = request.META.get('REMOTE_ADDR') ip_notification = socket.gethostbyname(KsUrl(remote_url).netloc) if ip_notification != ip: return render(request, 'knowledge_server/export.json', {'json': ApiResponse(ApiResponse.failure, "Trying to subscribe from a different IP address", "").json()}, content_type="application/json") # I try to get the remote ks info (if it is a ks) locally remote_ks = KnowledgeServer.get_remote_ks(remote_url) dataset = DataSet.objects.get(UKCL=UKCL) first_version_UKCL = dataset.first_version.UKCL if remote_ks: existing_subscriptions = SubscriptionToThis.objects.filter(first_version_UKCL=first_version_UKCL, remote_ks=remote_ks) else: existing_subscriptions = SubscriptionToThis.objects.filter(first_version_UKCL=first_version_UKCL, remote_url=remote_url) if len(existing_subscriptions) > 0: return render(request, 'knowledge_server/export.json', {'json': ApiResponse(ApiResponse.failure, "Already subscribed").json()}, content_type="application/json") stt = SubscriptionToThis() stt.first_version_UKCL=first_version_UKCL stt.remote_url=remote_url stt.remote_ks = remote_ks stt.save() return render(request, 'knowledge_server/export.json', {'json': ApiResponse(ApiResponse.success, "Subscribed sucessfully", first_version_UKCL).json()}, content_type="application/json")
def api_subscribe(request): ''' #35 parameters: UKCL is the one to which I want to subscribe remote_url the URL this KS has to invoke to notify NEVER CACHED ''' UKCL = request.GET['UKCL'] remote_url = request.GET['remote_url'] # I want to check the request comes from the same domain as the remote_url # I do this checking that the IP address is the same x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: ip = x_forwarded_for.split(',')[0] else: ip = request.META.get('REMOTE_ADDR') ip_notification = socket.gethostbyname(KsUrl(remote_url).netloc) if ip_notification != ip: return render( request, 'knowledge_server/export.json', { 'json': ApiResponse(ApiResponse.failure, "Trying to subscribe from a different IP address", "").json() }, content_type="application/json") ar = ApiResponse(request=request) # I try to get the remote ks info (if it is a ks) locally remote_ks = KnowledgeServer.get_remote_ks(remote_url) dataset = DataSet.objects.get(UKCL=UKCL) first_version_UKCL = dataset.first_version.UKCL # check the client KS has already subscribed; the check is done using the remote_ks (if any), remote URL otherwise if remote_ks: existing_subscriptions = SubscriptionToThis.objects.filter( first_version_UKCL=first_version_UKCL, remote_ks=remote_ks) else: existing_subscriptions = SubscriptionToThis.objects.filter( first_version_UKCL=first_version_UKCL, remote_url=remote_url) if len(existing_subscriptions) > 0: return render( request, 'knowledge_server/export.json', { 'json': ApiResponse(ApiResponse.failure, "Already subscribed").json() }, content_type="application/json") stt = SubscriptionToThis() stt.first_version_UKCL = first_version_UKCL stt.remote_url = remote_url stt.remote_ks = remote_ks stt.save() return render( request, 'knowledge_server/export.json', { 'json': ApiResponse(ApiResponse.success, "Subscribed sucessfully", first_version_UKCL).json() }, content_type="application/json")