-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponse.go
54 lines (49 loc) · 1.22 KB
/
response.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
51
52
53
54
package opensea
import (
"errors"
"fmt"
"net/http"
"github.com/casiphia/gopkg/restgo"
jsoniter "github.com/json-iterator/go"
)
var (
ErrNotFound = errors.New("resource.not.found")
ErrTooManyRequests = errors.New("too.many.requests")
ErrInternalServer = errors.New("internal.server.error")
)
var (
jConfig = jsoniter.Config{
MarshalFloatWith6Digits: false,
EscapeHTML: true,
SortMapKeys: true,
UseNumber: true,
TagKey: "opensea",
OnlyTaggedField: false,
ValidateJsonRawMessage: true,
}.Froze()
)
func ParseRsp(rsp restgo.IResponse, i interface{}) error {
var statusCode = rsp.StatusCode()
switch statusCode {
case http.StatusOK:
var data, err = rsp.Data()
if err != nil {
return err
}
return jConfig.Unmarshal(data, i)
case http.StatusBadRequest, http.StatusNotAcceptable:
var data, err = rsp.Data()
if err != nil {
return err
}
return fmt.Errorf("bad.request:%s", data)
case http.StatusNotFound:
return ErrNotFound
case http.StatusTooManyRequests:
return ErrTooManyRequests
case http.StatusInternalServerError:
return ErrInternalServer
default:
return fmt.Errorf("unknown.http.status.code: %d", statusCode)
}
}