diff options
| author | Christine Dodrill <me@christine.website> | 2018-10-04 22:48:42 -0700 |
|---|---|---|
| committer | Christine Dodrill <me@christine.website> | 2018-10-04 22:49:08 -0700 |
| commit | 9c9ae00ab8bd7c2a99590daf42fe9df475131929 (patch) | |
| tree | 44b08805061c0a1b21fa605b3e48544542aa3a85 /web | |
| parent | 9fd916604c371b41111c14e65d5534d4dbc30ff8 (diff) | |
| download | x-9c9ae00ab8bd7c2a99590daf42fe9df475131929.tar.xz x-9c9ae00ab8bd7c2a99590daf42fe9df475131929.zip | |
add bot to translate toki pona to english
Diffstat (limited to 'web')
| -rw-r--r-- | web/tokipana/tokipana.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/web/tokipana/tokipana.go b/web/tokipana/tokipana.go new file mode 100644 index 0000000..2daa54b --- /dev/null +++ b/web/tokipana/tokipana.go @@ -0,0 +1,44 @@ +// Package tokipana wraps http://inamidst.com/services/tokipana. +package tokipana + +import ( + "errors" + "net/http" + "net/url" +) + +// 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 +} + +// Translate returns a request to translate the given toki pona text into english. +func Translate(text string) *http.Request { + u, _ := url.Parse(APIURL) + q := u.Query() + q.Set("text", text) + u.RawQuery = q.Encode() + + req, err := http.NewRequest(http.MethodGet, u.String(), nil) + if err != nil { + panic(err) + } + + return req +} |
