/
http.go
113 lines (92 loc) · 2.48 KB
/
http.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package http
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"time"
"github.com/fox-one/mixin-cli/v2/session"
"github.com/fox-one/mixin-sdk-go/v2"
"github.com/nojima/httpie-go/exchange"
"github.com/nojima/httpie-go/input"
"github.com/spf13/cobra"
)
func NewCmdHttp() *cobra.Command {
var opt struct {
raw string
dumpRequest bool
}
cmd := &cobra.Command{
Use: "http",
Args: cobra.MinimumNArgs(1),
Short: "mixin api http client",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
s := session.From(ctx)
client, err := s.GetClient()
if err != nil {
return err
}
in, err := input.ParseArgs(args, cmd.InOrStdin(), &input.Options{JSON: true})
if err != nil {
return err
}
host, _ := url.Parse(mixin.GetRestyClient().HostURL)
in.URL.Host = host.Host
in.URL.Scheme = host.Scheme
in.Header.Fields = append(in.Header.Fields, input.Field{
Name: "User-Agent",
Value: "mixin-cli/" + s.Version,
})
if opt.raw != "" {
var raw json.RawMessage
if err := json.Unmarshal([]byte(opt.raw), &raw); err != nil {
return fmt.Errorf("json decode raw body failed: %w", err)
}
in.Method = http.MethodPost
in.Body = input.Body{
BodyType: input.RawBody,
Raw: raw,
}
}
req, err := exchange.BuildHTTPRequest(in, &exchange.Options{})
if err != nil {
return err
}
sig := mixin.SignRequest(req)
if token := client.Signer.SignToken(sig, mixin.RandomTraceID(), time.Minute); token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
req.Header.Set("Content-Type", "application/json")
if opt.dumpRequest {
b, err := httputil.DumpRequest(req, true)
if err != nil {
return err
}
cmd.Println(string(b), "\n")
}
resp, err := mixin.GetClient().Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
var body struct {
Error *mixin.Error
Data json.RawMessage `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
return fmt.Errorf("decode json body failed: %w", err)
}
if body.Error != nil && body.Error.Code > 0 {
return body.Error
}
data, _ := json.MarshalIndent(body.Data, "", " ")
fmt.Fprintln(cmd.OutOrStdout(), string(data))
return nil
},
}
cmd.Flags().StringVar(&opt.raw, "raw", "", "raw json body")
cmd.Flags().BoolVarP(&opt.dumpRequest, "dump", "v", false, "dump request information")
return cmd
}