**WORK IN PROGRESS** golang API client for interacting with the jschan imageboard API.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.4 KiB

2 years ago
package jschan
import (
"encoding/json"
"errors"
2 years ago
"fmt"
"io/ioutil"
2 years ago
"net/http"
"time"
2 years ago
)
type Client struct {
2 years ago
BaseURL string
2 years ago
SessionCookie string
2 years ago
CsrfToken string
HTTPClient *http.Client
2 years ago
}
func NewClient(baseURL string) *Client {
return &Client{
2 years ago
BaseURL: baseURL,
2 years ago
SessionCookie: "",
2 years ago
CsrfToken: "",
2 years ago
HTTPClient: &http.Client{
Timeout: time.Minute,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
2 years ago
},
}
}
type DynamicResponse struct {
2 years ago
Title string `json:"title"`
Message string `json:"message"`
2 years ago
Redirect string `json:"redirect,omitempty"`
}
type ReturnHeaders struct {
*http.Header
}
func cookieHeader(rawCookies string) []*http.Cookie {
header := http.Header{}
header.Add("Cookie", rawCookies)
req := http.Request{Header: header}
return req.Cookies()
}
func (c *Client) sendRequest(req *http.Request, v interface{}, h *ReturnHeaders) error {
if req.Header.Get("Content-Type") == "" {
if req.Method == http.MethodGet {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
} else {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
}
2 years ago
req.Header.Set("Accept", "application/json; charset=utf-8")
req.Header.Set("X-Using-XHR", "true")
req.Header.Set("Referer", c.BaseURL)
2 years ago
if c.SessionCookie != "" {
req.Header.Set("Cookie", fmt.Sprintf("connect.sid=%s", c.SessionCookie))
}
if c.CsrfToken != "" {
req.Header.Set("Csrf-Token", c.CsrfToken)
2 years ago
}
2 years ago
fmt.Printf("%s %s\n", req.Method, req.URL)
2 years ago
res, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
2 years ago
if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest {
var errRes DynamicResponse
if err = json.NewDecoder(res.Body).Decode(&errRes); err != nil {
return err
2 years ago
}
return errors.New(errRes.Message)
2 years ago
}
h = &ReturnHeaders{
&res.Header,
}
setCookieValue := h.Get("Set-Cookie")
if setCookieValue != "" {
parsedSetCookie := cookieHeader(setCookieValue)
for _, parsedCookie := range parsedSetCookie {
if parsedCookie.Name == "connect.sid" {
c.SessionCookie = parsedCookie.Value
}
}
}
if v != nil {
fullResponse := v
err := json.NewDecoder(res.Body).Decode(&fullResponse)
if err != nil {
body, err2 := ioutil.ReadAll(res.Body)
if err2 != nil {
return err
}
err3 := json.Unmarshal(body, &fullResponse)
if err3 != nil {
return err
}
}
2 years ago
}
2 years ago
2 years ago
return nil
}