oceanbase

Форк
0
81 строка · 2.1 Кб
1
/**
2
 * Copyright (c) 2021 OceanBase
3
 * OceanBase CE is licensed under Mulan PubL v2.
4
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
5
 * You may obtain a copy of Mulan PubL v2 at:
6
 *          http://license.coscl.org.cn/MulanPubL-2.0
7
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10
 * See the Mulan PubL v2 for more details.
11
 */
12

13
package server
14

15
import (
16
	"fmt"
17
	"net/http"
18
)
19

20
type ApiResponse struct {
21
	Code       int         `json:"Code"`
22
	Message    string      `json:"Message"`
23
	Successful bool        `json:"Success"`
24
	Data       interface{} `json:"Data"`
25
	TraceId    string      `json:"Trace"`
26
	Server     string      `json:"Server"`
27
	Cost       int64       `json:"Cost"`
28
}
29

30
type IterableData struct {
31
	Contents interface{} `json:"Contents"`
32
}
33

34
func NewSuccessResponse(data interface{}) *ApiResponse {
35
	return &ApiResponse{
36
		Code:       http.StatusOK,
37
		Message:    "successful",
38
		Successful: true,
39
		Data:       data,
40
	}
41
}
42

43
func NewBadRequestResponse(err error) *ApiResponse {
44
	return &ApiResponse{
45
		Code:       http.StatusBadRequest,
46
		Message:    fmt.Sprintf("bad request: %v", err),
47
		Successful: false,
48
	}
49
}
50

51
func NewIllegalArgumentResponse(err error) *ApiResponse {
52
	return &ApiResponse{
53
		Code:       http.StatusBadRequest,
54
		Message:    fmt.Sprintf("illegal argument: %v", err),
55
		Successful: false,
56
	}
57
}
58

59
func NewNotFoundResponse(err error) *ApiResponse {
60
	return &ApiResponse{
61
		Code:       http.StatusNotFound,
62
		Message:    fmt.Sprintf("resource not found: %v", err),
63
		Successful: false,
64
	}
65
}
66

67
func NewNotImplementedResponse(err error) *ApiResponse {
68
	return &ApiResponse{
69
		Code:       http.StatusNotImplemented,
70
		Message:    fmt.Sprintf("request not implemented: %v", err),
71
		Successful: false,
72
	}
73
}
74

75
func NewErrorResponse(err error) *ApiResponse {
76
	return &ApiResponse{
77
		Code:       http.StatusInternalServerError,
78
		Message:    fmt.Sprintf("got internal error: %v", err),
79
		Successful: false,
80
	}
81
}
82

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.