mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-22 17:31:03 +01:00
- Updated multiple controller methods to return VoidResponse instead of generic Response[any]. - Consolidated error handling to utilize GetErrorVoidResponse for consistent error responses. - Enhanced parameter handling in export and file management functions for improved clarity and maintainability. - Refactored health check and login/logout methods to align with new response structure. - Improved overall consistency in response formatting across various endpoints.
93 lines
2.8 KiB
Go
93 lines
2.8 KiB
Go
package controllers
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/crawlab-team/crawlab/core/constants"
|
|
"github.com/crawlab-team/crawlab/core/export"
|
|
"github.com/crawlab-team/crawlab/core/interfaces"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/juju/errors"
|
|
)
|
|
|
|
type PostExportParams struct {
|
|
Type string `path:"type" validate:"required"`
|
|
Target string `query:"target" validate:"required"`
|
|
Conditions string `query:"conditions" description:"Filter conditions. Format: [{\"key\":\"name\",\"op\":\"eq\",\"value\":\"test\"}]"`
|
|
}
|
|
|
|
func PostExport(_ *gin.Context, params *PostExportParams) (response *Response[string], err error) {
|
|
query, err := GetFilterQueryFromConditionString(params.Conditions)
|
|
if err != nil {
|
|
return GetErrorResponse[string](err)
|
|
}
|
|
var exportId string
|
|
switch params.Type {
|
|
case constants.ExportTypeCsv:
|
|
exportId, err = export.GetCsvService().Export(params.Type, params.Target, query)
|
|
case constants.ExportTypeJson:
|
|
exportId, err = export.GetJsonService().Export(params.Type, params.Target, query)
|
|
default:
|
|
return GetErrorResponse[string](errors.BadRequestf("invalid export type: %s", params.Type))
|
|
}
|
|
if err != nil {
|
|
return GetErrorResponse[string](err)
|
|
}
|
|
|
|
return GetDataResponse(exportId)
|
|
}
|
|
|
|
type GetExportParams struct {
|
|
Type string `path:"type" validate:"required"`
|
|
Id string `path:"id" validate:"required"`
|
|
}
|
|
|
|
func GetExport(_ *gin.Context, params *GetExportParams) (response *Response[interfaces.Export], err error) {
|
|
var exp interfaces.Export
|
|
switch params.Type {
|
|
case constants.ExportTypeCsv:
|
|
exp, err = export.GetCsvService().GetExport(params.Id)
|
|
case constants.ExportTypeJson:
|
|
exp, err = export.GetJsonService().GetExport(params.Id)
|
|
default:
|
|
return GetErrorResponse[interfaces.Export](errors.BadRequestf("invalid export type: %s", params.Type))
|
|
}
|
|
if err != nil {
|
|
return GetErrorResponse[interfaces.Export](err)
|
|
}
|
|
|
|
return GetDataResponse(exp)
|
|
}
|
|
|
|
type GetExportDownloadParams struct {
|
|
Type string `path:"type" validate:"required"`
|
|
Id string `path:"id" validate:"required"`
|
|
}
|
|
|
|
func GetExportDownload(c *gin.Context, params *GetExportDownloadParams) (err error) {
|
|
var exp interfaces.Export
|
|
switch params.Type {
|
|
case constants.ExportTypeCsv:
|
|
exp, err = export.GetCsvService().GetExport(params.Id)
|
|
case constants.ExportTypeJson:
|
|
exp, err = export.GetJsonService().GetExport(params.Id)
|
|
default:
|
|
return errors.BadRequestf("invalid export type: %s", params.Type)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
switch params.Type {
|
|
case constants.ExportTypeCsv:
|
|
c.Header("Content-Type", "text/csv")
|
|
case constants.ExportTypeJson:
|
|
c.Header("Content-Type", "text/plain")
|
|
default:
|
|
return errors.BadRequestf("invalid export type: %s", params.Type)
|
|
}
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", exp.GetDownloadPath()))
|
|
c.File(exp.GetDownloadPath())
|
|
return nil
|
|
}
|