Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
3b208aeeaf | |||
![]() |
bfc0e5c417 | ||
![]() |
e6183ddfa3 | ||
![]() |
1591816684 | ||
![]() |
8ce29414f4 | ||
![]() |
faa5474d22 | ||
![]() |
de98e6f444 | ||
![]() |
07d1f0962a | ||
![]() |
e5e717f3e1 | ||
![]() |
79d24f2ba0 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
api_test.go
|
@ -2,7 +2,7 @@ package anilist
|
|||||||
|
|
||||||
import "context"
|
import "context"
|
||||||
|
|
||||||
func (api *Api) GetAiringSchedule(ctx context.Context, vars AiringScheduleQuery, onError func(error)) Cursor[AiringSchedule] {
|
func (api *Api) GetAiringSchedule(ctx context.Context, vars AiringScheduleQuery, onError func(error)) *Cursor[AiringSchedule] {
|
||||||
resp := responseObj[*page[AiringSchedule]]{}
|
resp := responseObj[*page[AiringSchedule]]{}
|
||||||
return requestPaged(api, ctx, getAiringScheduleQuery, vars.toMap(), &resp, onError)
|
return requestPaged(api, ctx, getAiringScheduleQuery, vars.toMap(), &resp, onError)
|
||||||
}
|
}
|
||||||
|
12
api.go
12
api.go
@ -49,6 +49,10 @@ func request[T any](api *Api, query string, vars map[string]interface{}, respObj
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
return handleError(resp)
|
||||||
|
}
|
||||||
|
|
||||||
//data, _ := ioutil.ReadAll(resp.Body)
|
//data, _ := ioutil.ReadAll(resp.Body)
|
||||||
//fmt.Println(string(data))
|
//fmt.Println(string(data))
|
||||||
|
|
||||||
@ -61,7 +65,7 @@ func request[T any](api *Api, query string, vars map[string]interface{}, respObj
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func requestPaged[R any](api *Api, ctx context.Context, query string, vars map[string]interface{}, respObj *responseObj[*page[R]], onError func(error)) Cursor[R] {
|
func requestPaged[R any](api *Api, ctx context.Context, query string, vars map[string]interface{}, respObj *responseObj[*page[R]], onError func(error)) *Cursor[R] {
|
||||||
resp := responseObj[struct {
|
resp := responseObj[struct {
|
||||||
Page *page[R] `json:"Page"`
|
Page *page[R] `json:"Page"`
|
||||||
}]{}
|
}]{}
|
||||||
@ -90,6 +94,10 @@ func requestPaged[R any](api *Api, ctx context.Context, query string, vars map[s
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if resp.Data.Page == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for _, value := range resp.Data.Page.Data() {
|
for _, value := range resp.Data.Page.Data() {
|
||||||
value := value
|
value := value
|
||||||
select {
|
select {
|
||||||
@ -105,7 +113,7 @@ func requestPaged[R any](api *Api, ctx context.Context, query string, vars map[s
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return Cursor[R]{
|
return &Cursor[R]{
|
||||||
Chan: out,
|
Chan: out,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cancelFunc: cancelFunc,
|
cancelFunc: cancelFunc,
|
||||||
|
32
api_error.go
Normal file
32
api_error.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package anilist
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type apiError struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
Status int `json:"status"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type apiErrResp struct {
|
||||||
|
Errors []apiError `json:"errors"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleError(resp *http.Response) error {
|
||||||
|
respObj := &apiErrResp{}
|
||||||
|
|
||||||
|
dec := json.NewDecoder(resp.Body)
|
||||||
|
err := dec.Decode(respObj)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(respObj.Errors) == 0 {
|
||||||
|
return errors.New("unknown API error")
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.New(respObj.Errors[0].Message)
|
||||||
|
}
|
10
cursor.go
10
cursor.go
@ -8,16 +8,16 @@ type Cursor[T any] struct {
|
|||||||
Chan <-chan *T
|
Chan <-chan *T
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Cursor[T]) First() *T {
|
func (c *Cursor[T]) First() *T {
|
||||||
defer c.cancelFunc()
|
defer c.cancelFunc()
|
||||||
return <-c.Chan
|
return <-c.Chan
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Cursor[T]) Close() {
|
func (c *Cursor[T]) Close() {
|
||||||
defer c.cancelFunc()
|
c.cancelFunc()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Cursor[T]) Next() (*T, bool) {
|
func (c *Cursor[T]) Next() (*T, bool) {
|
||||||
if c.ctx.Err() == nil {
|
if c.ctx.Err() == nil {
|
||||||
value, ok := <-c.Chan
|
value, ok := <-c.Chan
|
||||||
return value, ok
|
return value, ok
|
||||||
@ -26,7 +26,7 @@ func (c Cursor[T]) Next() (*T, bool) {
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Cursor[T]) Slice() []T {
|
func (c *Cursor[T]) Slice() []T {
|
||||||
s := make([]T, 0)
|
s := make([]T, 0)
|
||||||
for value, ok := c.Next(); ok; value, ok = c.Next() {
|
for value, ok := c.Next(); ok; value, ok = c.Next() {
|
||||||
s = append(s, *value)
|
s = append(s, *value)
|
||||||
|
2
go.mod
2
go.mod
@ -1,3 +1,3 @@
|
|||||||
module git.tordarus.net/Tordarus/anilist
|
module git.tordarus.net/tordarus/anilist
|
||||||
|
|
||||||
go 1.18
|
go 1.18
|
||||||
|
2
media.go
2
media.go
@ -2,7 +2,7 @@ package anilist
|
|||||||
|
|
||||||
import "context"
|
import "context"
|
||||||
|
|
||||||
func (api *Api) GetMedia(ctx context.Context, vars MediaQuery, onError func(error)) Cursor[Media] {
|
func (api *Api) GetMedia(ctx context.Context, vars MediaQuery, onError func(error)) *Cursor[Media] {
|
||||||
resp := responseObj[*page[Media]]{}
|
resp := responseObj[*page[Media]]{}
|
||||||
return requestPaged(api, ctx, getMediaQuery, vars.toMap(), &resp, onError)
|
return requestPaged(api, ctx, getMediaQuery, vars.toMap(), &resp, onError)
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ package anilist
|
|||||||
|
|
||||||
import "context"
|
import "context"
|
||||||
|
|
||||||
func (api *Api) GetMediaList(ctx context.Context, vars MediaListQuery, onError func(error)) Cursor[MediaList] {
|
func (api *Api) GetMediaList(ctx context.Context, vars MediaListQuery, onError func(error)) *Cursor[MediaList] {
|
||||||
resp := responseObj[*page[MediaList]]{}
|
resp := responseObj[*page[MediaList]]{}
|
||||||
return requestPaged(api, ctx, getMediaListQuery, vars.toMap(), &resp, onError)
|
return requestPaged(api, ctx, getMediaListQuery, vars.toMap(), &resp, onError)
|
||||||
}
|
}
|
||||||
|
46
types.go
46
types.go
@ -9,8 +9,10 @@ type User struct {
|
|||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MediaID int
|
||||||
|
|
||||||
type Media struct {
|
type Media struct {
|
||||||
ID int `json:"id"`
|
ID MediaID `json:"id"`
|
||||||
Title MediaTitle `json:"title"`
|
Title MediaTitle `json:"title"`
|
||||||
Type MediaType `json:"type"`
|
Type MediaType `json:"type"`
|
||||||
Format MediaFormat `json:"format"`
|
Format MediaFormat `json:"format"`
|
||||||
@ -113,10 +115,12 @@ const (
|
|||||||
MediaSourcePictureBook MediaSource = "PICTURE_BOOK"
|
MediaSourcePictureBook MediaSource = "PICTURE_BOOK"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MediaTrailerID string
|
||||||
|
|
||||||
type MediaTrailer struct {
|
type MediaTrailer struct {
|
||||||
ID string `json:"id"`
|
ID MediaTrailerID `json:"id"`
|
||||||
Site string `json:"site"`
|
Site string `json:"site"`
|
||||||
Thumbnail string `json:"thumbnail"`
|
Thumbnail string `json:"thumbnail"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type MediaCoverImage struct {
|
type MediaCoverImage struct {
|
||||||
@ -126,8 +130,10 @@ type MediaCoverImage struct {
|
|||||||
Color string `json:"color"`
|
Color string `json:"color"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MediaTagID int
|
||||||
|
|
||||||
type MediaTag struct {
|
type MediaTag struct {
|
||||||
ID int `json:"id"`
|
MediaTagID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Category string `json:"category"`
|
Category string `json:"category"`
|
||||||
@ -173,10 +179,12 @@ func (d Seconds) Duration() time.Duration {
|
|||||||
return time.Duration(d) * time.Second
|
return time.Duration(d) * time.Second
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MediaListID int
|
||||||
|
|
||||||
type MediaList struct {
|
type MediaList struct {
|
||||||
ID int `json:"id"`
|
ID MediaListID `json:"id"`
|
||||||
UserID int `json:"userId"`
|
UserID int `json:"userId"`
|
||||||
MediaID int `json:"mediaId"`
|
MediaID MediaID `json:"mediaId"`
|
||||||
Status MediaListStatus `json:"status"`
|
Status MediaListStatus `json:"status"`
|
||||||
Score float64 `json:"score"`
|
Score float64 `json:"score"`
|
||||||
Progress int `json:"progress"`
|
Progress int `json:"progress"`
|
||||||
@ -197,18 +205,20 @@ type MediaList struct {
|
|||||||
type MediaListStatus string
|
type MediaListStatus string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MediaListStatusCurrent MediaSource = "CURRENT"
|
MediaListStatusCurrent MediaListStatus = "CURRENT"
|
||||||
MediaListStatusPlanning MediaSource = "PLANNING"
|
MediaListStatusPlanning MediaListStatus = "PLANNING"
|
||||||
MediaListStatusCompleted MediaSource = "COMPLETED"
|
MediaListStatusCompleted MediaListStatus = "COMPLETED"
|
||||||
MediaListStatusDropped MediaSource = "DROPPED"
|
MediaListStatusDropped MediaListStatus = "DROPPED"
|
||||||
MediaListStatusPaused MediaSource = "PAUSED"
|
MediaListStatusPaused MediaListStatus = "PAUSED"
|
||||||
MediaListStatusRepeating MediaSource = "REPEATING"
|
MediaListStatusRepeating MediaListStatus = "REPEATING"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type AiringScheduleID int
|
||||||
|
|
||||||
type AiringSchedule struct {
|
type AiringSchedule struct {
|
||||||
ID int `json:"id"`
|
ID AiringScheduleID `json:"id"`
|
||||||
MediaID int `json:"mediaId"`
|
MediaID MediaID `json:"mediaId"`
|
||||||
AiringAt UnixTime `json:"airingAt"`
|
AiringAt UnixTime `json:"airingAt"`
|
||||||
TimeUntilAiring Seconds `json:"timeUntilAiring"`
|
TimeUntilAiring Seconds `json:"timeUntilAiring"`
|
||||||
Episode int `json:"episode"`
|
Episode int `json:"episode"`
|
||||||
}
|
}
|
||||||
|
2
user.go
2
user.go
@ -44,7 +44,7 @@ func (api *Api) GetUserByID(id int) (*User, error) {
|
|||||||
return resp.Data.User, nil
|
return resp.Data.User, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *Api) SearchUsers(ctx context.Context, search string, onError func(error)) Cursor[User] {
|
func (api *Api) SearchUsers(ctx context.Context, search string, onError func(error)) *Cursor[User] {
|
||||||
vars := map[string]interface{}{"search": search}
|
vars := map[string]interface{}{"search": search}
|
||||||
resp := responseObj[*page[User]]{}
|
resp := responseObj[*page[User]]{}
|
||||||
return requestPaged(api, ctx, searchUsersQuery, vars, &resp, onError)
|
return requestPaged(api, ctx, searchUsersQuery, vars, &resp, onError)
|
||||||
|
2
utils.go
2
utils.go
@ -18,7 +18,7 @@ func addValue2InterfaceMap[K, T comparable](m map[K]interface{}, key K, value T)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func addSlice2InterfaceMap[K, T comparable](m map[K]interface{}, key K, value []T) {
|
func addSlice2InterfaceMap[K, T comparable](m map[K]interface{}, key K, value []T) {
|
||||||
if value != nil && len(value) > 0 {
|
if len(value) > 0 {
|
||||||
m[key] = value
|
m[key] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user