def register_item(alias, api_key, myredis, mydao): if not is_valid_key(api_key): raise InvalidApiKeyException if is_registered(alias, api_key): raise ItemAlreadyRegisteredToThisKey registered_item = None (namespace, nid) = alias tiid = item.get_tiid_by_alias(namespace, nid, mydao) if not tiid: if is_over_quota(api_key): analytics.track("CORE", "Raised Exception", { "exception class": "ApiLimitExceededException", "api_key": api_key }) raise ApiLimitExceededException else: tiid = item.create_item(namespace, nid, myredis, mydao) analytics.identify(api_key, {"name": api_key, "api_user": True}) analytics.track(api_key, "Created item because of registration", { "tiid": tiid, "namespace": namespace, "nid": nid, "api_key": api_key }) api_user = get_api_user(api_key) if api_user: registered_item = save_registered_item(alias, api_user) return {"tiid":tiid, "registered_item":registered_item}
def register_item(alias, api_key, myredis, mydao): if not is_valid_key(api_key): raise InvalidApiKeyException if is_registered(alias, api_key): raise ItemAlreadyRegisteredToThisKey registered_item = None (namespace, nid) = alias tiid = item.get_tiid_by_alias(namespace, nid, mydao) if not tiid: if is_over_quota(api_key): analytics.track("CORE", "Raised Exception", { "exception class": "ApiLimitExceededException", "api_key": api_key }) raise ApiLimitExceededException else: tiid = item.create_item(namespace, nid, myredis, mydao) analytics.identify(api_key, {"name": api_key, "api_user": True}) analytics.track( api_key, "Created item because of registration", { "tiid": tiid, "namespace": namespace, "nid": nid, "api_key": api_key }) api_user = get_api_user(api_key) if api_user: registered_item = save_registered_item(alias, api_user) return {"tiid": tiid, "registered_item": registered_item}
def get_item_from_namespace_nid(namespace, nid, format=None, include_history=False): namespace = item_module.clean_id(namespace) nid = item_module.clean_id(nid) include_history = request.args.get("include_history", 0) in ["1", "true", "True"] register = request.args.get("register", 0) in ["1", "true", "True"] callback_name = request.args.get("callback", None) api_key = request.values.get('key') debug_message = "" if register: try: api_user.register_item((namespace, nid), api_key, myredis, mydao) except api_user.ItemAlreadyRegisteredToThisKey: debug_message = u"ItemAlreadyRegisteredToThisKey for key {api_key}".format( api_key=api_key) logger.debug(debug_message) except api_user.ApiLimitExceededException: debug_message = u"ApiLimitExceededException for key {api_key}".format( api_key=api_key) logger.debug(debug_message) tiid = item_module.get_tiid_by_alias(namespace, nid, mydao) if not tiid: if not debug_message: debug_message = "Item not in database. Call POST to register it" # if registration failure, report that info. Else suggest they register. abort_custom(404, debug_message) return get_item_from_tiid(tiid, format, include_history, callback_name)
def tiid(ns, nid): tiid = item_module.get_tiid_by_alias(ns, nid, mydao) if not tiid: abort(404) resp = make_response(json.dumps(tiid, sort_keys=True, indent=4), 303) resp.mimetype = "application/json" return resp
def register_item(alias, api_key, myredis, mydao): if not is_valid_key(api_key, mydao): raise InvalidApiKeyException if is_registered(alias, api_key, mydao): raise ItemAlreadyRegisteredToThisKey (namespace, nid) = alias tiid = item.get_tiid_by_alias(namespace, nid, mydao) if not tiid: if is_over_quota(api_key, mydao): raise ApiLimitExceededException else: tiid = item.create_item(namespace, nid, myredis, mydao) registered = add_registration_data(alias, tiid, api_key, mydao) if registered: mixpanel.track("Create:Register", {"Namespace":namespace, "API Key":api_key}) return tiid
def test_init_item_and_add_aliases(self): item_object = Item() print item_object self.db.session.add(item_object) self.db.session.commit() # we have an item but it has no aliases found_item = Item.query.first() assert_true(len(found_item.tiid) > 10) assert_equals(found_item.aliases, []) test_alias = ("doi", "10.123/abc") (test_namespace, test_nid) = test_alias #make sure nothing there beforehand response = Alias.filter_by_alias(test_alias).first() assert_equals(response, None) new_alias = Alias(alias_tuple=test_alias) print new_alias self.db.session.add(item_object) item_object.aliases = [new_alias] # still not there response = Alias.filter_by_alias(test_alias).first() assert_equals(response, None) self.db.session.commit() # and now poof there it is response = Alias.query.all() assert_equals(response[0].alias_tuple, test_alias) response = Alias.query.filter_by(nid=test_alias[1]).first() assert_equals(response.nid, test_alias[1]) response = Alias.filter_by_alias(test_alias).first() assert_equals(response.alias_tuple, test_alias) response = item_module.get_tiid_by_alias(test_namespace, test_nid) assert_equals(response, found_item.tiid)
def get_item_from_namespace_nid(namespace, nid, format=None, include_history=False): include_history = request.args.get("include_history", 0) in ["1", "true", "True"] register = request.args.get("register", 0) in ["1", "true", "True"] api_key = request.values.get('key') if register: try: logger.debug("api_key is " + api_key) api_user.register_item((namespace, nid), api_key, myredis, mydao) except api_user.ItemAlreadyRegisteredToThisKey: logger.debug("ItemAlreadyRegisteredToThisKey") pass except api_user.ApiLimitExceededException: logger.debug("ApiLimitExceededException") pass tiid = item_module.get_tiid_by_alias(namespace, nid, mydao) if not tiid: abort(404, "Item not in database. Call POST to register it.") return get_item_from_tiid(tiid, format, include_history)
def get_tiid_from_namespace_nid(namespace, nid): tiid = item_module.get_tiid_by_alias(namespace, nid) if not tiid: abort_custom(404, "alias not in database") return make_response(json.dumps({"tiid": tiid}, sort_keys=True, indent=4), 200)
registration_dict = defaultdict(dict) for registration in all_registrations: (timestamp, namespace, nid, api_key) = registration if api_key in ["test", "api-docs", "ekjpt55agtzy10441yv7nh302"]: continue if len(api_key) > 40: continue alias = (namespace, nid) registration_dict[api_key][alias] = {"registered":timestamp} for api_key in registration_dict.keys(): for alias in registration_dict[api_key].keys(): (namespace, nid) = alias tiid = item.get_tiid_by_alias(namespace, nid, None, mydao) if not tiid: print "****************** no tiid, skipping*****************" raw_input("hit enter to continue") continue api_user.register_item(alias, tiid, api_key, mydao) for reg in registration_dict.keys(): print reg print len(registration_dict[reg]) print "\n\n" for reg in registration_dict.keys():