205 lines
5.1 KiB
Go
205 lines
5.1 KiB
Go
package apis
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
json "github.com/json-iterator/go"
|
|
"gitlink.org.cn/JointCloud/pcm-openi/common"
|
|
"gitlink.org.cn/JointCloud/pcm-openi/model"
|
|
"gitlink.org.cn/JointCloud/pcm-openi/service"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type datasetApi struct {
|
|
*Api
|
|
*service.DatasetService
|
|
}
|
|
|
|
var DatasetApi = datasetApi{
|
|
Api: BaseApi,
|
|
DatasetService: service.NewDatasetService(),
|
|
}
|
|
|
|
func GetDatasetPublic(ctx *gin.Context) {
|
|
var param model.DatasetParam
|
|
if err := ctx.BindJSON(¶m); err != nil {
|
|
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, err)
|
|
return
|
|
}
|
|
|
|
token := ctx.Query(common.ACCESSTOKEN)
|
|
if token == "" {
|
|
model.Response(ctx, 401, "access_token为必填字段", nil)
|
|
return
|
|
}
|
|
|
|
reqUrl := common.OPENIPREFIX + common.DATASETPUBLIC
|
|
|
|
var resp model.Dataset
|
|
|
|
req := common.GetRestyRequest(common.TIMEOUT)
|
|
res, err := req.
|
|
SetQueryParam(common.ACCESSTOKEN, token).
|
|
SetQueryParam("type", strconv.Itoa(param.Type)).
|
|
SetQueryParam("q", param.Q).
|
|
SetQueryParam("page", strconv.Itoa(param.Page)).
|
|
SetQueryParam("pageSize", strconv.Itoa(param.PageSize)).
|
|
SetPathParam("username", param.UserName).
|
|
SetPathParam("reponame", param.RepoName).
|
|
SetResult(&resp).
|
|
Get(reqUrl)
|
|
|
|
if err != nil {
|
|
model.Response(ctx, 500, common.INVOKEERROR, err)
|
|
return
|
|
}
|
|
if res != nil {
|
|
if res.StatusCode() == 401 {
|
|
model.Response(ctx, 401, "access_token值无效请检查", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
if resp.Code != 0 {
|
|
model.Response(ctx, 500, common.INVOKEERROR, err)
|
|
return
|
|
}
|
|
|
|
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
|
}
|
|
|
|
func ExportDataset(ctx *gin.Context) {
|
|
var param model.ExportDatasetParam
|
|
if err := ctx.BindJSON(¶m); err != nil {
|
|
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, err)
|
|
return
|
|
}
|
|
|
|
token := ctx.Query(common.ACCESSTOKEN)
|
|
|
|
reqUrl := common.OPENIPREFIX + common.DATASETEXISTEXPORT
|
|
|
|
var resp model.ExportDataset
|
|
|
|
req := common.GetRestyRequest(common.TIMEOUT)
|
|
_, err := req.
|
|
SetQueryParam(common.ACCESSTOKEN, token).
|
|
SetPathParam("username", param.UserName).
|
|
SetPathParam("reponame", param.RepoName).
|
|
SetFormData(map[string]string{
|
|
"jobId": param.JobId,
|
|
"datasetId": param.DatasetId,
|
|
"type": strconv.Itoa(param.Type),
|
|
"modelSelectedFile": param.ModelSelectedFile,
|
|
}).
|
|
SetResult(&resp).
|
|
Post(reqUrl)
|
|
|
|
if err != nil {
|
|
model.Response(ctx, 500, common.INVOKEERROR, err)
|
|
return
|
|
}
|
|
|
|
if resp.Code != "0" {
|
|
model.Response(ctx, 500, resp.Msg, nil)
|
|
return
|
|
}
|
|
|
|
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
|
}
|
|
|
|
func (r datasetApi) CreateDataset(ctx *gin.Context) {
|
|
var respErr model.RespErr
|
|
var param model.CreateDatasetParam
|
|
if err := ctx.BindJSON(¶m); err != nil {
|
|
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, err)
|
|
return
|
|
}
|
|
|
|
token := ctx.Query(common.ACCESSTOKEN)
|
|
|
|
reqUrl := common.OPENIPREFIX + common.DATASETCREATE
|
|
|
|
var resp model.CreateDataset
|
|
|
|
req := common.GetRestyRequest(common.TIMEOUT)
|
|
res, err := req.
|
|
SetQueryParam(common.ACCESSTOKEN, token).
|
|
SetPathParam("username", param.UserName).
|
|
SetPathParam("reponame", param.RepoName).
|
|
SetFormData(map[string]string{
|
|
"title": param.Title,
|
|
"category": param.Category,
|
|
"task": param.Task,
|
|
"description": param.Description,
|
|
}).
|
|
SetResult(&resp).SetError(&respErr).
|
|
Post(reqUrl)
|
|
|
|
if err != nil {
|
|
model.Response(ctx, 500, common.INVOKEERROR, err)
|
|
return
|
|
}
|
|
|
|
if res != nil {
|
|
if res.StatusCode() != 200 {
|
|
errStr := json.Get(res.Body(), "message").ToString()
|
|
model.Response(ctx, 500, errStr, nil)
|
|
return
|
|
}
|
|
}
|
|
|
|
if resp.Code != 0 {
|
|
model.Response(ctx, 500, resp.Message, nil)
|
|
return
|
|
}
|
|
//返回创建数据集的id
|
|
ld := model.DatasetParam{
|
|
UserName: param.UserName,
|
|
RepoName: param.RepoName,
|
|
Type: -1,
|
|
}
|
|
datasets, err := r.DatasetService.ListDatasets(token, &ld)
|
|
if err != nil {
|
|
model.Response(ctx, 500, common.INVOKEERROR, err)
|
|
}
|
|
resp.DatasetId = datasets.Data[0].Id
|
|
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
|
}
|
|
|
|
func (r datasetApi) ListDatasets(ctx *gin.Context) {
|
|
var param model.DatasetParam
|
|
if err := ctx.ShouldBind(¶m); err != nil {
|
|
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, translate(err))
|
|
return
|
|
}
|
|
token := ctx.Query(common.ACCESSTOKEN)
|
|
if resp, err := r.DatasetService.ListDatasets(token, ¶m); err != nil {
|
|
r.Failed(ctx, Failed, err.Error())
|
|
} else {
|
|
r.Success(ctx, "success", resp.Data)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (r datasetApi) UploadFileDatasets(ctx *gin.Context) {
|
|
// 绑定表单参数到结构体
|
|
var param model.DatasetUploadFileParam
|
|
if err := ctx.ShouldBind(¶m); err != nil {
|
|
model.Response(ctx, http.StatusBadRequest, common.INVALIDPARAMS, translate(err))
|
|
return
|
|
}
|
|
// 获取上传的文件
|
|
fileHeader, err := ctx.FormFile("file")
|
|
if err != nil {
|
|
model.Response(ctx, 500, common.INVOKEERROR, err)
|
|
return
|
|
}
|
|
token := ctx.Query(common.ACCESSTOKEN)
|
|
if resp, err := r.DatasetService.UploadFile(param, token, fileHeader); err != nil {
|
|
model.Response(ctx, 500, err.Error(), resp)
|
|
} else {
|
|
model.Response(ctx, http.StatusOK, common.SUCCESS, resp)
|
|
}
|
|
}
|