anilist/api.go

114 lines
2.0 KiB
Go
Raw Normal View History

2022-02-03 17:23:29 +01:00
package anilist
import (
"bytes"
2022-02-06 10:39:04 +01:00
"context"
2022-02-03 17:23:29 +01:00
"encoding/json"
"net/http"
)
type Api struct {
AccessToken string
}
func NewApi(accessToken string) *Api {
return &Api{AccessToken: accessToken}
}
type queryObj struct {
Query string `json:"query"`
Vars interface{} `json:"variables"`
}
type responseObj[T any] struct {
Data T `json:"data"`
}
func request[T any](api *Api, query string, vars map[string]interface{}, respObj *responseObj[T]) error {
q := &queryObj{
Query: query,
Vars: vars,
}
queryData, err := json.Marshal(q)
if err != nil {
return err
}
req, err := http.NewRequest("POST", "https://graphql.anilist.co/", bytes.NewReader(queryData))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+api.AccessToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
//data, _ := ioutil.ReadAll(resp.Body)
//fmt.Println(string(data))
dec := json.NewDecoder(resp.Body)
err = dec.Decode(respObj)
if err != nil {
return err
}
return nil
}
2022-02-06 10:39:04 +01:00
func requestPaged[R any](api *Api, ctx context.Context, query string, vars map[string]interface{}, respObj *responseObj[*page[R]], onError func(error)) Cursor[R] {
2022-02-03 17:23:29 +01:00
resp := responseObj[struct {
Page *page[R] `json:"Page"`
}]{}
2022-02-06 10:39:04 +01:00
var cancelFunc context.CancelFunc
ctx, cancelFunc = context.WithCancel(ctx)
2022-02-03 17:23:29 +01:00
out := make(chan *R, 50)
go func() {
defer close(out)
2022-02-06 10:39:04 +01:00
defer cancelFunc()
2022-02-03 17:23:29 +01:00
vars["page"] = 0
for {
if p, ok := vars["page"].(int); ok {
vars["page"] = p + 1
}
err := request(api, query, vars, &resp)
if err != nil {
if onError != nil {
onError(err)
}
return
}
for _, value := range resp.Data.Page.Data() {
value := value
2022-02-06 10:39:04 +01:00
select {
case out <- &value:
case <-ctx.Done():
return
}
2022-02-03 17:23:29 +01:00
}
if resp.Data.Page.PageInfo.CurrentPage == resp.Data.Page.PageInfo.LastPage {
return
}
}
}()
2022-02-06 10:39:04 +01:00
return Cursor[R]{
Chan: out,
ctx: ctx,
cancelFunc: cancelFunc,
}
2022-02-03 17:23:29 +01:00
}