Commit 0d67f209fcbabed5831d90561350a8e094c150c4
1 parent
84a3f8fc
Exists in
master
and in
4 other branches
refactor API and improve docs
Showing
3 changed files
with
89 additions
and
53 deletions
Show diff stats
lib/api.rb
| 1 | -require 'api/entities' | |
| 2 | -require 'api/helpers' | |
| 1 | +Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file} | |
| 3 | 2 | |
| 4 | 3 | module Gitlab |
| 5 | 4 | class API < Grape::API |
| 6 | 5 | format :json |
| 7 | 6 | helpers APIHelpers |
| 8 | 7 | |
| 9 | - # Users API | |
| 10 | - resource :users do | |
| 11 | - before { authenticate! } | |
| 12 | - | |
| 13 | - # GET /users | |
| 14 | - get do | |
| 15 | - @users = User.all | |
| 16 | - present @users, :with => Entities::User | |
| 17 | - end | |
| 18 | - | |
| 19 | - # GET /users/:id | |
| 20 | - get ":id" do | |
| 21 | - @user = User.find(params[:id]) | |
| 22 | - present @user, :with => Entities::User | |
| 23 | - end | |
| 24 | - end | |
| 25 | - | |
| 26 | - # GET /user | |
| 27 | - get "/user" do | |
| 28 | - authenticate! | |
| 29 | - present @current_user, :with => Entities::User | |
| 30 | - end | |
| 31 | - | |
| 32 | - # Projects API | |
| 33 | - resource :projects do | |
| 34 | - before { authenticate! } | |
| 35 | - | |
| 36 | - # GET /projects | |
| 37 | - get do | |
| 38 | - @projects = current_user.projects | |
| 39 | - present @projects, :with => Entities::Project | |
| 40 | - end | |
| 41 | - | |
| 42 | - # GET /projects/:id | |
| 43 | - get ":id" do | |
| 44 | - @project = current_user.projects.find_by_code(params[:id]) | |
| 45 | - present @project, :with => Entities::Project | |
| 46 | - end | |
| 47 | - | |
| 48 | - # GET /projects/:id/repository/branches | |
| 49 | - get ":id/repository/branches" do | |
| 50 | - @project = current_user.projects.find_by_code(params[:id]) | |
| 51 | - present @project.repo.heads.sort_by(&:name), :with => Entities::ProjectRepositoryBranches | |
| 52 | - end | |
| 53 | - | |
| 54 | - # GET /projects/:id/repository/tags | |
| 55 | - get ":id/repository/tags" do | |
| 56 | - @project = current_user.projects.find_by_code(params[:id]) | |
| 57 | - present @project.repo.tags.sort_by(&:name).reverse, :with => Entities::ProjectRepositoryTags | |
| 58 | - end | |
| 59 | - end | |
| 8 | + mount Users | |
| 9 | + mount Projects | |
| 60 | 10 | end |
| 61 | 11 | end | ... | ... |
| ... | ... | @@ -0,0 +1,50 @@ |
| 1 | +module Gitlab | |
| 2 | + # Projects API | |
| 3 | + class Projects < Grape::API | |
| 4 | + before { authenticate! } | |
| 5 | + | |
| 6 | + resource :projects do | |
| 7 | + # Get a projects list for authenticated user | |
| 8 | + # | |
| 9 | + # Example Request: | |
| 10 | + # GET /projects | |
| 11 | + get do | |
| 12 | + @projects = current_user.projects | |
| 13 | + present @projects, :with => Entities::Project | |
| 14 | + end | |
| 15 | + | |
| 16 | + # Get a single project | |
| 17 | + # | |
| 18 | + # Parameters: | |
| 19 | + # id (required) - The code of a project | |
| 20 | + # Example Request: | |
| 21 | + # GET /projects/:id | |
| 22 | + get ":id" do | |
| 23 | + @project = current_user.projects.find_by_code(params[:id]) | |
| 24 | + present @project, :with => Entities::Project | |
| 25 | + end | |
| 26 | + | |
| 27 | + # Get a project repository branches | |
| 28 | + # | |
| 29 | + # Parameters: | |
| 30 | + # id (required) - The code of a project | |
| 31 | + # Example Request: | |
| 32 | + # GET /projects/:id/repository/branches | |
| 33 | + get ":id/repository/branches" do | |
| 34 | + @project = current_user.projects.find_by_code(params[:id]) | |
| 35 | + present @project.repo.heads.sort_by(&:name), :with => Entities::ProjectRepositoryBranches | |
| 36 | + end | |
| 37 | + | |
| 38 | + # Get a project repository tags | |
| 39 | + # | |
| 40 | + # Parameters: | |
| 41 | + # id (required) - The code of a project | |
| 42 | + # Example Request: | |
| 43 | + # GET /projects/:id/repository/tags | |
| 44 | + get ":id/repository/tags" do | |
| 45 | + @project = current_user.projects.find_by_code(params[:id]) | |
| 46 | + present @project.repo.tags.sort_by(&:name).reverse, :with => Entities::ProjectRepositoryTags | |
| 47 | + end | |
| 48 | + end | |
| 49 | + end | |
| 50 | +end | ... | ... |
| ... | ... | @@ -0,0 +1,36 @@ |
| 1 | +module Gitlab | |
| 2 | + # Users API | |
| 3 | + class Users < Grape::API | |
| 4 | + before { authenticate! } | |
| 5 | + | |
| 6 | + resource :users do | |
| 7 | + # Get a users list | |
| 8 | + # | |
| 9 | + # Example Request: | |
| 10 | + # GET /users | |
| 11 | + get do | |
| 12 | + @users = User.all | |
| 13 | + present @users, :with => Entities::User | |
| 14 | + end | |
| 15 | + | |
| 16 | + # Get a single user | |
| 17 | + # | |
| 18 | + # Parameters: | |
| 19 | + # id (required) - The ID of a user | |
| 20 | + # Example Request: | |
| 21 | + # GET /users/:id | |
| 22 | + get ":id" do | |
| 23 | + @user = User.find(params[:id]) | |
| 24 | + present @user, :with => Entities::User | |
| 25 | + end | |
| 26 | + end | |
| 27 | + | |
| 28 | + # Get currently authenticated user | |
| 29 | + # | |
| 30 | + # Example Request: | |
| 31 | + # GET /user | |
| 32 | + get "/user" do | |
| 33 | + present @current_user, :with => Entities::User | |
| 34 | + end | |
| 35 | + end | |
| 36 | +end | ... | ... |