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
O
Oswaldo Ferreira 已提交
14
    end
N
Nihad Abbasov 已提交
15

16 17
    before { allow_access_with_scope :api }

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

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

C
Connor Shea 已提交
26
    # Retain 405 error rather than a 500 error for Grape 0.15.0+.
27 28 29 30 31
    # 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 已提交
32 33
    rescue_from Grape::Exceptions::Base do |e|
      error! e.message, e.status, e.headers
34 35
    end

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

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

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

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

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