aboutsummaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2019-02-02 06:53:12 -0800
committerChristine Dodrill <me@christine.website>2019-02-02 06:53:12 -0800
commit7bcfbc6a4da40f498af83e40c3c57ef02ec212cf (patch)
treeee224b7b08722e06d78f49bc557249891264e38f /web
parentda3595468056f1dd04d79c908f8a65dc0c5a397f (diff)
downloadx-7bcfbc6a4da40f498af83e40c3c57ef02ec212cf.tar.xz
x-7bcfbc6a4da40f498af83e40c3c57ef02ec212cf.zip
web: use error type, simplify some code
Diffstat (limited to 'web')
-rw-r--r--web/discordwebhook/webhook.go2
-rw-r--r--web/error.go2
-rw-r--r--web/switchcounter/switchc.go16
-rw-r--r--web/tokipana/tokipana.go14
-rw-r--r--web/tokiponatokens/toki_pona.go14
5 files changed, 35 insertions, 13 deletions
diff --git a/web/discordwebhook/webhook.go b/web/discordwebhook/webhook.go
index 6cbe6d1..91df381 100644
--- a/web/discordwebhook/webhook.go
+++ b/web/discordwebhook/webhook.go
@@ -59,7 +59,7 @@ func Send(whurl string, w Webhook) *http.Request {
// Validate validates the response from Discord.
func Validate(resp *http.Response) error {
- if resp.StatusCode/100 != 2 {
+ if resp.StatusCode != http.StatusOK {
return web.NewError(http.StatusOK, resp)
}
diff --git a/web/error.go b/web/error.go
index 051d61d..d5ff31a 100644
--- a/web/error.go
+++ b/web/error.go
@@ -13,6 +13,8 @@ import (
// NewError creates an Error based on an expected HTTP status code vs data populated
// from an HTTP response.
+//
+// This consumes the body of the HTTP response.
func NewError(wantStatusCode int, resp *http.Response) error {
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
diff --git a/web/switchcounter/switchc.go b/web/switchcounter/switchc.go
index b74eb42..f40929f 100644
--- a/web/switchcounter/switchc.go
+++ b/web/switchcounter/switchc.go
@@ -6,6 +6,8 @@ import (
"encoding/json"
"net/http"
"time"
+
+ "github.com/Xe/x/web"
)
type arg struct {
@@ -19,6 +21,16 @@ type Status struct {
StartedAt time.Time `json:"started_at"`
}
+// Validate ensures a HTTP response contains the expected fields.
+func Validate(resp *http.Response) error {
+ if resp.StatusCode == http.StatusOK {
+ return nil
+ }
+
+ return web.NewError(http.StatusOK, resp)
+}
+
+// API is a builder for HTTP requests to interface with Switch Counter.
type API struct {
url string // webhook url
}
@@ -44,6 +56,7 @@ func (a API) makeRequestWith(body interface{}) (*http.Request, error) {
return req, nil
}
+// Status returns a request for which systemmate is currently in front.
func (a API) Status() *http.Request {
result, err := a.makeRequestWith(arg{Command: "switch"})
if err != nil {
@@ -52,6 +65,7 @@ func (a API) Status() *http.Request {
return result
}
+// Switch changes the recorded front to the given systemmate.
func (a API) Switch(front string) *http.Request {
result, err := a.makeRequestWith(arg{Command: "switch", MemberName: front})
if err != nil {
@@ -61,7 +75,7 @@ func (a API) Switch(front string) *http.Request {
}
// NewHTTPClient creates a new instance of API over HTTP.
-func NewHTTPClient(a *http.Client, webhookURL string) API {
+func NewHTTPClient(webhookURL string) API {
return API{
url: webhookURL,
}
diff --git a/web/tokipana/tokipana.go b/web/tokipana/tokipana.go
index 2daa54b..b5871ee 100644
--- a/web/tokipana/tokipana.go
+++ b/web/tokipana/tokipana.go
@@ -2,30 +2,22 @@
package tokipana
import (
- "errors"
"net/http"
"net/url"
+
+ "github.com/Xe/x/web"
)
// The API URL.
const APIURL = `http://inamidst.com/services/tokipana`
-// Errors
-var (
- ErrInvalidRequest = errors.New("tokipana: invalid request")
-)
-
// Validate checks if a response from the API is valid or not.
func Validate(resp *http.Response) error {
if resp.StatusCode == http.StatusOK {
return nil
}
- if resp.StatusCode%100 != 2 {
- return ErrInvalidRequest
- }
-
- return nil
+ return web.NewError(http.StatusOK, resp)
}
// Translate returns a request to translate the given toki pona text into english.
diff --git a/web/tokiponatokens/toki_pona.go b/web/tokiponatokens/toki_pona.go
index f1079cb..c3e3fe3 100644
--- a/web/tokiponatokens/toki_pona.go
+++ b/web/tokiponatokens/toki_pona.go
@@ -5,6 +5,8 @@ import (
"encoding/json"
"net/http"
"strings"
+
+ "github.com/Xe/x/web"
)
// Part is an individual part of a sentence.
@@ -90,6 +92,14 @@ const (
// Sentence is a series of sentence parts. This correlates to one Toki Pona sentence.
type Sentence []Part
+func validate(resp *http.Response) error {
+ if resp.StatusCode != http.StatusOK {
+ return web.NewError(http.StatusOK, resp)
+ }
+
+ return nil
+}
+
// Tokenize returns a series of toki pona tokens.
func Tokenize(aurl, text string) ([]Sentence, error) {
buf := bytes.NewBuffer([]byte(text))
@@ -105,6 +115,10 @@ func Tokenize(aurl, text string) ([]Sentence, error) {
return nil, err
}
defer resp.Body.Close()
+ err = validate(resp)
+ if err != nil {
+ return nil, err
+ }
var result []Sentence
err = json.NewDecoder(resp.Body).Decode(&result)