|
| 1 | +package pi |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | +) |
| 8 | + |
| 9 | +type channelsCommand struct { |
| 10 | + Post createChannelCommand `description:"create Channel" command:"create" subcommands-optional:"true"` |
| 11 | + Update updateChannelCommand `description:"update Channel Definition" command:"update" subcommands-optional:"true"` |
| 12 | + Get getChannelsCommand `description:"get Channel Definitions" command:"get" subcommands-optional:"true"` |
| 13 | + Delete deleteChannelCommand `description:"delete Channel" command:"delete" subcommands-optional:"true"` |
| 14 | +} |
| 15 | + |
| 16 | +type createChannelCommand struct { |
| 17 | + Username string `short:"u" long:"username" description:"User name of channel owner."` |
| 18 | + ID string `short:"i" long:"channel-id" description:"ID for identifying the channel." required:"true"` |
| 19 | + Name string `short:"n" long:"name" description:"The name of the channel." required:"true"` |
| 20 | + Type string `short:"t" long:"type" description:"The type for notification." required:"true"` |
| 21 | + Detail string `short:"d" long:"detail" description:"Object that specifies the details of the type. It is specified as JSON string." required:"true"` |
| 22 | +} |
| 23 | + |
| 24 | +type createChannelParam struct { |
| 25 | + ID string `json:"id"` |
| 26 | + Name string `json:"name"` |
| 27 | + Type string `json:"type"` |
| 28 | + Detail json.RawMessage `json:"detail"` |
| 29 | +} |
| 30 | + |
| 31 | +type updateChannelCommand struct { |
| 32 | + Username string `short:"u" long:"username" description:"User name of channel owner."` |
| 33 | + ID string `short:"i" long:"channel-id" description:"ID for identifying the channel." required:"true"` |
| 34 | + Name string `short:"n" long:"name" description:"The name of the channel."` |
| 35 | + Type string `short:"t" long:"type" description:"The type for notification."` |
| 36 | + Detail string `short:"d" long:"detail" description:"Object that specifies the details of the type. It is specified as JSON string."` |
| 37 | +} |
| 38 | + |
| 39 | +type updateChannelParam struct { |
| 40 | + ID string `json:"id"` |
| 41 | + Name string `json:"name,omitempty"` |
| 42 | + Type string `json:"type,omitempty"` |
| 43 | + Detail json.RawMessage `json:"detail,omitempty"` |
| 44 | +} |
| 45 | + |
| 46 | +type getChannelsCommand struct { |
| 47 | + Username string `short:"u" long:"username" description:"User name of channel owner."` |
| 48 | +} |
| 49 | + |
| 50 | +type deleteChannelCommand struct { |
| 51 | + Username string `short:"u" long:"username" description:"User name of channel owner."` |
| 52 | + ID string `short:"i" long:"channel-id" description:"ID for identifying the channel." required:"true"` |
| 53 | +} |
| 54 | + |
| 55 | +func (cC *createChannelCommand) Execute(args []string) error { |
| 56 | + req, err := generateCreateChannelRequest(cC) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + err = doRequest(req) |
| 62 | + return err |
| 63 | +} |
| 64 | + |
| 65 | +func generateCreateChannelRequest(cC *createChannelCommand) (*http.Request, error) { |
| 66 | + username, err := getUsername(cC.Username) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + paramStruct := &createChannelParam{ |
| 72 | + ID: cC.ID, |
| 73 | + Name: cC.Name, |
| 74 | + Type: cC.Type, |
| 75 | + Detail: json.RawMessage(cC.Detail), |
| 76 | + } |
| 77 | + |
| 78 | + req, err := generateRequestWithToken( |
| 79 | + "POST", |
| 80 | + fmt.Sprintf("v1/users/%s/channels", username), |
| 81 | + paramStruct, |
| 82 | + ) |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("Failed to generate create api request : %s", err) |
| 85 | + } |
| 86 | + |
| 87 | + return req, nil |
| 88 | +} |
| 89 | + |
| 90 | +func (uC *updateChannelCommand) Execute(args []string) error { |
| 91 | + req, err := generateUpdateChannelRequest(uC) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + err = doRequest(req) |
| 97 | + return err |
| 98 | +} |
| 99 | + |
| 100 | +func generateUpdateChannelRequest(uC *updateChannelCommand) (*http.Request, error) { |
| 101 | + username, err := getUsername(uC.Username) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + paramStruct := &updateChannelParam{ |
| 107 | + ID: uC.ID, |
| 108 | + Name: uC.Name, |
| 109 | + Type: uC.Type, |
| 110 | + Detail: json.RawMessage(uC.Detail), |
| 111 | + } |
| 112 | + |
| 113 | + req, err := generateRequestWithToken( |
| 114 | + "PUT", |
| 115 | + fmt.Sprintf("v1/users/%s/channels/%s", username, uC.ID), |
| 116 | + paramStruct, |
| 117 | + ) |
| 118 | + if err != nil { |
| 119 | + return nil, fmt.Errorf("Failed to generate update api request : %s", err) |
| 120 | + } |
| 121 | + |
| 122 | + return req, nil |
| 123 | +} |
| 124 | + |
| 125 | +func (gC *getChannelsCommand) Execute(args []string) error { |
| 126 | + req, err := generateGetChannelsRequest(gC) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + err = doRequest(req) |
| 132 | + return err |
| 133 | +} |
| 134 | + |
| 135 | +func generateGetChannelsRequest(gC *getChannelsCommand) (*http.Request, error) { |
| 136 | + username, err := getUsername(gC.Username) |
| 137 | + if err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + |
| 141 | + req, err := generateRequestWithToken( |
| 142 | + "GET", |
| 143 | + fmt.Sprintf("v1/users/%s/channels", username), |
| 144 | + nil, |
| 145 | + ) |
| 146 | + if err != nil { |
| 147 | + return nil, fmt.Errorf("Failed to generate get api request : %s", err) |
| 148 | + } |
| 149 | + |
| 150 | + return req, nil |
| 151 | +} |
| 152 | + |
| 153 | +func (dC *deleteChannelCommand) Execute(args []string) error { |
| 154 | + req, err := generateDeleteChannelRequest(dC) |
| 155 | + if err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + |
| 159 | + err = doRequest(req) |
| 160 | + return err |
| 161 | +} |
| 162 | + |
| 163 | +func generateDeleteChannelRequest(dC *deleteChannelCommand) (*http.Request, error) { |
| 164 | + username, err := getUsername(dC.Username) |
| 165 | + if err != nil { |
| 166 | + return nil, err |
| 167 | + } |
| 168 | + |
| 169 | + req, err := generateRequestWithToken( |
| 170 | + "DELETE", |
| 171 | + fmt.Sprintf("v1/users/%s/channels/%s", username, dC.ID), |
| 172 | + nil, |
| 173 | + ) |
| 174 | + if err != nil { |
| 175 | + return nil, fmt.Errorf("Failed to generate delete api request : %s", err) |
| 176 | + } |
| 177 | + return req, nil |
| 178 | +} |
0 commit comments