Example #1
0
    def url_to_resource_sequence(e: Operation, resources: list):

        counter = dict()
        for r in RESOURCE_TYPES:
            counter[r] = 1

        # Remove unknown resources from the beginning of the url
        if resources:
            to_remove = []
            for i, r in enumerate(reversed(resources)):

                if r.resource_type in [UNKNOWN_PARAM_RESOURCE] and i + 1 != len(resources) \
                        and (not r.is_param or not ParamUtils.is_necessary_param(r.name)):
                    to_remove.append(r)
                else:
                    break
            for r in to_remove:
                resources.remove(r)

        # Remove unwanted resources
        resources = list(
            filter(
                lambda r: r.resource_type not in
                [VERSION_RESOURCE, ALL_RESOURCE], resources))

        ret = [e.verb]

        if e.operation_id:
            e.operation_id = e.operation_id.replace("post", "")
            e.operation_id = ParamUtils.normalize(e.operation_id)
            if " by " in e.operation_id:
                e.operation_id = e.operation_id[:e.operation_id.index(" by ")]
            words = e.operation_id.split()
            if len(words) > 1 and is_verb(words[0]):
                ret.append("OperationID")

        # ret.append(str("|"))
        for rs in resources:

            if rs.resource_type == SINGLETON:
                rs_coll_id = '{}_{}'.format(COLLECTION, counter[COLLECTION])
                rs_id = '{}_{}'.format(SINGLETON, counter[SINGLETON])
                counter[COLLECTION] += 1
                ret.append(rs_coll_id)
                ret.append(rs_id)
                rs.ids = [rs_coll_id, rs_id]
            else:
                id = '{}_{}'.format(rs.resource_type,
                                    counter[rs.resource_type])
                ret.append(id)
                rs.ids = [id]

            counter[rs.resource_type] += 1

        if len(ret) == 1:
            return None, resources

        ret = [str(i) for i in ret]
        return " ".join(ret), resources
Example #2
0
def translate_collection(method, resources, sample_values):
    if method not in ['get', 'post', 'delete']:
        return None

    if len(resources) != 1:
        return None

        # if ':' in resources[0].name:
        # //v1/{name}:setDefault
        # return None

    if resources[0].resource_type == ACTION_RESOURCE:
        ret = ParamUtils.normalize(resources[0].name)

        for w in ret.split():
            if not ParamUtils.is_necessary_param(w):
                return None

        if not ret or not is_verb(ret.split()[0]) or ret.endswith('ing') or ret.endswith('s'):
            return None

        return ret

    if resources[0].resource_type != COLLECTION:
        return None

    resource = ParamUtils.normalize(resources[0].name)

    for key in ["get ", "set ", "create ", "put ", "delete "]:
        if resource.startswith(key):
            resource = resource[len(key):]

    if is_singular(resource) or ' ' in resource:
        ret = 'get the {}'
    else:
        ret = 'get the list of {}'

    if method == 'post':
        resource = singular(resource)
        ret = 'create a {}'
    elif method == 'delete':
        ret = 'delete all {}'

    return ret.format(resource)