Пример #1
0
def createDoc(doc, HYDRUS_SERVER_URL=None, API_NAME=None):
    """Create the HydraDoc object from the API Documentation."""
    # Check @id
    try:
        id_ = doc["@id"]
    except KeyError:
        raise SyntaxError("The API Documentation must have [@id]")

    # Extract base_url, entrypoint and API name
    matchObj = re.match(r'(.*)://(.*)/(.*)/vocab#?', id_, re.M | re.I)
    if matchObj:
        base_url = matchObj.group(1) + '://' + matchObj.group(2) + '/'
        entrypoint = matchObj.group(3)

    # Syntax checks
    else:
        raise SyntaxError(
            "The '@id' of the Documentation must be of the form:\n'[protocol] :// [base url] / [entrypoint] / vocab'"
        )
    try:
        desc = doc["description"]
    except KeyError:
        raise SyntaxError("The API Documentation must have [description]")
    try:
        title = doc["title"]
    except KeyError:
        raise SyntaxError("The API Documentation must have [title]")
    try:
        supportedClass = doc["supportedClass"]
    except KeyError:
        raise SyntaxError("The API Documentation must have [supportedClass]")
    try:
        context = doc["@context"]
    except KeyError:
        raise SyntaxError("The API Documentation must have [@context]")
    try:
        possibleStatus = doc["possibleStatus"]
    except KeyError:
        raise SyntaxError("The API Documentation must have [possibleStatus]")

    # EntryPoint object
    entrypoint_obj = getEntrypoint(
        doc)  # getEntrypoint checks if all classes have @id

    # Main doc object
    if HYDRUS_SERVER_URL is not None and API_NAME is not None:
        apidoc = HydraDoc(API_NAME, title, desc, API_NAME, HYDRUS_SERVER_URL)
    else:
        apidoc = HydraDoc(entrypoint, title, desc, entrypoint, base_url)

    # additional context entries
    for entry in context:
        apidoc.add_to_context(entry, context[entry])

    # add all parsed_classes
    for class_ in supportedClass:
        class_obj, collection = createClass(entrypoint_obj, class_)
        if class_obj:
            apidoc.add_supported_class(class_obj, collection=collection)

    # add possibleStatus
    for status in possibleStatus:
        status_obj = createStatus(status)
        apidoc.add_possible_status(status_obj)

    apidoc.add_baseResource()
    apidoc.add_baseCollection()
    apidoc.gen_EntryPoint()
    return apidoc
Пример #2
0
def create_doc(doc: Dict[str, Any],
               HYDRUS_SERVER_URL: str = None,
               API_NAME: str = None) -> HydraDoc:
    """Create the HydraDoc object from the API Documentation."""
    # Check @id
    try:
        id_ = doc["@id"]
    except KeyError:
        raise SyntaxError("The API Documentation must have [@id]")

    # Extract base_url, entrypoint and API name
    match_obj = re.match(r'(.*)://(.*)/(.*)/vocab#?', id_, re.M | re.I)
    if match_obj:
        base_url = "{0}://{1}/".format(match_obj.group(1), match_obj.group(2))
        entrypoint = match_obj.group(3)

    # Syntax checks
    else:
        raise SyntaxError(
            "The '@id' of the Documentation must be of the form:\n"
            "'[protocol] :// [base url] / [entrypoint] / vocab'")
    doc_keys = {
        "description": False,
        "title": False,
        "supportedClass": False,
        "@context": False,
        "possibleStatus": False
    }
    result = {}
    for k, literal in doc_keys.items():
        result[k] = input_key_check(doc, k, "doc", literal)

    # EntryPoint object
    # getEntrypoint checks if all classes have @id
    entrypoint_obj = get_entrypoint(doc)

    # Main doc object
    if HYDRUS_SERVER_URL is not None and API_NAME is not None:
        apidoc = HydraDoc(API_NAME, result["title"], result["description"],
                          API_NAME, HYDRUS_SERVER_URL)
    else:
        apidoc = HydraDoc(entrypoint, result["title"], result["description"],
                          entrypoint, base_url)

    # additional context entries
    for entry in result["@context"]:
        apidoc.add_to_context(entry, result["@context"][entry])

    # add all parsed_classes
    for class_ in result["supportedClass"]:
        class_obj, collection, collection_path = create_class(
            entrypoint_obj, class_)
        if class_obj:
            apidoc.add_supported_class(class_obj,
                                       collection=collection,
                                       collection_path=collection_path)

    # add possibleStatus
    for status in result["possibleStatus"]:
        status_obj = create_status(status)
        apidoc.add_possible_status(status_obj)

    apidoc.add_baseResource()
    apidoc.add_baseCollection()
    apidoc.gen_EntryPoint()
    return apidoc