Skip to content

Unable to load all the api end points or call api in grape api swagger. #897

Description

@RabeyaMuna

I have implemented Grape swagger to load all the Grape APIs to save them as documentation and manipulate data using these API. Unfortunately, I am unable to load any API or redirect it. Even when I tried to check routes, I have seen it is unable to mount with the api's under the resources folder. I need some help to solve the issue.

The is the code structure:

Screenshot 2023-05-09 at 6 32 20 PM

app/api/api.rb

require 'grape-swagger'

module API
  class Base < Grape::API
    include API::ExceptionHandling
    version 'v1', using: :path
    format :json
    prefix :api

    add_swagger_documentation(
      api_version: 'v1',
      hide_documentation_path: true,
      mount_path: '/api/v1/swagger_doc',
      hide_format: true,
      markdown: false,
    )

    include API::Resources
    include CanCan::Ability
    include Grape::Kaminari

    mount API::V1::Resources::Base

   route :any, '*path' do
      error!({ error: 'Not Found', details: "No such route '#{request.path}'", status: 404 }, 404)
    end 
  end
end

app/api/api/v1/resources/base.rb

require 'grape-swagger'

module API
  module V1
    module Resources
      class Base < Grape::API
        include Grape::Kaminari

        version 'v1', using: :accept_version_header
        format :json

        params do
          use :pagination, per_page: 2, max_per_page: 3
        end

        mount Resources::Users
        mount Resources::Orders
        mount Resources::Authors
        mount Resources::Books
        mount Resources::Genres

        add_swagger_documentation format: :json,
                                  hide_documentation_path: true,
                                  info: {
                                    title: 'GRAPE API',
                                    description: 'API to manage ' + 'GRAPE API',
                                  },
                                  api_version: 'v1',
                                  version: true,
                                  doc_version: '1.0.0',
                                  mount_path: 'docs'
      end
    end
  end
end

app/api/api/v1/resources/books.rb

module API
  module V1
    module Resources
      class Books < Resources::Base

        helpers do
          def find_book
            Book.find(params[:id])
          end
        end

        resource :books do
          desc 'Get all books'
          get do
            books = Book.all
            present paginate(books), with: Entities::Book
          end

          desc 'Get a specific book'
          params do
            requires :id, type: Integer, desc: 'ID of the book'
          end
          get ':id', root: 'book' do
            present find_book, with: Entities::Book
          end

          route_param :id, type: Integer do
            desc 'Get authors of a specific book'
            get :book_authors do
              authors = find_book.authors
              present paginate(authors), with: Entities::Author
            end
          end

          desc 'Create a book'
          params do
            requires :book, type: Hash do
              requires :name, type: String, desc: 'Name of the book'
              requires :price, type: Float, desc: 'Price of the book'
              requires :total_copies, type: Integer, desc: 'Total Copies of the book'
            end
          end
          post do
            Book.create!(params[:book])
          end

          desc 'Update a book'
          params do
            requires :book, type: Hash do
              optional :name, type: String, allow_blank: false
              optional :price, type: Float, allow_blank: false
              optional :total_copies, type: Integer
            end
          end
          patch ':id' do
            book = find_book
            book.update!(params[:book])
            present book, with: Entities::Book, message: 'Book Successfully Updated'
          end

          desc 'Delete a book'
          params do
            requires :id, type: Integer
          end
          delete ':id' do
            book = find_book
            if book.destroy
              present book, with: Entities::Book, message: 'Book Successfully Deleted'
            else
              error = { error: book.errors.messages, message: 'Book Failed to Delete' }
              present error
            end
          end
        end
      end
    end
  end
end

config/routes.rb

Rails.application.routes.draw do
  constraints Clearance::Constraints::SignedIn.new { |user| user.admin? } do
    require 'sidekiq/web'
    mount Sidekiq::Web => '/sidekiq'
  end

  # scope '(:locale)', locale: /en|bn/ do
  #   mount API::Base, at: '/'
  #   mount GrapeSwaggerRails::Engine => '/swagger'
  # end

  mount API::Base => '/api'
  mount GrapeSwaggerRails::Engine => '/swagger'
end

config/initializers/swagger.rb

GrapeSwaggerRails.options.app_name = 'GRAPE API'
GrapeSwaggerRails.options.url = 'api/docs.json'
GrapeSwaggerRails.options.before_action do
  GrapeSwaggerRails.options.app_url = request.protocol + request.host_with_port + '/'
#   GrapeSwaggerRails.options.api_key_default_value = request.cookies['token']
end
GrapeSwaggerRails.options.headers['Accept-Version'] = 'v1'
# GrapeSwaggerRails.options.api_key_name = 'Authorization'
GrapeSwaggerRails.options.api_key_type = 'header'
GrapeSwaggerRails.options.hide_api_key_input = true

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions