Skip to content

Commit 32a4bd6

Browse files
committed
Move API-related funcs to api.go
1 parent dc1ab5b commit 32a4bd6

File tree

2 files changed

+189
-184
lines changed

2 files changed

+189
-184
lines changed

cmd/writeas/api.go

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"github.com/atotto/clipboard"
7+
"gopkg.in/urfave/cli.v1"
8+
"io/ioutil"
9+
"net/http"
10+
"net/url"
11+
"strconv"
12+
"strings"
13+
)
14+
15+
func client(read, tor bool, path, query string) (string, *http.Client) {
16+
var u *url.URL
17+
var client *http.Client
18+
if tor {
19+
u, _ = url.ParseRequestURI(hiddenAPIURL)
20+
u.Path = "/api/" + path
21+
client = torClient()
22+
} else {
23+
u, _ = url.ParseRequestURI(apiURL)
24+
u.Path = "/api/" + path
25+
client = &http.Client{}
26+
}
27+
if query != "" {
28+
u.RawQuery = query
29+
}
30+
urlStr := fmt.Sprintf("%v", u)
31+
32+
return urlStr, client
33+
}
34+
35+
// DoFetch retrieves the Write.as post with the given friendlyID,
36+
// optionally via the Tor hidden service.
37+
func DoFetch(friendlyID string, tor bool) error {
38+
path := friendlyID
39+
40+
urlStr, client := client(true, tor, path, "")
41+
42+
r, _ := http.NewRequest("GET", urlStr, nil)
43+
r.Header.Add("User-Agent", "writeas-cli v"+version)
44+
45+
resp, err := client.Do(r)
46+
if err != nil {
47+
return err
48+
}
49+
defer resp.Body.Close()
50+
51+
if resp.StatusCode == http.StatusOK {
52+
content, err := ioutil.ReadAll(resp.Body)
53+
if err != nil {
54+
return err
55+
}
56+
fmt.Printf("%s\n", string(content))
57+
} else if resp.StatusCode == http.StatusNotFound {
58+
return ErrPostNotFound
59+
} else if resp.StatusCode == http.StatusGone {
60+
} else {
61+
return fmt.Errorf("Unable to get post: %s", resp.Status)
62+
}
63+
return nil
64+
}
65+
66+
// DoPost creates a Write.as post, returning an error if it was
67+
// unsuccessful.
68+
func DoPost(c *cli.Context, post []byte, font string, encrypt, tor, code bool) error {
69+
data := url.Values{}
70+
data.Set("w", string(post))
71+
if encrypt {
72+
data.Add("e", "")
73+
}
74+
data.Add("font", getFont(code, font))
75+
76+
urlStr, client := client(false, tor, "", "")
77+
78+
r, _ := http.NewRequest("POST", urlStr, bytes.NewBufferString(data.Encode()))
79+
r.Header.Add("User-Agent", "writeas-cli v"+version)
80+
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
81+
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
82+
83+
resp, err := client.Do(r)
84+
if err != nil {
85+
return err
86+
}
87+
defer resp.Body.Close()
88+
89+
if resp.StatusCode == http.StatusOK {
90+
content, err := ioutil.ReadAll(resp.Body)
91+
if err != nil {
92+
return err
93+
}
94+
95+
nlPos := strings.Index(string(content), "\n")
96+
url := content[:nlPos]
97+
idPos := strings.LastIndex(string(url), "/") + 1
98+
id := string(url[idPos:])
99+
token := string(content[nlPos+1 : len(content)-1])
100+
101+
addPost(id, token)
102+
103+
// Copy URL to clipboard
104+
err = clipboard.WriteAll(string(url))
105+
if err != nil {
106+
Errorln("writeas: Didn't copy to clipboard: %s", err)
107+
} else {
108+
Info(c, "Copied to clipboard.")
109+
}
110+
111+
// Output URL
112+
fmt.Printf("%s\n", url)
113+
} else {
114+
return fmt.Errorf("Unable to post: %s", resp.Status)
115+
}
116+
117+
return nil
118+
}
119+
120+
// DoUpdate updates the given post on Write.as.
121+
func DoUpdate(c *cli.Context, post []byte, friendlyID, token, font string, tor, code bool) error {
122+
urlStr, client := client(false, tor, friendlyID, fmt.Sprintf("t=%s", token))
123+
124+
data := url.Values{}
125+
data.Set("w", string(post))
126+
127+
if code || font != "" {
128+
// Only update font if explicitly changed
129+
data.Add("font", getFont(code, font))
130+
}
131+
132+
r, _ := http.NewRequest("POST", urlStr, bytes.NewBufferString(data.Encode()))
133+
r.Header.Add("User-Agent", "writeas-cli v"+version)
134+
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
135+
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
136+
137+
resp, err := client.Do(r)
138+
if err != nil {
139+
return err
140+
}
141+
defer resp.Body.Close()
142+
143+
if resp.StatusCode == http.StatusOK {
144+
if tor {
145+
Info(c, "Post updated via hidden service.")
146+
} else {
147+
Info(c, "Post updated.")
148+
}
149+
} else {
150+
if debug {
151+
ErrorlnQuit("Problem updating: %s", resp.Status)
152+
} else {
153+
return fmt.Errorf("Post doesn't exist, or bad edit token given.")
154+
}
155+
}
156+
return nil
157+
}
158+
159+
// DoDelete deletes the given post on Write.as.
160+
func DoDelete(c *cli.Context, friendlyID, token string, tor bool) error {
161+
urlStr, client := client(false, tor, friendlyID, fmt.Sprintf("t=%s", token))
162+
163+
r, _ := http.NewRequest("DELETE", urlStr, nil)
164+
r.Header.Add("User-Agent", "writeas-cli v"+version)
165+
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
166+
167+
resp, err := client.Do(r)
168+
if err != nil {
169+
return err
170+
}
171+
defer resp.Body.Close()
172+
173+
if resp.StatusCode == http.StatusOK {
174+
if tor {
175+
Info(c, "Post deleted from hidden service.")
176+
} else {
177+
Info(c, "Post deleted.")
178+
}
179+
removePost(friendlyID)
180+
} else {
181+
if debug {
182+
ErrorlnQuit("Problem deleting: %s", resp.Status)
183+
} else {
184+
return fmt.Errorf("Post doesn't exist, or bad edit token given.")
185+
}
186+
}
187+
188+
return nil
189+
}

cmd/writeas/cli.go

Lines changed: 0 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@ package main
22

33
import (
44
"bufio"
5-
"bytes"
6-
"fmt"
7-
"github.com/atotto/clipboard"
85
"gopkg.in/urfave/cli.v1"
96
"io"
10-
"io/ioutil"
117
"log"
12-
"net/http"
13-
"net/url"
148
"os"
15-
"strconv"
16-
"strings"
179
)
1810

1911
// API constants for communicating with Write.as.
@@ -263,179 +255,3 @@ func handlePost(fullPost []byte, c *cli.Context) error {
263255

264256
return DoPost(c, fullPost, c.String("font"), false, tor, c.Bool("code"))
265257
}
266-
267-
func client(read, tor bool, path, query string) (string, *http.Client) {
268-
var u *url.URL
269-
var client *http.Client
270-
if tor {
271-
u, _ = url.ParseRequestURI(hiddenAPIURL)
272-
u.Path = "/api/" + path
273-
client = torClient()
274-
} else {
275-
u, _ = url.ParseRequestURI(apiURL)
276-
u.Path = "/api/" + path
277-
client = &http.Client{}
278-
}
279-
if query != "" {
280-
u.RawQuery = query
281-
}
282-
urlStr := fmt.Sprintf("%v", u)
283-
284-
return urlStr, client
285-
}
286-
287-
// DoFetch retrieves the Write.as post with the given friendlyID,
288-
// optionally via the Tor hidden service.
289-
func DoFetch(friendlyID string, tor bool) error {
290-
path := friendlyID
291-
292-
urlStr, client := client(true, tor, path, "")
293-
294-
r, _ := http.NewRequest("GET", urlStr, nil)
295-
r.Header.Add("User-Agent", "writeas-cli v"+version)
296-
297-
resp, err := client.Do(r)
298-
if err != nil {
299-
return err
300-
}
301-
defer resp.Body.Close()
302-
303-
if resp.StatusCode == http.StatusOK {
304-
content, err := ioutil.ReadAll(resp.Body)
305-
if err != nil {
306-
return err
307-
}
308-
fmt.Printf("%s\n", string(content))
309-
} else if resp.StatusCode == http.StatusNotFound {
310-
return ErrPostNotFound
311-
} else if resp.StatusCode == http.StatusGone {
312-
} else {
313-
return fmt.Errorf("Unable to get post: %s", resp.Status)
314-
}
315-
return nil
316-
}
317-
318-
// DoPost creates a Write.as post, returning an error if it was
319-
// unsuccessful.
320-
func DoPost(c *cli.Context, post []byte, font string, encrypt, tor, code bool) error {
321-
data := url.Values{}
322-
data.Set("w", string(post))
323-
if encrypt {
324-
data.Add("e", "")
325-
}
326-
data.Add("font", getFont(code, font))
327-
328-
urlStr, client := client(false, tor, "", "")
329-
330-
r, _ := http.NewRequest("POST", urlStr, bytes.NewBufferString(data.Encode()))
331-
r.Header.Add("User-Agent", "writeas-cli v"+version)
332-
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
333-
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
334-
335-
resp, err := client.Do(r)
336-
if err != nil {
337-
return err
338-
}
339-
defer resp.Body.Close()
340-
341-
if resp.StatusCode == http.StatusOK {
342-
content, err := ioutil.ReadAll(resp.Body)
343-
if err != nil {
344-
return err
345-
}
346-
347-
nlPos := strings.Index(string(content), "\n")
348-
url := content[:nlPos]
349-
idPos := strings.LastIndex(string(url), "/") + 1
350-
id := string(url[idPos:])
351-
token := string(content[nlPos+1 : len(content)-1])
352-
353-
addPost(id, token)
354-
355-
// Copy URL to clipboard
356-
err = clipboard.WriteAll(string(url))
357-
if err != nil {
358-
Errorln("writeas: Didn't copy to clipboard: %s", err)
359-
} else {
360-
Info(c, "Copied to clipboard.")
361-
}
362-
363-
// Output URL
364-
fmt.Printf("%s\n", url)
365-
} else {
366-
return fmt.Errorf("Unable to post: %s", resp.Status)
367-
}
368-
369-
return nil
370-
}
371-
372-
// DoUpdate updates the given post on Write.as.
373-
func DoUpdate(c *cli.Context, post []byte, friendlyID, token, font string, tor, code bool) error {
374-
urlStr, client := client(false, tor, friendlyID, fmt.Sprintf("t=%s", token))
375-
376-
data := url.Values{}
377-
data.Set("w", string(post))
378-
379-
if code || font != "" {
380-
// Only update font if explicitly changed
381-
data.Add("font", getFont(code, font))
382-
}
383-
384-
r, _ := http.NewRequest("POST", urlStr, bytes.NewBufferString(data.Encode()))
385-
r.Header.Add("User-Agent", "writeas-cli v"+version)
386-
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
387-
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
388-
389-
resp, err := client.Do(r)
390-
if err != nil {
391-
return err
392-
}
393-
defer resp.Body.Close()
394-
395-
if resp.StatusCode == http.StatusOK {
396-
if tor {
397-
Info(c, "Post updated via hidden service.")
398-
} else {
399-
Info(c, "Post updated.")
400-
}
401-
} else {
402-
if debug {
403-
ErrorlnQuit("Problem updating: %s", resp.Status)
404-
} else {
405-
return fmt.Errorf("Post doesn't exist, or bad edit token given.")
406-
}
407-
}
408-
return nil
409-
}
410-
411-
// DoDelete deletes the given post on Write.as.
412-
func DoDelete(c *cli.Context, friendlyID, token string, tor bool) error {
413-
urlStr, client := client(false, tor, friendlyID, fmt.Sprintf("t=%s", token))
414-
415-
r, _ := http.NewRequest("DELETE", urlStr, nil)
416-
r.Header.Add("User-Agent", "writeas-cli v"+version)
417-
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
418-
419-
resp, err := client.Do(r)
420-
if err != nil {
421-
return err
422-
}
423-
defer resp.Body.Close()
424-
425-
if resp.StatusCode == http.StatusOK {
426-
if tor {
427-
Info(c, "Post deleted from hidden service.")
428-
} else {
429-
Info(c, "Post deleted.")
430-
}
431-
removePost(friendlyID)
432-
} else {
433-
if debug {
434-
ErrorlnQuit("Problem deleting: %s", resp.Status)
435-
} else {
436-
return fmt.Errorf("Post doesn't exist, or bad edit token given.")
437-
}
438-
}
439-
440-
return nil
441-
}

0 commit comments

Comments
 (0)