예제 #1
0
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)
예제 #2
0
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)
예제 #3
0
def item_namespace_post(namespace, nid):
    namespace = item_module.clean_id(namespace)
    nid = item_module.clean_id(nid)

    api_key = request.values.get('key')
    try:
        api_user.register_item((namespace, nid), api_key, myredis, mydao)
        response_code = 201 # Created
    except api_user.ItemAlreadyRegisteredToThisKey:
        response_code = 200
    except api_user.ApiLimitExceededException:
        abort_custom(403, "Registration limit exceeded. Contact [email protected] to discuss options.")

    resp = make_response(json.dumps("ok"), response_code)
    return resp
예제 #4
0
    def test_register_item_success(self):
        api_key = "SFUlqzam8"
        response = api_user.is_registered(self.test_alias, api_key, self.d)
        assert_equals(response, False)

        tiid = api_user.register_item(self.test_alias, api_key, self.r, self.d)
        response = api_user.is_registered(self.test_alias, api_key, self.d)
        assert_equals(response, True)
예제 #5
0
def item_namespace_post(namespace, nid):
    namespace = item_module.clean_id(namespace)
    nid = item_module.clean_id(nid)

    api_key = request.values.get('key')
    try:
        api_user.register_item((namespace, nid), api_key, myredis, mydao)
        response_code = 201  # Created
    except api_user.ItemAlreadyRegisteredToThisKey:
        response_code = 200
    except api_user.ApiLimitExceededException:
        abort_custom(
            403,
            "Registration limit exceeded. Contact [email protected] to discuss options."
        )

    resp = make_response(json.dumps("ok"), response_code)
    return resp
예제 #6
0
    def test_register_item_success(self):
        existing_api_key = self.existing_api_user.api_key

        response = api_user.is_registered(self.test_alias, existing_api_key)
        assert_equals(response, False)

        response = api_user.register_item(self.test_alias, existing_api_key, self.r, self.d)
        assert_equals(response["registered_item"].alias, ('doi', '10.1371/journal.pcbi.1'))

        response = api_user.is_registered(self.test_alias, existing_api_key)
        assert_equals(response, True)
예제 #7
0
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)
예제 #8
0
    def test_register_item_success(self):
        existing_api_key = self.existing_api_user.api_key

        response = api_user.is_registered(self.test_alias, existing_api_key)
        assert_equals(response, False)

        response = api_user.register_item(self.test_alias, existing_api_key,
                                          self.r, self.d)
        assert_equals(response["registered_item"].alias,
                      ('doi', '10.1371/journal.pcbi.1'))

        response = api_user.is_registered(self.test_alias, existing_api_key)
        assert_equals(response, True)
예제 #9
0
    def test_is_over_quota(self):
        api_key = self.existing_api_user.api_key
        response = api_user.is_over_quota(api_key)
        assert_equals(response, False)

        for x in ["a", "b", "c"]:  # max_registered_items was set to 3 for this test api_user
            try:
                response = api_user.register_item(("doi", "10."+x), api_key, self.r, self.d)
                print response["registered_item"]
                self.db.session.add(response["registered_item"])
            except ApiLimitExceededException:
                pass

        self.db.session.commit()

        response = api_user.is_over_quota(api_key)
        assert_equals(response, True)
예제 #10
0
    def test_is_over_quota(self):
        api_key = self.existing_api_user.api_key
        response = api_user.is_over_quota(api_key)
        assert_equals(response, False)

        for x in [
                "a", "b", "c"
        ]:  # max_registered_items was set to 3 for this test api_user
            try:
                response = api_user.register_item(("doi", "10." + x), api_key,
                                                  self.r, self.d)
                print response["registered_item"]
                self.db.session.add(response["registered_item"])
            except ApiLimitExceededException:
                pass

        self.db.session.commit()

        response = api_user.is_over_quota(api_key)
        assert_equals(response, True)
예제 #11
0
 def test_register_item_invalid_key(self):
     api_user.register_item(self.test_alias, "INVALID_KEY", self.r, self.d)
    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():
    print reg
    for key in registration_dict[reg].keys():
        print key
예제 #13
0
 def test_register_item_invalid_key(self):
     api_user.register_item(self.test_alias, "INVALID_KEY", self.r, self.d)