Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit e8a4548

Browse files
authored
Merge pull request #20 from a-know/follow-pixela-updates
Follow pixela updates
2 parents 39404b2 + e4145d3 commit e8a4548

15 files changed

+1399
-49
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ devel-deps: deps
1717
$(GO) get ${u} \
1818
golang.org/x/lint/golint \
1919
github.com/rakyll/gotest \
20-
github.com/motemen/gobump/cmd/gobump \
20+
github.com/x-motemen/gobump \
2121
github.com/Songmu/goxz/cmd/goxz \
2222
github.com/Songmu/ghch/cmd/ghch \
2323
github.com/tcnksm/ghr

api.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/ioutil"
99
"net/http"
1010
"os"
11+
"strings"
1112
)
1213

1314
func generateRequest(method string, path string, paramStruct interface{}) (*http.Request, error) {
@@ -17,12 +18,17 @@ func generateRequest(method string, path string, paramStruct interface{}) (*http
1718
}
1819

1920
var reqBody io.Reader
21+
buffer := &bytes.Buffer{}
2022
if paramStruct != nil {
21-
params, err := json.Marshal(paramStruct)
23+
encoder := json.NewEncoder(buffer)
24+
encoder.SetEscapeHTML(false)
25+
err := encoder.Encode(paramStruct)
2226
if err != nil {
2327
return nil, fmt.Errorf("Failed to marshal options to json : %s", err)
2428
}
25-
reqBody = bytes.NewBuffer(params)
29+
str := string(buffer.Bytes())
30+
str = strings.TrimRight(str, "\n")
31+
reqBody = bytes.NewBuffer([]byte(str))
2632
}
2733

2834
req, err := http.NewRequest(

channels.go

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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

Comments
 (0)