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

Failed to load API definition. Errors Hide Fetch errorFailed to fetch /apispec_1.json #572

Open
scheung38 opened this issue Jun 1, 2023 · 1 comment

Comments

@scheung38
Copy link

scheung38 commented Jun 1, 2023

This is app/init.py:

"""Initialize Flask app."""
from flask import Flask, redirect

from flasgger import Swagger
from flask_restful import Api

def create_app():
    """Create Flask application."""
    app = Flask(__name__, instance_relative_config=True)

    with app.app_context():
        from .generator import rss_content_gen_api
        from .version import version_api

        # Register Blueprints
        # app.register_blueprint(content_gen_api.generator_bp)
        app.register_blueprint(rss_content_gen_api.rss_generator_bp)
        app.register_blueprint(version_api.version_bp)

        # # Initialize Swagger
        swag = Swagger(app)

        print("swag :", swag)
        return app

This is my app/version/version_api.py:

from flask import Blueprint, jsonify, request, abort

import os
from pathlib import Path
import logging

from flask_restful import Api, Resource
from flasgger.utils import swag_from

version_bp = Blueprint("version_bp", __name__)

@version_bp.route("/version", methods=["GET"])
@swag_from("versions.yml")
def get_version():
    """Example endpoint returning a list of colors by palette
    This is using docstrings for specifications.
    ---
    parameters:
      - name: palette
        in: path
        type: string
        enum: ['all', 'rgb', 'cmyk']
        required: true
        default: all
    definitions:
      Palette:
        type: object
        properties:
          palette_name:
            type: array
            items:
              $ref: '#/definitions/Color'
      Color:
        type: string
    responses:
      200:
        description: A list of colors (may be filtered by palette)
        schema:
          $ref: '#/definitions/Palette'
        examples:
          rgb: ['red', 'green', 'blue']
    """
    try:
        version_file_path = Path("/home/appuser/version.txt")

        if not version_file_path.exists():
            abort(404, "Version file not found!")

        with version_file_path.open("r") as f:
            version_info = [line.strip() for line in f]

        return jsonify(version_info)

    except Exception as e:
        print(e)


if __name__ == "__main__":
    version_bp.run()

Dockefile at root:

# Use an official Python runtime as a parent image
FROM python:3.9

# Define environment variable
ENV FLASK_APP=run.py
ENV FLASK_PORT 5000
ENV PATH="/home/appuser/.local/bin:${PATH}"

# Create a non-root user and also directory of the same "appuser"
RUN useradd -m appuser

# Set the working directory to /home/appuser
WORKDIR /home/appuser

# Change to non-root user
USER appuser

# Create the log folder
RUN mkdir -p log

# Create the app.log file
RUN touch log/app.log

# Set ownership and permissions for log folder
RUN chown -R appuser:appuser log

# Copy only the requirements.txt first to leverage Docker cache
# This layer is recreated only when requirements.txt changes
COPY --chown=appuser:appuser requirements.txt ./

# Replace the access token placeholder in requirements.txt
ARG GITLAB_ACCESS_TOKEN
RUN pip install --no-cache-dir -r requirements.txt
# --trusted-host pypi.python.org

# Then copy the rest of the application code
# This layer is recreated whenever a file in your project changes
COPY --chown=appuser:appuser . .

# Expose the Flask port to the world
EXPOSE $FLASK_PORT

# Run the command to start the Flask app
CMD flask run --host=0.0.0.0 --port=$FLASK_PORT

Terminal:

docker build --build-arg GITLAB_ACCESS_TOKEN=$GITLAB_ACCESS_TOKEN -t generator:1 .

docker run -p 5001:5000 generator:1

Then:

http://127.0.0.1:5001/apidocs

Parts of requirements.txt:

flasgger==0.9.7.1
Flask==2.3.2
Flask-RESTful==0.3.10

Why am I getting within Insomnia:

Screenshot 2023-06-02 at 00 05 13

app/version/versions.yml:

Get a Version by it's branch and commit hash
---
tags:
  - version
produces:
  - application/json
  - application/xml
parameters:
  - in: path
    name: version_id
    description: ID of the Version to get
    type: integer
    required: true
responses:
  200:
    description: OK
    schema:
      type: string

Logs:

172.17.0.1 - - [01/Jun/2023 19:03:13] "GET /apidocs HTTP/1.1" 308 -
172.17.0.1 - - [01/Jun/2023 19:03:13] "GET /apidocs/ HTTP/1.1" 200 -
172.17.0.1 - - [01/Jun/2023 19:03:14] "GET /flasgger_static/lib/jquery.min.js HTTP/1.1" 304 -
172.17.0.1 - - [01/Jun/2023 19:03:14] "GET /flasgger_static/swagger-ui-bundle.js HTTP/1.1" 304 -
172.17.0.1 - - [01/Jun/2023 19:03:14] "GET /flasgger_static/swagger-ui.css HTTP/1.1" 304 -
172.17.0.1 - - [01/Jun/2023 19:03:14] "GET /flasgger_static/swagger-ui-standalone-preset.js HTTP/1.1" 304 -
172.17.0.1 - - [01/Jun/2023 19:03:14] "GET /apispec_1.json HTTP/1.1" 200 -
172.17.0.1 - - [01/Jun/2023 19:03:15] "GET /apidocs HTTP/1.1" 308 -
172.17.0.1 - - [01/Jun/2023 19:03:15] "GET /apidocs/ HTTP/1.1" 200 -
172.17.0.1 - - [01/Jun/2023 19:03:15] "GET /flasgger_static/swagger-ui-standalone-preset.js HTTP/1.1" 304 -
172.17.0.1 - - [01/Jun/2023 19:03:15] "GET /flasgger_static/swagger-ui.css HTTP/1.1" 304 -
172.17.0.1 - - [01/Jun/2023 19:03:15] "GET /flasgger_static/lib/jquery.min.js HTTP/1.1" 304 -
172.17.0.1 - - [01/Jun/2023 19:03:15] "GET /flasgger_static/swagger-ui-bundle.js HTTP/1.1" 304 -
172.17.0.1 - - [01/Jun/2023 19:03:15] "GET /apispec_1.json HTTP/1.1" 200 -

EDIT:

It works in Chrome browser:

Screenshot 2023-06-02 at 00 45 35

But why inside Insomnia I am still getting the above?

@fn-hide
Copy link

fn-hide commented Jun 19, 2023

same issue

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

2 participants