Files
PixivFE/core/http/request.go
2024-02-18 02:42:31 +00:00

91 lines
1.7 KiB
Go

package core
import (
"errors"
"fmt"
"io"
"net/http"
core "codeberg.org/vnpower/pixivfe/v2/core/config"
"github.com/tidwall/gjson"
)
type HttpResponse struct {
Ok bool
StatusCode int
Body string
Message string
}
func WebAPIRequest(URL, token string) HttpResponse {
req, _ := http.NewRequest("GET", URL, nil)
req.Header.Add("User-Agent", core.GlobalServerConfig.UserAgent)
req.Header.Add("Accept-Language", core.GlobalServerConfig.AcceptLanguage)
if token == "" {
req.AddCookie(&http.Cookie{
Name: "PHPSESSID",
Value: core.GetRandomDefaultToken(),
})
} else {
req.AddCookie(&http.Cookie{
Name: "PHPSESSID",
Value: token,
})
}
// Make the request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return HttpResponse{
Ok: false,
StatusCode: 0,
Body: "",
Message: fmt.Sprintf("Failed to create a request to %s\n.", URL),
}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return HttpResponse{
Ok: false,
StatusCode: 0,
Body: "",
Message: fmt.Sprintln("Failed to parse request data."),
}
}
return HttpResponse{
Ok: true,
StatusCode: resp.StatusCode,
Body: string(body),
Message: "",
}
}
func UnwrapWebAPIRequest(URL, token string) (string, error) {
resp := WebAPIRequest(URL, token)
if !resp.Ok {
return "", errors.New(resp.Message)
}
if !gjson.Valid(resp.Body) {
return "", fmt.Errorf("invalid json: %v", resp.Body)
}
err := gjson.Get(resp.Body, "error")
if !err.Exists() {
return "", errors.New("incompatible request body")
}
if err.Bool() {
return "", errors.New(gjson.Get(resp.Body, "message").String())
}
return gjson.Get(resp.Body, "body").String(), nil
}