<commit_before>package api

import (
	"net/http"

	"github.com/Dataman-Cloud/swan/src/config"
	"github.com/Dataman-Cloud/swan/src/manager/framework/scheduler"
	"github.com/Dataman-Cloud/swan/src/manager/framework/state"
	swanapiserver "github.com/Dataman-Cloud/swan/src/manager/new_apiserver"
	"github.com/Dataman-Cloud/swan/src/types"

	"github.com/Sirupsen/logrus"
	"github.com/emicklei/go-restful"
	"github.com/gin-gonic/gin"
	"golang.org/x/net/context"
)

const (
	API_PREFIX = "v_beta"
)

type Api struct {
	config    config.Scheduler
	Scheduler *scheduler.Scheduler
}

func NewApi(eng *scheduler.Scheduler, config config.Scheduler) *Api {
	return &Api{
		Scheduler: eng,
		config:    config,
	}
}

// TODO(xychu): after we port all the apis to restful.WebService,
//              we could remove Api and NewApi above.
type AppService struct {
	Scheduler *scheduler.Scheduler
	swanapiserver.ApiRegister
}

func NewAndInstallAppService(apiServer *swanapiserver.ApiServer, eng *scheduler.Scheduler) *AppService {
	appService := &AppService{
		Scheduler: eng,
	}
	swanapiserver.Install(apiServer, appService)
	return appService
}

// NOTE(xychu): Every service need to registed to ApiServer need to impl
//              a `Register` interface so that it can be added to ApiServer.Start
func (api *AppService) Register(container *restful.Container) {
	ws := new(restful.WebService)
	ws.
		ApiVersion(API_PREFIX).
		Path("/" + API_PREFIX + "/apps").
		Doc("App management").
		Consumes(restful.MIME_JSON).
		Produces(restful.MIME_JSON)

	ws.Route(ws.GET("/").To(api.ListApp).
		// docs
		Doc("List Apps").
		Operation("listApps").
		Returns(200, "OK", []types.Application{}))
	ws.Route(ws.POST("/").To(api.CreateApp).
		// docs
		Doc("Create App").
		Operation("createApp").
		Returns(201, "OK", types.Application{}).
		Returns(400, "BadRequest", nil).
		Reads(types.Version{}).
		Writes(types.Application{}))
	ws.Route(ws.GET("/{app_id}").To(api.GetApp).
		// docs
		Doc("Get an App").
		Operation("getApp").
		Param(ws.PathParameter("app_id", "identifier of the app").DataType("string")).
		Returns(200, "OK", types.Application{}).
		Returns(404, "NotFound", nil).
		Writes(types.Application{}))
	ws.Route(ws.DELETE("/{app_id}").To(api.DeleteApp).
		// docs
		Doc("Delete App").
		Operation("deleteApp").
		Returns(204, "OK", nil).
		Returns(404, "NotFound", nil).
		Param(ws.PathParameter("app_id", "identifier of the app").DataType("string")))
	ws.Route(ws.PATCH("/{app_id}/scale-up").To(api.ScaleUp).
		// docs
		Doc("Scale Up App").
		Operation("scaleUp").
		Returns(400, "BadRequest", nil).
		Param(ws.PathParameter("app_id", "identifier of the app").DataType("string")))
	ws.Route(ws.PATCH("/{app_id}/scale-down").To(api.ScaleDown).
		// docs
		Doc("Scale Down App").
		Operation("scaleDown").
		Returns(400, "BadRequest", nil).
		Param(ws.PathParameter("app_id", "identifier of the app").DataType("string")))
	ws.Route(ws.PUT("/{app_id}").To(api.UpdateApp).
		// docs
		Doc("Update App").
		Operation("updateApp").
		Returns(404, "NotFound", nil).
		Param(ws.PathParameter("app_id", "identifier of the app").DataType("string")))
	ws.Route(ws.PATCH("/{app_id}/proceed-update").To(api.ProceedUpdate).
		// docs
		Doc("Proceed Update App").
		Operation("proceedUpdateApp").
		Returns(400, "BadRequest", nil).
		Param(ws.PathParameter("app_id", "identifier of the app").DataType("string")))
	ws.Route(ws.PATCH("/{app_id}/cancel-update").To(api.CancelUpdate).
		// docs
		Doc("Cancel Update App").
		Operation("cancelUpdateApp").
		Returns(400, "BadRequest", nil).
		Param(ws.PathParameter("app_id", "identifier of the app").DataType("string")))

	container.Add(ws)
}

func (api *AppService) CreateApp(request *restful.Request, response *restful.Response) {
	var version types.Version

	err := request.ReadEntity(&version)
	if err != nil {
		response.WriteErrorString(http.StatusBadRequest, err.Error())
	}

	err = CheckVersion(&version)
	if err != nil {
		response.WriteErrorString(http.StatusBadRequest, err.Error())
	}

	app, err := api.Scheduler.CreateApp(&version)
	if err != nil {
		response.WriteErrorString(http.StatusInternalServerError, err.Error())
	} else {
		response.WriteHeaderAndEntity(http.StatusCreated, app)
	}
}
<commit_msg>Use api App struct as return type (#116)<commit_after>package api

import (
	"net/http"

	"github.com/Dataman-Cloud/swan/src/config"
	"github.com/Dataman-Cloud/swan/src/manager/framework/scheduler"
	"github.com/Dataman-Cloud/swan/src/manager/framework/state"
	swanapiserver "github.com/Dataman-Cloud/swan/src/manager/new_apiserver"
	"github.com/Dataman-Cloud/swan/src/types"

	"github.com/Sirupsen/logrus"
	"github.com/emicklei/go-restful"
	"github.com/gin-gonic/gin"
	"golang.org/x/net/context"
)

const (
	API_PREFIX = "v_beta"
)

type Api struct {
	config    config.Scheduler
	Scheduler *scheduler.Scheduler
}

func NewApi(eng *scheduler.Scheduler, config config.Scheduler) *Api {
	return &Api{
		Scheduler: eng,
		config:    config,
	}
}

// TODO(xychu): after we port all the apis to restful.WebService,
//              we could remove Api and NewApi above.
type AppService struct {
	Scheduler *scheduler.Scheduler
	swanapiserver.ApiRegister
}

func NewAndInstallAppService(apiServer *swanapiserver.ApiServer, eng *scheduler.Scheduler) *AppService {
	appService := &AppService{
		Scheduler: eng,
	}
	swanapiserver.Install(apiServer, appService)
	return appService
}

// NOTE(xychu): Every service need to registed to ApiServer need to impl
//              a `Register` interface so that it can be added to ApiServer.Start
func (api *AppService) Register(container *restful.Container) {
	ws := new(restful.WebService)
	ws.
		ApiVersion(API_PREFIX).
		Path("/" + API_PREFIX + "/apps").
		Doc("App management").
		Consumes(restful.MIME_JSON).
		Produces(restful.MIME_JSON)

	ws.Route(ws.GET("/").To(api.ListApp).
		// docs
		Doc("List Apps").
		Operation("listApps").
		Returns(200, "OK", []types.Application{}))
	ws.Route(ws.POST("/").To(api.CreateApp).
		// docs
		Doc("Create App").
		Operation("createApp").
		Returns(201, "OK", types.Application{}).
		Returns(400, "BadRequest", nil).
		Reads(types.Version{}).
		Writes(types.Application{}))
	ws.Route(ws.GET("/{app_id}").To(api.GetApp).
		// docs
		Doc("Get an App").
		Operation("getApp").
		Param(ws.PathParameter("app_id", "identifier of the app").DataType("string")).
		Returns(200, "OK", types.Application{}).
		Returns(404, "NotFound", nil).
		Writes(types.Application{}))
	ws.Route(ws.DELETE("/{app_id}").To(api.DeleteApp).
		// docs
		Doc("Delete App").
		Operation("deleteApp").
		Returns(204, "OK", nil).
		Returns(404, "NotFound", nil).
		Param(ws.PathParameter("app_id", "identifier of the app").DataType("string")))
	ws.Route(ws.PATCH("/{app_id}/scale-up").To(api.ScaleUp).
		// docs
		Doc("Scale Up App").
		Operation("scaleUp").
		Returns(400, "BadRequest", nil).
		Param(ws.PathParameter("app_id", "identifier of the app").DataType("string")))
	ws.Route(ws.PATCH("/{app_id}/scale-down").To(api.ScaleDown).
		// docs
		Doc("Scale Down App").
		Operation("scaleDown").
		Returns(400, "BadRequest", nil).
		Param(ws.PathParameter("app_id", "identifier of the app").DataType("string")))
	ws.Route(ws.PUT("/{app_id}").To(api.UpdateApp).
		// docs
		Doc("Update App").
		Operation("updateApp").
		Returns(404, "NotFound", nil).
		Param(ws.PathParameter("app_id", "identifier of the app").DataType("string")))
	ws.Route(ws.PATCH("/{app_id}/proceed-update").To(api.ProceedUpdate).
		// docs
		Doc("Proceed Update App").
		Operation("proceedUpdateApp").
		Returns(400, "BadRequest", nil).
		Param(ws.PathParameter("app_id", "identifier of the app").DataType("string")))
	ws.Route(ws.PATCH("/{app_id}/cancel-update").To(api.CancelUpdate).
		// docs
		Doc("Cancel Update App").
		Operation("cancelUpdateApp").
		Returns(400, "BadRequest", nil).
		Param(ws.PathParameter("app_id", "identifier of the app").DataType("string")))

	container.Add(ws)
}

func (api *AppService) CreateApp(request *restful.Request, response *restful.Response) {
	var version types.Version

	err := request.ReadEntity(&version)
	if err != nil {
		response.WriteErrorString(http.StatusBadRequest, err.Error())
	}

	err = CheckVersion(&version)
	if err != nil {
		response.WriteErrorString(http.StatusBadRequest, err.Error())
	}

	app, err := api.Scheduler.CreateApp(&version)
	if err != nil {
		response.WriteErrorString(http.StatusInternalServerError, err.Error())
	} else {
		appRet := &App{
			ID:               version.AppId,
			Name:             version.AppId,
			Instances:        int(version.Instances),
			RunningInstances: app.RunningInstances(),
			RunAs:            version.RunAs,
			ClusterId:        app.MesosConnector.ClusterId,
			Created:          app.Created,
			Updated:          app.Updated,
			Mode:             string(app.Mode),
			State:            app.State,
		}

		appRet.Versions = make([]string, 0)
		for _, v := range app.Versions {
			appRet.Versions = append(appRet.Versions, v.ID)
		}

		appRet.Tasks = FilterTasksFromApp(app)
		response.WriteHeaderAndEntity(http.StatusCreated, appRet)
	}
}

{'fraction_non_alphanumeric': 0.16964971999560777, 'fraction_numerical': 0.00834522894476776, 'mean_word_length': 4.249567723342939, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5200987', 'n_tokens_mistral': 3387, 'n_tokens_neox': 3166, 'n_words': 581}