Add Go client

This commit is contained in:
Christine Dodrill 2015-08-12 18:07:20 -07:00
parent 2325d76df8
commit d7e5378b5e
4 changed files with 171 additions and 0 deletions

5
client/go/doc.go Normal file
View File

@ -0,0 +1,5 @@
/*
Package ponyapi provides glue for applications written in Go
that want to use PonyAPI for getting information.
*/
package ponyapi

18
client/go/episode.go Normal file
View File

@ -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"`
}

91
client/go/get.go Normal file
View File

@ -0,0 +1,91 @@
package ponyapi
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
const (
endpoint = "http://ponyapi.apps.xeserv.us"
)
func getJson(fragment string) (data []byte, err error) {
resp, err := http.Get(endpoint + fragment)
if err != nil {
return nil, err
}
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return
}
func getEpisode(fragment string) (*Episode, error) {
data, err := getJson(fragment)
if err != nil {
return nil, err
}
ewr := &episodeWrapper{}
err = json.Unmarshal(data, ewr)
if err != nil {
return nil, err
}
return ewr.Episode, nil
}
func getEpisodes(fragment string) ([]*Episode, error) {
data, err := getJson(fragment)
if err != nil {
return nil, err
}
eswr := &episodes{}
err = json.Unmarshal(data, eswr)
if err != nil {
return nil, err
}
return eswr.Episodes, nil
}
func Newest() (*Episode, error) {
return getEpisode("/newest")
}
func Random() (*Episode, error) {
return getEpisode("/random")
}
func GetEpisode(season, episode int) (*Episode, error) {
return getEpisode(fmt.Sprintf("/season/%d/episode/%d", season, episode))
}
func AllEpisodes() ([]*Episode, error) {
return getEpisodes("/all")
}
func GetSeason(season int) ([]*Episode, error) {
return getEpisodes(fmt.Sprintf("/season/%d", season))
}
func Search(query string) ([]*Episode, error) {
path, err := url.Parse("/search")
if err != nil {
panic(err)
}
q := path.Query()
q.Set("q", query)
path.RawQuery = q.Encode()
return getEpisodes(path.String())
}

57
client/go/get_test.go Normal file
View File

@ -0,0 +1,57 @@
package ponyapi
import "testing"
func TestNewestEpisode(t *testing.T) {
ep, err := Newest()
if err != nil {
t.Fatal(err)
}
t.Logf("%#v", ep)
}
func TestRandomEpisode(t *testing.T) {
ep, err := Random()
if err != nil {
t.Fatal(err)
}
t.Logf("%#v", ep)
}
func TestSpecificEpisode(t *testing.T) {
ep, err := GetEpisode(2, 24)
if err != nil {
t.Fatal(err)
}
t.Logf("%#v", ep)
}
func TestAllEpisodes(t *testing.T) {
eps, err := AllEpisodes()
if err != nil {
t.Fatal(err)
}
t.Logf("%d episodes", len(eps))
}
func TestGetSeason(t *testing.T) {
eps, err := GetSeason(4)
if err != nil {
t.Fatal(err)
}
t.Logf("%d episodes", len(eps))
}
func TestSearch(t *testing.T) {
eps, err := Search("pony")
if err != nil {
t.Fatal(err)
}
t.Logf("%#v", eps)
}