Welcome to mirror list, hosted at ThFree Co, Russian Federation.

unleash.rb « api « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1fbd7cf5afc8173ab3b752956f7a45f3daabb5dd (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true

module API
  class Unleash < ::API::Base
    include PaginationParams

    feature_category :feature_flags

    namespace :feature_flags do
      resource :unleash, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
        params do
          requires :project_id, type: String, desc: 'The ID of a project'
          optional :instance_id, type: String, desc: 'The Instance ID of Unleash Client'
          optional :app_name, type: String, desc: 'The Application Name of Unleash Client'
        end
        route_param :project_id do
          before do
            authorize_by_unleash_instance_id!
          end

          get do
            # not supported yet
            status :ok
          end

          desc 'Get a list of features (deprecated, v2 client support)'
          get 'features' do
            if ::Feature.enabled?(:cache_unleash_client_api, project)
              present_feature_flags
            else
              present :version, 1
              present :features, feature_flags, with: ::API::Entities::UnleashFeature
            end
          end

          # We decrease the urgency of this endpoint until the maxmemory issue of redis-cache has been resolved.
          # See https://gitlab.com/gitlab-org/gitlab/-/issues/365575#note_1033611872 for more information.
          desc 'Get a list of features'
          get 'client/features', urgency: :low do
            if ::Feature.enabled?(:cache_unleash_client_api, project)
              present_feature_flags
            else
              present :version, 1
              present :features, feature_flags, with: ::API::Entities::UnleashFeature
            end
          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 present_feature_flags
        present_cached feature_flags_client,
          with: ::API::Entities::Unleash::ClientFeatureFlags,
          cache_context: -> (client) { client.unleash_api_cache_key }
      end

      def project
        @project ||= find_project(params[:project_id])
      end

      def feature_flags_client
        strong_memoize(:feature_flags_client) do
          client = Operations::FeatureFlagsClient.find_for_project_and_token(project, unleash_instance_id)
          client.unleash_app_name = unleash_app_name if client
          client
        end
      end

      def unleash_instance_id
        env['HTTP_UNLEASH_INSTANCEID'] || params[:instance_id]
      end

      def unleash_app_name
        env['HTTP_UNLEASH_APPNAME'] || params[:app_name]
      end

      def authorize_by_unleash_instance_id!
        unauthorized! unless feature_flags_client
      end

      def feature_flags
        return [] unless unleash_app_name.present?

        Operations::FeatureFlag.for_unleash_client(project, unleash_app_name)
      end
    end
  end
end