github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/ws/wsmessagesend.go (about) 1 package ws 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "io" 7 "net/http" 8 "net/url" 9 10 "github.com/pkg/errors" 11 "github.com/spf13/cobra" 12 13 "github.com/iotexproject/iotex-core/ioctl/config" 14 "github.com/iotexproject/iotex-core/ioctl/output" 15 ) 16 17 var ( 18 // wsMessageSend represents the w3bstream send message command 19 wsMessageSend = &cobra.Command{ 20 Use: "send", 21 Short: config.TranslateInLang(wsMessageSendShorts, config.UILanguage), 22 RunE: func(cmd *cobra.Command, args []string) error { 23 projectID, err := cmd.Flags().GetUint64("project-id") 24 if err != nil { 25 return output.PrintError(err) 26 } 27 projectVersion, err := cmd.Flags().GetString("project-version") 28 if err != nil { 29 return output.PrintError(err) 30 } 31 data, err := cmd.Flags().GetString("data") 32 if err != nil { 33 return output.PrintError(err) 34 } 35 tok, err := cmd.Flags().GetString("did-vc-token") 36 if err != nil { 37 return errors.Wrap(err, "failed to get flag did-vc-token") 38 } 39 out, err := sendMessage(projectID, projectVersion, data, tok) 40 if err != nil { 41 return output.PrintError(err) 42 } 43 output.PrintResult(out) 44 return nil 45 }, 46 } 47 48 // wsMessageSendShorts w3bstream message send shorts multi-lang support 49 wsMessageSendShorts = map[config.Language]string{ 50 config.English: "send message to w3bstream for zk proofing", 51 config.Chinese: "向w3bstream发送消息请求zk证明", 52 } 53 54 _flagProjectIDUsages = map[config.Language]string{ 55 config.English: "project id", 56 config.Chinese: "项目ID", 57 } 58 _flagProjectVersionUsages = map[config.Language]string{ 59 config.English: "project version", 60 config.Chinese: "项目版本", 61 } 62 _flagSendDataUsages = map[config.Language]string{ 63 config.English: "send data", 64 config.Chinese: "要发送的数据", 65 } 66 ) 67 68 func init() { 69 wsMessageSend.Flags().Uint64P("project-id", "p", 0, config.TranslateInLang(_flagProjectIDUsages, config.UILanguage)) 70 wsMessageSend.Flags().StringP("project-version", "v", "", config.TranslateInLang(_flagProjectVersionUsages, config.UILanguage)) 71 wsMessageSend.Flags().StringP("data", "d", "", config.TranslateInLang(_flagSendDataUsages, config.UILanguage)) 72 wsMessageSend.Flags().StringP("did-vc-token", "t", "", config.TranslateInLang(_flagDIDVCTokenUsages, config.UILanguage)) 73 74 _ = wsMessageSend.MarkFlagRequired("project-id") 75 _ = wsMessageSend.MarkFlagRequired("project-version") 76 _ = wsMessageSend.MarkFlagRequired("data") 77 } 78 79 func sendMessage(projectID uint64, projectVersion string, data string, tok string) (string, error) { 80 reqbody, err := json.Marshal(&sendMessageReq{ 81 ProjectID: projectID, 82 ProjectVersion: projectVersion, 83 Data: data, 84 }) 85 if err != nil { 86 return "", errors.Wrap(err, "failed to build call message") 87 } 88 89 u := &url.URL{ 90 Scheme: "http", 91 Host: config.ReadConfig.WsEndpoint, 92 Path: "/message", 93 } 94 req, err := http.NewRequest(http.MethodPost, u.String(), bytes.NewReader(reqbody)) 95 if err != nil { 96 return "", errors.Wrap(err, "failed to new http request") 97 } 98 99 if tok != "" { 100 req.Header.Set("Authorization", tok) 101 } 102 103 rsp, err := http.DefaultClient.Do(req) 104 if err != nil { 105 return "", errors.Wrap(err, "call w3bsteam failed") 106 } 107 defer rsp.Body.Close() 108 109 if rsp.StatusCode != http.StatusOK { 110 return "", errors.Errorf("call w3bsteam failed: %s", rsp.Status) 111 } 112 113 rspbody, err := io.ReadAll(rsp.Body) 114 if err != nil { 115 return "", errors.Wrap(err, "failed to read responded content") 116 } 117 rspdata := &sendMessageRsp{} 118 if err = json.Unmarshal(rspbody, rspdata); err != nil { 119 return "", errors.Wrap(err, "failed to parse responded content") 120 } 121 out, err := json.MarshalIndent(rspdata, "", " ") 122 if err != nil { 123 return "", errors.Wrap(err, "failed to serialize output") 124 } 125 return string(out), err 126 }