def _api_check(request, *args, **kwargs): # require api_account_id if 'api_account_id' not in request.REQUEST \ or request.REQUEST['api_account_id'] == '': return api_response_invalid(missing_field='api_account_id') # require api_access_key if 'api_access_key' not in request.REQUEST or \ request.REQUEST['api_access_key'] == '': return api_response_invalid(missing_field='api_access_key') # look for target company with matching access key api_account_id = request.REQUEST['api_account_id'] api_access_key = request.REQUEST['api_access_key'] try: company = Company.objects.get(api_account_id=api_account_id, api_access_key=api_access_key) except ObjectDoesNotExist: return HttpResponse('Permission Denied', status=403) # save info on request for view function request.api_info = dict(api_account_id=api_account_id, api_access_key=api_access_key, company=company) # continue to regular view function return view_func(request, *args, **kwargs)
def api_response(success=False, error=None, error_code=None, **kwargs): response = kwargs response['success'] = success if error or error_code: response['error'] = error response['error_code'] = error_code return HttpResponse(json.dumps(response))
def api_response_permission_denied(invalid_field=None): message = "invalid request" if missing_field: message = 'Missing field %s' % missing_field if invalid_field: message = 'Invalid field %s' % invalid_field return HttpResponse(message, status=400)
def recache_matches(request): # replay all matches count = 0 for company in Company.objects.all(): count += 1 company.recache_matches() return HttpResponse("Recached all matches for %d companies" % count)
def json_response(success, mimetype='application/json', **kwargs): """Accepts boolean success and any other keyword arguments and returns an HttpResponse with a json encoded object.""" response = {'success': success} response.update(kwargs) return HttpResponse(json.dumps(response), mimetype=mimetype)
def raw_json_response(response, mimetype='application/json'): """Accepts an object and returns an HttpResponse with it encoded, using the specified mimetype or application/json.""" return HttpResponse(json.dumps(response), mimetype=mimetype)
def _api_get(request, *args, **kwargs): if request.method != 'GET': return HttpResponse("This endpoint only supports GET requests", status=405) return view_func(request, *args, **kwargs)