api.rb 2.6 KB
Newer Older
1
module API
N
Nihad Abbasov 已提交
2
  class API < Grape::API
V
Valery Sizov 已提交
3
    include APIGuard
O
Oswaldo Ferreira 已提交
4 5 6 7

    version %w(v3 v4), using: :path

    version 'v3', using: :path do
8
      mount ::API::V3::DeployKeys
9
      mount ::API::V3::Issues
10
      mount ::API::V3::Members
11
      mount ::API::V3::MergeRequests
O
Oswaldo Ferreira 已提交
12
      mount ::API::V3::Projects
13
      mount ::API::V3::ProjectSnippets
14
      mount ::API::V3::Templates
O
Oswaldo Ferreira 已提交
15
    end
N
Nihad Abbasov 已提交
16

17 18
    before { allow_access_with_scope :api }

19 20 21 22
    rescue_from Gitlab::Access::AccessDeniedError do
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
    end

N
Nihad Abbasov 已提交
23
    rescue_from ActiveRecord::RecordNotFound do
24
      rack_response({ 'message' => '404 Not found' }.to_json, 404)
N
Nihad Abbasov 已提交
25 26
    end

C
Connor Shea 已提交
27
    # Retain 405 error rather than a 500 error for Grape 0.15.0+.
28 29 30 31 32
    # https://github.com/ruby-grape/grape/blob/a3a28f5b5dfbb2797442e006dbffd750b27f2a76/UPGRADING.md#changes-to-method-not-allowed-routes
    rescue_from Grape::Exceptions::MethodNotAllowed do |e|
      error! e.message, e.status, e.headers
    end

C
Connor Shea 已提交
33 34
    rescue_from Grape::Exceptions::Base do |e|
      error! e.message, e.status, e.headers
35 36
    end

F
Felix Gilcher 已提交
37
    rescue_from :all do |exception|
S
Stan Hu 已提交
38
      handle_api_exception(exception)
39 40
    end

N
Nihad Abbasov 已提交
41
    format :json
42 43
    content_type :txt, "text/plain"

44
    # Ensure the namespace is right, otherwise we might load Grape::API::Helpers
S
Stan Hu 已提交
45
    helpers ::SentryHelper
46 47
    helpers ::API::Helpers

48
    # Keep in alphabetical order
49
    mount ::API::AccessRequests
50
    mount ::API::AwardEmoji
R
Robert Schilling 已提交
51
    mount ::API::Boards
52
    mount ::API::Branches
53
    mount ::API::BroadcastMessages
54 55
    mount ::API::Builds
    mount ::API::Commits
R
Robert Schilling 已提交
56
    mount ::API::CommitStatuses
57
    mount ::API::DeployKeys
Z
Z.J. van de Weg 已提交
58
    mount ::API::Deployments
59
    mount ::API::Environments
60 61 62
    mount ::API::Files
    mount ::API::Groups
    mount ::API::Internal
63
    mount ::API::Issues
64 65
    mount ::API::Keys
    mount ::API::Labels
66
    mount ::API::Lint
67
    mount ::API::Members
68
    mount ::API::MergeRequestDiffs
R
Robert Schilling 已提交
69
    mount ::API::MergeRequests
70 71
    mount ::API::Milestones
    mount ::API::Namespaces
72
    mount ::API::Notes
73
    mount ::API::NotificationSettings
Z
Z.J. van de Weg 已提交
74
    mount ::API::Pipelines
75
    mount ::API::ProjectHooks
76
    mount ::API::Projects
R
Robert Schilling 已提交
77
    mount ::API::ProjectSnippets
78 79
    mount ::API::Repositories
    mount ::API::Runners
80
    mount ::API::Services
81
    mount ::API::Session
82
    mount ::API::Settings
83
    mount ::API::SidekiqMetrics
84
    mount ::API::Snippets
85 86
    mount ::API::Subscriptions
    mount ::API::SystemHooks
87
    mount ::API::Tags
Z
ZJ van de Weg 已提交
88
    mount ::API::Templates
D
Douglas Barbosa Alexandre 已提交
89
    mount ::API::Todos
90
    mount ::API::Triggers
91
    mount ::API::Users
92
    mount ::API::Variables
R
Robert Schilling 已提交
93
    mount ::API::Version
94 95

    route :any, '*path' do
96
      error!('404 Not Found', 404)
97
    end
N
Nihad Abbasov 已提交
98
  end
99
end