Compare commits

...

7 Commits
v1.4.0 ... main

Author SHA1 Message Date
3b208aeeaf Update go.mod 2024-12-16 19:49:05 +01:00
milarin
bfc0e5c417 removed lint warning 2023-10-09 13:49:37 +02:00
milarin
e6183ddfa3 handle error messages 2023-10-09 13:48:09 +02:00
Timon Ringwald
1591816684 fixed nil pointer when no API entry found 2022-11-14 18:07:54 +01:00
Timon Ringwald
8ce29414f4 fixed foreign key data types 2022-08-03 23:12:16 +02:00
Timon Ringwald
faa5474d22 added id types 2022-08-03 23:08:50 +02:00
Timon Ringwald
de98e6f444 moved to git.milar.in 2022-08-03 21:17:48 +02:00
7 changed files with 65 additions and 14 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
api_test.go

8
api.go
View File

@ -49,6 +49,10 @@ func request[T any](api *Api, query string, vars map[string]interface{}, respObj
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return handleError(resp)
}
//data, _ := ioutil.ReadAll(resp.Body)
//fmt.Println(string(data))
@ -90,6 +94,10 @@ func requestPaged[R any](api *Api, ctx context.Context, query string, vars map[s
return
}
if resp.Data.Page == nil {
return
}
for _, value := range resp.Data.Page.Data() {
value := value
select {

32
api_error.go Normal file
View 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)
}

2
go.mod
View File

@ -1,3 +1,3 @@
module git.tordarus.net/Tordarus/anilist
module git.tordarus.net/tordarus/anilist
go 1.18

0
go.sum
View File

View File

@ -9,8 +9,10 @@ type User struct {
Name string `json:"name,omitempty"`
}
type MediaID int
type Media struct {
ID int `json:"id"`
ID MediaID `json:"id"`
Title MediaTitle `json:"title"`
Type MediaType `json:"type"`
Format MediaFormat `json:"format"`
@ -113,10 +115,12 @@ const (
MediaSourcePictureBook MediaSource = "PICTURE_BOOK"
)
type MediaTrailerID string
type MediaTrailer struct {
ID string `json:"id"`
Site string `json:"site"`
Thumbnail string `json:"thumbnail"`
ID MediaTrailerID `json:"id"`
Site string `json:"site"`
Thumbnail string `json:"thumbnail"`
}
type MediaCoverImage struct {
@ -126,8 +130,10 @@ type MediaCoverImage struct {
Color string `json:"color"`
}
type MediaTagID int
type MediaTag struct {
ID int `json:"id"`
MediaTagID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Category string `json:"category"`
@ -173,10 +179,12 @@ func (d Seconds) Duration() time.Duration {
return time.Duration(d) * time.Second
}
type MediaListID int
type MediaList struct {
ID int `json:"id"`
ID MediaListID `json:"id"`
UserID int `json:"userId"`
MediaID int `json:"mediaId"`
MediaID MediaID `json:"mediaId"`
Status MediaListStatus `json:"status"`
Score float64 `json:"score"`
Progress int `json:"progress"`
@ -205,10 +213,12 @@ const (
MediaListStatusRepeating MediaListStatus = "REPEATING"
)
type AiringScheduleID int
type AiringSchedule struct {
ID int `json:"id"`
MediaID int `json:"mediaId"`
AiringAt UnixTime `json:"airingAt"`
TimeUntilAiring Seconds `json:"timeUntilAiring"`
Episode int `json:"episode"`
ID AiringScheduleID `json:"id"`
MediaID MediaID `json:"mediaId"`
AiringAt UnixTime `json:"airingAt"`
TimeUntilAiring Seconds `json:"timeUntilAiring"`
Episode int `json:"episode"`
}

View File

@ -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) {
if value != nil && len(value) > 0 {
if len(value) > 0 {
m[key] = value
}
}