Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not handle Folders in a collection #5

Open
cdgriffith opened this issue Nov 19, 2018 · 4 comments
Open

Does not handle Folders in a collection #5

cdgriffith opened this issue Nov 19, 2018 · 4 comments

Comments

@cdgriffith
Copy link

https://github.com/TitorX/Postdown/blob/master/postdown/parser.py#L28 will error out here

It is possible for an item to only have have the name and item fields (and can have multiple levels of subfolders)

@adamteale
Copy link

adamteale commented Nov 22, 2018

@cdgriffith this quick hack on the parser.py file seems to get it to export

import json
from .ctor import MDDoc


def get_rows(raw, keys):
    result = list()
    for i in raw:
        result.append([i.get(k, '') for k in keys])
    return result


def parse(in_file, out_file):
    doc = MDDoc()

    with open(in_file) as f:
        collection = json.load(f)

    # The basic info.
    doc.title(collection['info']['name'])
    doc.hr()
    doc.line(collection['info']['description'])
    doc.br()
    doc.hr()

    # API
    apiItem=collection['item']
    for api in apiItem:
        doc.title(api['name'], 2)
        if "item" in api:
            for i in api["item"]:
                if "request" in i:
                    request = i["request"]
        
        url = request['url']['raw'] if isinstance(request['url'], dict) \
            else request['url']
        doc.code_block(
            '{0} {1}'.format(request['method'], url)
        )
        if 'description' in request:
            doc.block(request['description'])
            doc.hr()

        # Request information.
        doc.title('Request', 3)
        doc.comment_begin()
        if isinstance(request['url'], dict):
            # Request Query
            doc.bold('Query')
            try:
                rows = get_rows(
                    request['url']['query'],
                    ['key', 'value', 'description']
                )
                doc.table(['Key', 'Value', 'Description'], rows)
            except:
                print("bad luck")

        # Request Header
        if request['header']:
            doc.bold('Header')
            rows = get_rows(
                request['header'],
                ['key', 'value', 'description']
            )
            doc.table(['Key', 'Value', 'Description'], rows)

        # Request Body
        if request['body']:
            content = request['body'][request['body']['mode']]
            if request['body']['mode'] == 'file' and isinstance(content, dict):
                content = content.get('src', '')

            if content:
                doc.bold('Body')
                if request['body']['mode'] in ['formdata', 'urlencoded']:
                    rows = get_rows(
                        request['body'][request['body']['mode']],
                        ['key', 'value', 'type', 'description']
                    )
                    doc.table(['Key', 'Value', 'Type', 'Description'], rows)
                elif request['body']['mode'] == 'raw':
                    doc.code_block(request['body']['raw'])
                elif request['body']['mode'] == 'file':
                    doc.text(request['body']['file']['src'])

        doc.comment_end()

        # Response example.
        doc.title('Examples:', 3)
        doc.comment_begin()
        
        if 'response' in apiItem:
            if len(apiItem['response']) > 0:
                for response in apiItem['response']:
                    doc.bold('Example: {0}'.format(response['name']))
                    doc.comment_begin()

                    # Original Request
                    request = response['originalRequest']

                    # Request URL
                    url = request['url']['raw'] if isinstance(request['url'], dict) \
                        else request['url']
                    doc.code_block(
                        '{0} {1}'.format(request['method'], url)
                    )

                    # Request Query
                    doc.bold('Request')
                    doc.comment_begin()
                    if isinstance(request['url'], dict):
                        doc.bold('Query')
                        rows = get_rows(
                            request['url']['query'],
                            ['key', 'value', 'description']
                        )
                        doc.table(['Key', 'Value', 'Description'], rows)

                    # Request Header
                    if request['header']:
                        doc.bold('Header')
                        rows = get_rows(
                            request['header'],
                            ['key', 'value', 'description']
                        )
                        doc.table(['Key', 'Value', 'Description'], rows)

                    # Request Body
                    if request['body']:
                        content = request['body'][request['body']['mode']]
                        if request['body']['mode'] == 'file' and \
                                isinstance(content, dict):
                            content = content.get('src', '')

                        if content:
                            doc.bold('Body')
                            if request['body']['mode'] in ['formdata', 'urlencoded']:
                                rows = get_rows(
                                    content,
                                    ['key', 'value', 'type', 'description']
                                )
                                doc.table(
                                    ['Key', 'Value', 'Type', 'Description'], rows
                                )
                            elif request['body']['mode'] == 'raw':
                                doc.code_block(request['body']['raw'])
                            elif request['body']['mode'] == 'file':
                                doc.text(request['body']['file']['src'])

                    doc.comment_end()

                    doc.hr()

                    # Response
                    doc.bold('Response')
                    doc.comment_begin()
                    # doc.bold('Header')
                    # header_rows = [[i['key'], i['value']] for i in response['header']]
                    # doc.table(['Key', 'Value'], header_rows)
                    doc.bold('Body')
                    doc.code_block(json.dumps(json.loads(response['body']), indent=2))
                    doc.comment_end()

                    doc.comment_end()
        doc.comment_end()
        doc.hr()

    with open(out_file, 'w+') as f:
        f.write(doc.output())

@samcrane8
Copy link
Contributor

I'm currently waiting for TitorX to review my pull request, but in the meantime you can checkout my fork here, it handles this issue.

It's not on pip though so you have to clone the source.

@Andromelus
Copy link

@samcrane8 Can you be more explicit on how to use your version ? Which script needs to run ?

@Mehrdad-Dadkhah
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants