-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
codes.go
50 lines (41 loc) · 1.09 KB
/
codes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package codes
import (
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
codes "google.golang.org/grpc/codes"
)
type Code codes.Code
const (
// Accepted device accepts request and action will be proceed in future.
Accepted Code = iota + 4096
// MethodNotAllowed device refuse call the method.
MethodNotAllowed
// Created success status response code indicates that the request has succeeded and has led to the creation of a resource.
Created
// InvalidCode cannot determines result from device code.
InvalidCode Code = iota + (2 * 4096)
)
var code2string = map[Code]string{
Created: "created",
MethodNotAllowed: "methodNotAllowed",
Accepted: "accepted",
}
var code2httpCode = map[Code]int{
Created: http.StatusCreated,
MethodNotAllowed: http.StatusMethodNotAllowed,
Accepted: http.StatusAccepted,
}
func (c Code) ToHTTPCode() int {
v, ok := code2httpCode[c]
if ok {
return v
}
return runtime.HTTPStatusFromCode(codes.Code(c))
}
func (c Code) String() string {
v, ok := code2string[c]
if ok {
return v
}
return codes.Code(c).String()
}