aboutsummaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2018-10-05 08:44:12 -0700
committerChristine Dodrill <me@christine.website>2018-10-05 08:44:12 -0700
commit9877dd68744f2d8771949f60d6b03c3b1da72cb2 (patch)
tree06e95a4ceae32e6daff51df1af71891906c06440 /web
parent79d5cac32717b3444454f0849bc2e7816351d39a (diff)
downloadx-9877dd68744f2d8771949f60d6b03c3b1da72cb2.tar.xz
x-9877dd68744f2d8771949f60d6b03c3b1da72cb2.zip
add package ponyapi
Diffstat (limited to 'web')
-rw-r--r--web/ponyapi/doc.go5
-rw-r--r--web/ponyapi/episode.go18
-rw-r--r--web/ponyapi/get.go114
3 files changed, 137 insertions, 0 deletions
diff --git a/web/ponyapi/doc.go b/web/ponyapi/doc.go
new file mode 100644
index 0000000..4ee2abf
--- /dev/null
+++ b/web/ponyapi/doc.go
@@ -0,0 +1,5 @@
+/*
+Package ponyapi provides glue for applications written in Go
+that want to use PonyAPI for getting information.
+*/
+package ponyapi
diff --git a/web/ponyapi/episode.go b/web/ponyapi/episode.go
new file mode 100644
index 0000000..a3e9443
--- /dev/null
+++ b/web/ponyapi/episode.go
@@ -0,0 +1,18 @@
+package ponyapi
+
+// Episode is an episode of My Little Pony: Friendship is Magic.
+type Episode struct {
+ AirDate int `json:"air_date"`
+ Episode int `json:"episode"`
+ IsMovie bool `json:"is_movie"`
+ Name string `json:"name"`
+ Season int `json:"season"`
+}
+
+type episodeWrapper struct {
+ Episode *Episode `json:"episode"`
+}
+
+type episodes struct {
+ Episodes []Episode `json:"episodes"`
+}
diff --git a/web/ponyapi/get.go b/web/ponyapi/get.go
new file mode 100644
index 0000000..6211dd6
--- /dev/null
+++ b/web/ponyapi/get.go
@@ -0,0 +1,114 @@
+package ponyapi
+
+import (
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "net/http"
+ "net/url"
+)
+
+const (
+ endpoint = "https://ponyapi.apps.xeserv.us/"
+)
+
+func getJSON(fragment string) *http.Request {
+ req, err := http.NewRequest(http.MethodGet, endpoint+fragment, nil)
+ if err != nil {
+ panic(err)
+ }
+ req.Header.Set("Accept", "application/json")
+ return req
+}
+
+func readData(resp *http.Response) ([]byte, error) {
+ if resp.StatusCode%100 != 2 {
+ return nil, fmt.Errorf("status code: %d", resp.StatusCode)
+ }
+
+ data, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+ return data, nil
+}
+
+// ReadEpisode reads information about an invididual episode from an HTTP response.
+func ReadEpisode(resp *http.Response) (*Episode, error) {
+ if resp.StatusCode != 200 {
+ return nil, fmt.Errorf("status code: %d", resp.StatusCode)
+ }
+
+ var ewr episodeWrapper
+ err := json.NewDecoder(resp.Body).Decode(&ewr)
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ return ewr.Episode, nil
+}
+
+// ReadEpisodes reads a slice of episode information out of a HTTP response.
+func ReadEpisodes(resp *http.Response) ([]Episode, error) {
+ if resp.StatusCode != 200 {
+ return nil, fmt.Errorf("status code: %d", resp.StatusCode)
+ }
+
+ var eswr episodes
+ err := json.NewDecoder(resp.Body).Decode(&eswr)
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ return eswr.Episodes, nil
+}
+
+// Newest returns information on the newest episode or an error.
+func Newest() *http.Request {
+ return getJSON("/newest")
+}
+
+// LastAired returns information on the most recently aried episode
+// or an error.
+func LastAired() *http.Request {
+ return getJSON("/last_aired")
+}
+
+// Random returns information on a random episode.
+func Random() *http.Request {
+ return getJSON("/random")
+}
+
+// GetEpisode gets information about season x episode y or an error.
+func GetEpisode(season, episode int) *http.Request {
+ return getJSON(fmt.Sprintf("/season/%d/episode/%d", season, episode))
+}
+
+// AllEpisodes gets all information on all episodes or returns an error.
+func AllEpisodes() *http.Request {
+ return getJSON("/all")
+}
+
+// GetSeason returns all information on season x or returns an error.
+func GetSeason(season int) *http.Request {
+ return getJSON(fmt.Sprintf("/season/%d", season))
+}
+
+// Search takes the give search terms and uses that to search the
+// list of episodes.
+func Search(query string) *http.Request {
+ path, err := url.Parse("/search")
+ if err != nil {
+ panic(err)
+ }
+
+ q := path.Query()
+ q.Set("q", query)
+
+ path.RawQuery = q.Encode()
+
+ return getJSON(path.String())
+}