summaryrefslogtreecommitdiff
path: root/lib/api/feature_flags.rb
blob: dff43544a75a07e2c0f79462fe55cff108863fac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module API
  class FeatureFlags < Grape::API
    include PaginationParams

    resource :feature_flags do
      resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
        params do
          requires :id, type: String, desc: 'The ID of a project'
        end
        route_param :id do
          resource :unleash do
            before do
              authenticate_by_unleash_access_token!
            end

            get 'features' do
              present project, with: Entities::UnleashFeatures
            end

            post 'client/register' do
              # not supported yet
              status :ok
            end

            post 'client/metrics' do
              # not supported yet
              status :ok
            end
          end
        end
      end

      helpers do
        def project
          @project ||= find_project(params[:id])
        end

        def unleash_instanceid
          params[:instanceid] || env[:HTTP_UNLEASH_INSTANCEID]
        end

        def authenticate_by_unleash_access_token!
          unless Operations::FeatureFlagsAccessToken.find_by(token: unleash_instanceid, project: project)
            unauthorized!
          end
        end
      end
    end
  end
end