From 634cbd71380f595f6f44ce93399b92fbee37f98f Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 14 May 2013 15:33:31 +0300 Subject: Refactor API classes. So api classes like Gitlab::Issues become API::Issues --- app/controllers/application_controller.rb | 2 +- config/routes.rb | 6 ++--- lib/api.rb | 38 ------------------------------- lib/api/api.rb | 38 +++++++++++++++++++++++++++++++ lib/api/entities.rb | 2 +- lib/api/groups.rb | 2 +- lib/api/helpers.rb | 2 +- lib/api/internal.rb | 4 ++-- lib/api/issues.rb | 2 +- lib/api/merge_requests.rb | 2 +- lib/api/milestones.rb | 2 +- lib/api/notes.rb | 2 +- lib/api/projects.rb | 2 +- lib/api/session.rb | 2 +- lib/api/system_hooks.rb | 2 +- lib/api/users.rb | 2 +- spec/requests/api/groups_spec.rb | 2 +- spec/requests/api/internal_spec.rb | 4 ++-- spec/requests/api/issues_spec.rb | 2 +- spec/requests/api/merge_requests_spec.rb | 2 +- spec/requests/api/milestones_spec.rb | 2 +- spec/requests/api/notes_spec.rb | 2 +- spec/requests/api/projects_spec.rb | 2 +- spec/requests/api/session_spec.rb | 2 +- spec/requests/api/system_hooks_spec.rb | 2 +- spec/requests/api/users_spec.rb | 2 +- spec/routing/routing_spec.rb | 2 +- spec/support/api_helpers.rb | 2 +- 28 files changed, 68 insertions(+), 68 deletions(-) delete mode 100644 lib/api.rb create mode 100644 lib/api/api.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a6a887fa939..dfc1fdcee8a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -152,7 +152,7 @@ class ApplicationController < ActionController::Base def add_gon_variables gon.default_issues_tracker = Project.issues_tracker.default_value - gon.api_version = Gitlab::API.version + gon.api_version = API::API.version gon.api_token = current_user.private_token if current_user gon.gravatar_url = request.ssl? ? Gitlab.config.gravatar.ssl_url : Gitlab.config.gravatar.plain_url gon.relative_url_root = Gitlab.config.gitlab.relative_url_root diff --git a/config/routes.rb b/config/routes.rb index 0e60aeb9984..0b0bd293da2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ require 'sidekiq/web' +require 'api/api' Gitlab::Application.routes.draw do # @@ -7,9 +8,8 @@ Gitlab::Application.routes.draw do get 'search' => "search#show" # API - require 'api' - Gitlab::API.logger Rails.logger - mount Gitlab::API => '/api' + API::API.logger Rails.logger + mount API::API => '/api' constraint = lambda { |request| request.env["warden"].authenticate? and request.env['warden'].user.admin? } constraints constraint do diff --git a/lib/api.rb b/lib/api.rb deleted file mode 100644 index 082769da6d3..00000000000 --- a/lib/api.rb +++ /dev/null @@ -1,38 +0,0 @@ -Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file} - -module Gitlab - class API < Grape::API - version 'v3', using: :path - - rescue_from ActiveRecord::RecordNotFound do - rack_response({'message' => '404 Not found'}.to_json, 404) - end - - rescue_from :all do |exception| - # lifted from https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L60 - # why is this not wrapped in something reusable? - trace = exception.backtrace - - message = "\n#{exception.class} (#{exception.message}):\n" - message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code) - message << " " << trace.join("\n ") - - API.logger.add Logger::FATAL, message - rack_response({'message' => '500 Internal Server Error'}, 500) - end - - format :json - helpers APIHelpers - - mount Groups - mount Users - mount Projects - mount Issues - mount Milestones - mount Session - mount MergeRequests - mount Notes - mount Internal - mount SystemHooks - end -end diff --git a/lib/api/api.rb b/lib/api/api.rb new file mode 100644 index 00000000000..28e6add73ed --- /dev/null +++ b/lib/api/api.rb @@ -0,0 +1,38 @@ +Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file} + +module API + class API < Grape::API + version 'v3', using: :path + + rescue_from ActiveRecord::RecordNotFound do + rack_response({'message' => '404 Not found'}.to_json, 404) + end + + rescue_from :all do |exception| + # lifted from https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L60 + # why is this not wrapped in something reusable? + trace = exception.backtrace + + message = "\n#{exception.class} (#{exception.message}):\n" + message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code) + message << " " << trace.join("\n ") + + API.logger.add Logger::FATAL, message + rack_response({'message' => '500 Internal Server Error'}, 500) + end + + format :json + helpers APIHelpers + + mount Groups + mount Users + mount Projects + mount Issues + mount Milestones + mount Session + mount MergeRequests + mount Notes + mount Internal + mount SystemHooks + end +end diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 3fe4abc3e2a..c3c351e1733 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -1,4 +1,4 @@ -module Gitlab +module API module Entities class User < Grape::Entity expose :id, :username, :email, :name, :bio, :skype, :linkedin, :twitter, diff --git a/lib/api/groups.rb b/lib/api/groups.rb index efdbeb176c6..701f6777b77 100644 --- a/lib/api/groups.rb +++ b/lib/api/groups.rb @@ -1,4 +1,4 @@ -module Gitlab +module API # groups API class Groups < Grape::API before { authenticate! } diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index f12fb5fdbd0..94cf4f2e69f 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -1,4 +1,4 @@ -module Gitlab +module API module APIHelpers def current_user @current_user ||= User.find_by_authentication_token(params[:private_token] || env["HTTP_PRIVATE_TOKEN"]) diff --git a/lib/api/internal.rb b/lib/api/internal.rb index 3e1173bd0a3..a602dc05418 100644 --- a/lib/api/internal.rb +++ b/lib/api/internal.rb @@ -1,4 +1,4 @@ -module Gitlab +module API # Internal access API class Internal < Grape::API namespace 'internal' do @@ -58,7 +58,7 @@ module Gitlab get "/check" do { - api_version: Gitlab::API.version, + api_version: API.version, gitlab_version: Gitlab::VERSION, gitlab_rev: Gitlab::REVISION, } diff --git a/lib/api/issues.rb b/lib/api/issues.rb index 500a8551f35..53e2e8cfa35 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -1,4 +1,4 @@ -module Gitlab +module API # Issues API class Issues < Grape::API before { authenticate! } diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb index d5595d5f16b..23e2f82889f 100644 --- a/lib/api/merge_requests.rb +++ b/lib/api/merge_requests.rb @@ -1,4 +1,4 @@ -module Gitlab +module API # MergeRequest API class MergeRequests < Grape::API before { authenticate! } diff --git a/lib/api/milestones.rb b/lib/api/milestones.rb index 1adeefece1f..a25bbad1302 100644 --- a/lib/api/milestones.rb +++ b/lib/api/milestones.rb @@ -1,4 +1,4 @@ -module Gitlab +module API # Milestones API class Milestones < Grape::API before { authenticate! } diff --git a/lib/api/notes.rb b/lib/api/notes.rb index 450faae535a..cb2bc764476 100644 --- a/lib/api/notes.rb +++ b/lib/api/notes.rb @@ -1,4 +1,4 @@ -module Gitlab +module API # Notes API class Notes < Grape::API before { authenticate! } diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 18e912ff48e..d9743b4539a 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -1,4 +1,4 @@ -module Gitlab +module API # Projects API class Projects < Grape::API before { authenticate! } diff --git a/lib/api/session.rb b/lib/api/session.rb index b4050160ae4..509acded51e 100644 --- a/lib/api/session.rb +++ b/lib/api/session.rb @@ -1,4 +1,4 @@ -module Gitlab +module API # Users API class Session < Grape::API # Login to get token diff --git a/lib/api/system_hooks.rb b/lib/api/system_hooks.rb index 9e3574e3b99..3e239c5afe7 100644 --- a/lib/api/system_hooks.rb +++ b/lib/api/system_hooks.rb @@ -1,4 +1,4 @@ -module Gitlab +module API # Hooks API class SystemHooks < Grape::API before { diff --git a/lib/api/users.rb b/lib/api/users.rb index cda543495a0..84256b51124 100644 --- a/lib/api/users.rb +++ b/lib/api/users.rb @@ -1,4 +1,4 @@ -module Gitlab +module API # Users API class Users < Grape::API before { authenticate! } diff --git a/spec/requests/api/groups_spec.rb b/spec/requests/api/groups_spec.rb index cf284b607a5..d2e25fb9e23 100644 --- a/spec/requests/api/groups_spec.rb +++ b/spec/requests/api/groups_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Gitlab::API do +describe API::API do include ApiHelpers let(:user1) { create(:user) } diff --git a/spec/requests/api/internal_spec.rb b/spec/requests/api/internal_spec.rb index ba11a41da29..5a43953d15c 100644 --- a/spec/requests/api/internal_spec.rb +++ b/spec/requests/api/internal_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Gitlab::API do +describe API::API do include ApiHelpers let(:user) { create(:user) } @@ -12,7 +12,7 @@ describe Gitlab::API do get api("/internal/check") response.status.should == 200 - json_response['api_version'].should == Gitlab::API.version + json_response['api_version'].should == API::API.version end end diff --git a/spec/requests/api/issues_spec.rb b/spec/requests/api/issues_spec.rb index ecf0bdb7084..d5f75367e4a 100644 --- a/spec/requests/api/issues_spec.rb +++ b/spec/requests/api/issues_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Gitlab::API do +describe API::API do include ApiHelpers let(:user) { create(:user) } diff --git a/spec/requests/api/merge_requests_spec.rb b/spec/requests/api/merge_requests_spec.rb index 25bbd9860bd..5bf228a7448 100644 --- a/spec/requests/api/merge_requests_spec.rb +++ b/spec/requests/api/merge_requests_spec.rb @@ -1,6 +1,6 @@ require "spec_helper" -describe Gitlab::API do +describe API::API do include ApiHelpers let(:user) { create(:user ) } diff --git a/spec/requests/api/milestones_spec.rb b/spec/requests/api/milestones_spec.rb index c9a10417fff..af12c088c8f 100644 --- a/spec/requests/api/milestones_spec.rb +++ b/spec/requests/api/milestones_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Gitlab::API do +describe API::API do include ApiHelpers before(:each) { enable_observers } diff --git a/spec/requests/api/notes_spec.rb b/spec/requests/api/notes_spec.rb index 901640834f2..78d55a7b4ed 100644 --- a/spec/requests/api/notes_spec.rb +++ b/spec/requests/api/notes_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Gitlab::API do +describe API::API do include ApiHelpers let(:user) { create(:user) } diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index 7aeee354ba1..4346bfe8f2e 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Gitlab::API do +describe API::API do include ApiHelpers before(:each) { enable_observers } diff --git a/spec/requests/api/session_spec.rb b/spec/requests/api/session_spec.rb index 4a37312bffd..88c17f26a69 100644 --- a/spec/requests/api/session_spec.rb +++ b/spec/requests/api/session_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Gitlab::API do +describe API::API do include ApiHelpers let(:user) { create(:user) } diff --git a/spec/requests/api/system_hooks_spec.rb b/spec/requests/api/system_hooks_spec.rb index 647e31ce034..b1df3cb7886 100644 --- a/spec/requests/api/system_hooks_spec.rb +++ b/spec/requests/api/system_hooks_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Gitlab::API do +describe API::API do include ApiHelpers let(:user) { create(:user) } diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index b4688dd29f7..33df9f7ece5 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Gitlab::API do +describe API::API do include ApiHelpers let(:user) { create(:user) } diff --git a/spec/routing/routing_spec.rb b/spec/routing/routing_spec.rb index 5ad8165ecce..049cfeaab01 100644 --- a/spec/routing/routing_spec.rb +++ b/spec/routing/routing_spec.rb @@ -7,7 +7,7 @@ describe SearchController, "routing" do end end -# gitlab_api /api Gitlab::API +# gitlab_api /api API::API # resque /info/resque Resque::Server # /:path Grack describe "Mounted Apps", "routing" do diff --git a/spec/support/api_helpers.rb b/spec/support/api_helpers.rb index c4514bf38be..ec9a326a1ea 100644 --- a/spec/support/api_helpers.rb +++ b/spec/support/api_helpers.rb @@ -18,7 +18,7 @@ module ApiHelpers # # Returns the relative path to the requested API resource def api(path, user = nil) - "/api/#{Gitlab::API.version}#{path}" + + "/api/#{API::API.version}#{path}" + # Normalize query string (path.index('?') ? '' : '?') + -- cgit v1.2.1