Example #1
0
    def get_single_item_from_search(self,
                                    uri,
                                    tcm_type = None,
                                    params = {},
                                    headers = get_json_headers()):
        '''
            This will always return a single item or None type.  May have many responses, so throw an
            error if there is more than one.
            It goes into the "searchResult" tcm_type of response

            Yeah, it's inefficient to create a list, then potentially return the first item as NOT a list.
            But this makes the coding easier and more uniform, so I chose to do that.
        '''

        list = self.get_list_from_search(uri, tcm_type, params = params, headers = headers)

        # if the last attempt to get something returned None, we want to persist that in the last_id,
        # otherwise we may think we're referencing the LAST one, but we'd be getting the one from before
        if len(list) == 0:
            self.store_latest(None)
            return None
        assert len(list) < 2,\
            '''More than one object returned from search.  Don't know which one to return.
                uri: %s
                params: %s
            %s
            '''\
            % (uri, params, jstr(list))

        item = list[0]
        self.store_latest(item)
        return item
Example #2
0
    def log_user_in(self, name):
        headers = get_json_headers(self.get_auth_header(name))
        # log the user in

        return do_put_for_cookie("%s/login" % self.root_path,
                                 "",
                                 headers)
Example #3
0
    def get_list_from_endpoint(self,
                               uri,
                               tcm_type = None,
                               headers = get_json_headers()):
        '''
            This hits an endpoint.  It goes into the ArrayOfXXX tcm_type of response
        '''
        if tcm_type == None:
            tcm_type = self.singular

        response_txt = do_get(uri, headers = headers)

        try:
            array_of_type = json_to_obj(response_txt)[ns(as_arrayof(tcm_type))][0]
            if (len(array_of_type) > 1):
                items = array_of_type[ns(tcm_type)]
                if (not isinstance(items, list)):
                    items = [items]
            else:
                items = []

            return items
        except (KeyError, TypeError) as err:
            assert False, \
                "%s\nDidn't find [%s][0][%s] in\n%s" % \
                (str(err),
                 ns(as_arrayof(tcm_type)),
                 ns(tcm_type),
                 json_pretty(response_txt))
Example #4
0
    def get_single_item_from_endpoint(self,
                                      uri = None,
                                      tcm_type = None,
                                      params = {},
                                      headers = get_json_headers()):
        '''
            This hits an endpoint.  No searchResult or ArrayOfXXXX part here
        '''

        if uri == None:
            uri = self.root_path

        if tcm_type == None:
            tcm_type = self.singular

        response_txt = do_get(uri, params = params, headers = headers)
        tcm_obj = json_to_obj(response_txt)
        try:
            item = tcm_obj[ns(tcm_type)][0]

            # we only want to store this latest item, if it's an object of this type.  If this
            # model is doing a search for a different type of item (like percent complete object
            # of "CategoryValueInfo" then we don't want to store it as "latest item"
            if uri == self.root_path:
                self.store_latest(item)
            return item

        except KeyError:
            assert False, "%s\nDidn't find %s in %s" % (str(KeyError), ns(tcm_type), jstr(tcm_obj))
Example #5
0
    def get_single_item_from_endpoint(self,
                                      uri=None,
                                      tcm_type=None,
                                      params={},
                                      headers=get_json_headers()):
        '''
            This hits an endpoint.  No searchResult or ArrayOfXXXX part here
        '''

        if uri == None:
            uri = self.root_path

        if tcm_type == None:
            tcm_type = self.singular

        response_txt = do_get(uri, params=params, headers=headers)
        tcm_obj = json_to_obj(response_txt)
        try:
            item = tcm_obj[ns(tcm_type)][0]

            # we only want to store this latest item, if it's an object of this type.  If this
            # model is doing a search for a different type of item (like percent complete object
            # of "CategoryValueInfo" then we don't want to store it as "latest item"
            if uri == self.root_path:
                self.store_latest(item)
            return item

        except KeyError:
            assert False, "%s\nDidn't find %s in %s" % (
                str(KeyError), ns(tcm_type), jstr(tcm_obj))
Example #6
0
    def get_list_from_endpoint(self,
                               uri,
                               tcm_type=None,
                               params={},
                               headers=get_json_headers()):
        '''
            This hits an endpoint.  It goes into the ArrayOfXXX tcm_type of response
        '''
        if tcm_type == None:
            tcm_type = self.singular

        response_txt = do_get(uri, params=params, headers=headers)

        try:
            array_of_type = json_to_obj(response_txt)[ns(
                as_arrayof(tcm_type))][0]
            if (len(array_of_type) > 1):
                items = array_of_type[ns(tcm_type)]
                if (not isinstance(items, list)):
                    items = [items]
            else:
                items = []

            return items
        except (KeyError, TypeError) as err:
            assert False, \
                "%s\nDidn't find [%s][0][%s] in\n%s" % \
                (str(err),
                 ns(as_arrayof(tcm_type)),
                 ns(tcm_type),
                 json_pretty(response_txt))
Example #7
0
    def get_single_item_from_search(self,
                                    uri,
                                    tcm_type=None,
                                    params={},
                                    headers=get_json_headers()):
        '''
            This will always return a single item or None type.  May have many responses, so throw an
            error if there is more than one.
            It goes into the "searchResult" tcm_type of response

            Yeah, it's inefficient to create a list, then potentially return the first item as NOT a list.
            But this makes the coding easier and more uniform, so I chose to do that.
        '''

        list = self.get_list_from_search(uri,
                                         tcm_type,
                                         params=params,
                                         headers=headers)

        # if the last attempt to get something returned None, we want to persist that in the last_id,
        # otherwise we may think we're referencing the LAST one, but we'd be getting the one from before
        if len(list) == 0:
            self.store_latest(None)
            return None
        assert len(list) < 2,\
            '''More than one object returned from search.  Don't know which one to return.
                uri: %s
                params: %s
            %s
            '''\
            % (uri, params, jstr(list))

        item = list[0]
        self.store_latest(item)
        return item
Example #8
0
def log_in_with_credentials(step):
    user = step.hashes[0]
    headers = get_json_headers(get_auth_header(user["email"],
                                               user["password"]))

    cookie = do_put_for_cookie(UserModel().root_path + "/" + "login", "",
                               headers)[1]
    world.auth_cookie = cookie
Example #9
0
    def get_list_from_search(self,
                             uri,
                             tcm_type = None,
                             plural_tcm_type = None,
                             params = {},
                             headers = get_json_headers()):
        '''
            This will always return an array.  May have many, one or no items in it
            it goes into the "searchResult" tcm_type of response
        '''
        if tcm_type == None:
            tcm_type = self.singular
            plural_tcm_type = self.plural

        if plural_tcm_type == None:
            plural_tcm_type = plural(tcm_type)

        response_txt = do_get(uri, params, headers)

        sr_field = ns("searchResult")
        tcm_type = ns(tcm_type)
        pl_type = ns(plural_tcm_type)

        try:
            sr = json_to_obj(response_txt)[sr_field][0]
            if (sr[ns("totalResults")] > 0):
                items = sr[pl_type][tcm_type]
                if (not isinstance(items, list)):
                    items = [items]
            else:
                items = []

            return items
        except (KeyError, TypeError) as err:
            assert False, \
                "%s\nDidn't find [%s][0][%s][%s] in\n%s" % \
                (str(err),
                 sr_field,
                 pl_type,
                 ns(tcm_type),
                 json_pretty(response_txt))
Example #10
0
    def get_list_from_search(self,
                             uri,
                             tcm_type=None,
                             plural_tcm_type=None,
                             params={},
                             headers=get_json_headers()):
        '''
            This will always return an array.  May have many, one or no items in it
            it goes into the "searchResult" tcm_type of response
        '''
        if tcm_type == None:
            tcm_type = self.singular
            plural_tcm_type = self.plural

        if plural_tcm_type == None:
            plural_tcm_type = plural(tcm_type)

        response_txt = do_get(uri, params, headers)

        sr_field = ns("searchResult")
        tcm_type = ns(tcm_type)
        pl_type = ns(plural_tcm_type)

        try:
            sr = json_to_obj(response_txt)[sr_field][0]
            if (sr[ns("totalResults")] > 0):
                items = sr[pl_type][tcm_type]
                if (not isinstance(items, list)):
                    items = [items]
            else:
                items = []

            return items
        except (KeyError, TypeError) as err:
            assert False, \
                "%s\nDidn't find [%s][0][%s][%s] in\n%s" % \
                (str(err),
                 sr_field,
                 pl_type,
                 ns(tcm_type),
                 json_pretty(response_txt))
Example #11
0
    def get_single_item_from_endpoint(self,
                                      uri = None,
                                      tcm_type = None,
                                      headers = get_json_headers()):
        '''
            This hits an endpoint.  No searchResult or ArrayOfXXXX part here
        '''

        if uri == None:
            uri = self.root_path

        if tcm_type == None:
            tcm_type = self.singular

        response_txt = do_get(uri, headers = headers)
        tcm_obj = json_to_obj(response_txt)
        try:
            item = tcm_obj[ns(tcm_type)][0]
            self.store_latest(item)
            return item

        except KeyError:
            assert False, "%s\nDidn't find %s in %s" % (str(KeyError), ns(tcm_type), jstr(tcm_obj))
Example #12
0
def log_in_with_credentials(step):
    user = step.hashes[0]
    headers = get_json_headers(get_auth_header(user["email"], user["password"]))

    cookie = do_put_for_cookie(UserModel().root_path + "/" + "login", "", headers)[1]
    world.auth_cookie = cookie
Example #13
0
    def log_user_in(self, name):
        headers = get_json_headers(self.get_auth_header(name))
        # log the user in

        return do_put_for_cookie("%s/login" % self.root_path, "", headers)