37 lines
873 B
Go
37 lines
873 B
Go
package main
|
|
|
|
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, ""
|
|
}
|