2024-07-30 12:57:08 -04:00
|
|
|
package main
|
2024-07-30 12:59:06 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SimklQuery(body interface{}, login bool) (json.RawMessage, string) {
|
|
|
|
reader, _ := json.Marshal(body)
|
|
|
|
response, err := http.NewRequest("POST", "https://graphql.anilist.co", bytes.NewBuffer(reader))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed at response, %s\n", err)
|
|
|
|
}
|
|
|
|
if login && (SimklJWT{}) != simklJwt {
|
|
|
|
response.Header.Add("Authorization", "Bearer "+simklJwt.AccessToken)
|
|
|
|
} else if login {
|
|
|
|
return nil, "Please login to anilist to make this request"
|
|
|
|
}
|
|
|
|
response.Header.Add("Content-Type", "application/json")
|
|
|
|
response.Header.Add("Accept", "application/json")
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
res, reserr := client.Do(response)
|
|
|
|
if reserr != nil {
|
|
|
|
log.Printf("Failed at res, %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
returnedBody, err := io.ReadAll(res.Body)
|
|
|
|
|
|
|
|
return returnedBody, ""
|
|
|
|
}
|