def report_calls(request): ''' POST endpoint for APIs to report their statistics requires parameters: api, key, calls, date, endpoint & signature if 'api' or 'key' parameter is invalid returns a 404 if signature is bad returns a 400 returns a 200 with content 'OK' if call succeeds ''' api_obj = get_object_or_404(Api, name=request.POST['api']) # check the signature if get_signature(request.POST, api_obj.signing_key) != request.POST['signature']: return HttpResponseBadRequest('bad signature') key_obj = get_object_or_404(Key, key=request.POST['key']) calls = int(request.POST['calls']) try: # use get_or_create to update unique #calls for (date,api,key,endpoint) report,c = Report.objects.get_or_create(date=request.POST['date'], api=api_obj, key=key_obj, endpoint=request.POST['endpoint'], defaults={'calls':calls}) if not c: report.calls = calls report.save() except Exception: raise return HttpResponse('OK')
def report_calls(request): ''' POST endpoint for APIs to report their statistics requires parameters: api, key, calls, date, endpoint & signature if 'api' or 'key' parameter is invalid returns a 404 if signature is bad returns a 400 returns a 200 with content 'OK' if call succeeds ''' api_obj = get_object_or_404(Api, name=request.POST['api']) # check the signature if get_signature(request.POST, api_obj.signing_key) != request.POST['signature']: return HttpResponseBadRequest('bad signature') key_obj = get_object_or_404(Key, key=request.POST['key']) calls = int(request.POST['calls']) try: # use get_or_create to update unique #calls for (date,api,key,endpoint) report, c = Report.objects.get_or_create( date=request.POST['date'], api=api_obj, key=key_obj, endpoint=request.POST['endpoint'], defaults={'calls': calls}) if not c: report.calls = calls report.save() except Exception, e: raise
def check_key(request): ''' POST endpoint determining whether or not a key exists and is valid ''' api_objs = list(Api.objects.filter(name=request.POST['api'])) if not api_objs: return HttpResponseBadRequest('Must specify valid API') # check the signature if get_signature(request.POST, api_objs[0].signing_key) != request.POST['signature']: return HttpResponseBadRequest('bad signature') get_object_or_404(Key, key=request.POST['key'], status='A') return HttpResponse('OK')
def reset_keys(request): ''' POST endpoint to reset API keys for a given API (triggering a request for new keys) ''' api_obj = get_object_or_404(Api, name=request.POST['api']) # check the signature if get_signature(request.POST, api_obj.signing_key) != request.POST['signature']: return HttpResponseBadRequest('bad signature') ReplicatedApiNames = getattr(settings, 'LOCKSMITH_REPLICATED_APIS', []) if api_obj.name in ReplicatedApiNames: for key in Key.objects.all(): replicate_key.delay(key, api_obj) else: api_obj.pub_statuses.update(status=UNPUBLISHED) for key in Key.objects.all(): push_key.delay(key, replicate_too=False) return HttpResponse('OK')
def verify_signature(post): return get_signature(post, settings.LOCKSMITH_SIGNING_KEY) == post['signature']