api.rb 3.67 KB
Newer Older
1
module API
Nihad Abbasov's avatar
Nihad Abbasov committed
2
  class API < Grape::API
Valery Sizov's avatar
Valery Sizov committed
3
    include APIGuard
4 5 6 7

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

    version 'v3', using: :path do
8 9
      helpers ::API::V3::Helpers

Robert Schilling's avatar
Robert Schilling committed
10
      mount ::API::V3::AwardEmoji
Robert Schilling's avatar
Robert Schilling committed
11 12
      mount ::API::V3::Boards
      mount ::API::V3::Branches
Robert Schilling's avatar
Robert Schilling committed
13
      mount ::API::V3::BroadcastMessages
14
      mount ::API::V3::Builds
15
      mount ::API::V3::Commits
16
      mount ::API::V3::DeployKeys
Robert Schilling's avatar
Robert Schilling committed
17
      mount ::API::V3::Environments
18
      mount ::API::V3::Files
Robert Schilling's avatar
Robert Schilling committed
19
      mount ::API::V3::Groups
20
      mount ::API::V3::Issues
Robert Schilling's avatar
Robert Schilling committed
21
      mount ::API::V3::Labels
22
      mount ::API::V3::Members
Robert Schilling's avatar
Robert Schilling committed
23
      mount ::API::V3::MergeRequestDiffs
24
      mount ::API::V3::MergeRequests
25
      mount ::API::V3::Notes
26
      mount ::API::V3::Pipelines
Robert Schilling's avatar
Robert Schilling committed
27
      mount ::API::V3::ProjectHooks
28
      mount ::API::V3::Milestones
29
      mount ::API::V3::Projects
30
      mount ::API::V3::ProjectSnippets
Robert Schilling's avatar
Robert Schilling committed
31
      mount ::API::V3::Repositories
Robert Schilling's avatar
Robert Schilling committed
32 33
      mount ::API::V3::Runners
      mount ::API::V3::Services
34
      mount ::API::V3::Settings
35
      mount ::API::V3::Snippets
36
      mount ::API::V3::Subscriptions
Robert Schilling's avatar
Robert Schilling committed
37 38
      mount ::API::V3::SystemHooks
      mount ::API::V3::Tags
39
      mount ::API::V3::Templates
Robert Schilling's avatar
Robert Schilling committed
40 41
      mount ::API::V3::Todos
      mount ::API::V3::Triggers
Robert Schilling's avatar
Robert Schilling committed
42
      mount ::API::V3::Users
Robert Schilling's avatar
Robert Schilling committed
43
      mount ::API::V3::Variables
44
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
45

46 47
    before { allow_access_with_scope :api }

48 49 50 51
    rescue_from Gitlab::Access::AccessDeniedError do
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
    end

52
    rescue_from ActiveRecord::RecordNotFound do
53
      rack_response({ 'message' => '404 Not found' }.to_json, 404)
54 55
    end

Connor Shea's avatar
Connor Shea committed
56
    # Retain 405 error rather than a 500 error for Grape 0.15.0+.
57 58 59 60 61
    # 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

Connor Shea's avatar
Connor Shea committed
62 63
    rescue_from Grape::Exceptions::Base do |e|
      error! e.message, e.status, e.headers
64 65
    end

66
    rescue_from Gitlab::Auth::TooManyIps do |e|
67
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
68 69
    end

70
    rescue_from :all do |exception|
Stan Hu's avatar
Stan Hu committed
71
      handle_api_exception(exception)
72 73
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
74
    format :json
75 76
    content_type :txt, "text/plain"

77
    # Ensure the namespace is right, otherwise we might load Grape::API::Helpers
Stan Hu's avatar
Stan Hu committed
78
    helpers ::SentryHelper
79 80
    helpers ::API::Helpers

81
    # Keep in alphabetical order
82
    mount ::API::AccessRequests
83
    mount ::API::AwardEmoji
Robert Schilling's avatar
Robert Schilling committed
84
    mount ::API::Boards
85
    mount ::API::Branches
86
    mount ::API::BroadcastMessages
87
    mount ::API::Commits
Robert Schilling's avatar
Robert Schilling committed
88
    mount ::API::CommitStatuses
89
    mount ::API::DeployKeys
Z.J. van de Weg's avatar
Z.J. van de Weg committed
90
    mount ::API::Deployments
91
    mount ::API::Environments
92 93 94
    mount ::API::Files
    mount ::API::Groups
    mount ::API::Internal
95
    mount ::API::Issues
96
    mount ::API::Jobs
97 98
    mount ::API::Keys
    mount ::API::Labels
99
    mount ::API::Lint
100
    mount ::API::Members
101
    mount ::API::MergeRequestDiffs
Robert Schilling's avatar
Robert Schilling committed
102
    mount ::API::MergeRequests
103 104
    mount ::API::Milestones
    mount ::API::Namespaces
105
    mount ::API::Notes
106
    mount ::API::NotificationSettings
107
    mount ::API::Pipelines
108
    mount ::API::ProjectHooks
109
    mount ::API::Projects
Robert Schilling's avatar
Robert Schilling committed
110
    mount ::API::ProjectSnippets
111
    mount ::API::Repositories
112
    mount ::API::Runner
113
    mount ::API::Runners
114
    mount ::API::Services
115
    mount ::API::Session
116
    mount ::API::Settings
117
    mount ::API::SidekiqMetrics
118
    mount ::API::Snippets
119 120
    mount ::API::Subscriptions
    mount ::API::SystemHooks
121
    mount ::API::Tags
ZJ van de Weg's avatar
ZJ van de Weg committed
122
    mount ::API::Templates
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
123
    mount ::API::Todos
124
    mount ::API::Triggers
125
    mount ::API::Users
126
    mount ::API::Variables
Robert Schilling's avatar
Robert Schilling committed
127
    mount ::API::Version
128 129

    route :any, '*path' do
130
      error!('404 Not Found', 404)
131
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
132
  end
133
end