aboutsummaryrefslogtreecommitdiff
path: root/web/ponyapi/get.go
blob: 6211dd6765392d151981f9246dbae1da4a5a7b27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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())
}