Commit 7827d187 authored by Kamil Trzciński's avatar Kamil Trzciński

Refactor API interface

parent d36a4504
......@@ -9,7 +9,6 @@ class Projects::FeatureFlagsController < Projects::ApplicationController
def index
@feature_flags = project.operations_feature_flags
@unleash_instanceid = project.feature_flag_access_token
end
def new
......
# frozen_string_literal: true
module FeatureFlagsHelper
def unleash_api_url(project)
"#{root_url(only_path: false)}api/v4/feature_flags/projects/#{project.id}/unleash"
end
def unleash_api_instanceid(project)
project.feature_flag_access_token
end
end
......@@ -15,9 +15,8 @@
.form-group
= label_tag :api_url, s_('FeatureFlags|API URL'), class: 'label-light'
.input-group
- unleash_api_url = "#{root_url(only_path: false)}api/v4/unleash"
= text_field_tag :api_url,
unleash_api_url,
unleash_api_url(@project),
readonly: true,
class: "form-control js-select-on-focus"
%span.input-group-append
......@@ -28,29 +27,29 @@
class: "input-group-text btn btn-default")
.form-group
= label_tag :application_name, s_('FeatureFlags|Application name'), class: 'label-light'
= label_tag :instance_id, s_('FeatureFlags|Instance ID'), class: 'label-light'
.input-group
= text_field_tag :application_name,
@project.id,
= text_field_tag :instance_id,
unleash_api_instanceid(@project),
readonly: true,
class: "form-control js-select-on-focus"
%span.input-group-append
= clipboard_button(target: '#application_name',
title: _("Copy name to clipboard"),
= clipboard_button(target: '#instance_id',
title: _("Copy ID to clipboard"),
placement: "left",
container: '#configure-feature-flags-modal',
class: "input-group-text btn btn-default")
.form-group
= label_tag :instance_id, s_('FeatureFlags|Instance ID'), class: 'label-light'
= label_tag :application_name, s_('FeatureFlags|Application name'), class: 'label-light'
.input-group
= text_field_tag :instance_id,
@unleash_instanceid,
= text_field_tag :application_name,
"production",
readonly: true,
class: "form-control js-select-on-focus"
%span.input-group-append
= clipboard_button(target: '#instance_id',
title: _("Copy ID to clipboard"),
= clipboard_button(target: '#application_name',
title: _("Copy name to clipboard"),
placement: "left",
container: '#configure-feature-flags-modal',
class: "input-group-text btn btn-default")
......@@ -108,6 +108,7 @@ module API
mount ::API::Environments
mount ::API::Events
mount ::API::Features
mount ::API::FeatureFlags
mount ::API::Files
mount ::API::GroupBoards
mount ::API::GroupMilestones
......@@ -158,7 +159,6 @@ module API
mount ::API::Templates
mount ::API::Todos
mount ::API::Triggers
mount ::API::Unleash
mount ::API::Users
mount ::API::Variables
mount ::API::Version
......
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
module API
class Unleash < Grape::API
include PaginationParams
before do
unauthorized! unless access_token
end
get ':unleash/features' do
present @project, with: Entities::UnleashFeatures
end
post 'unleash/client/register' do
status :ok
end
post 'unleash/client/metrics' do
status :ok
end
private
helpers do
def project
@project ||= find_project(unleash_appname)
end
def access_token
@access_token ||= ProjectFeatureFlagsAccessToken.find_by(token: unleash_instanceid, project: project)
end
def unleash_appname
params[:appname] || env[:HTTP_UNLEASH_APPNAME]
end
def unleash_instanceid
params[:instanceid] || env[:HTTP_UNLEASH_INSTANCEID]
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment