Change url query string building to more proper method with url.Values{} and will also handle multipel values properly e.g. sites

Globalmanage board list (no auth yet)
master
Thomas Lynch 2 years ago
parent d9450400d3
commit d5749b982c
  1. 103
      app/boardlist.go
  2. 5
      example.go

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"net/url"
)
type LastPostTimestamp struct {
@ -30,23 +31,23 @@ type Boards struct {
Webring bool `json:"webring"`
Ppd int `json:"ppd,omitempty"`
}
type GetBoardsOptions struct {
type GetBoardsResponse struct {
Boards []Boards `json:"boards"`
Page int `json:"page"`
MaxPage int `json:"maxPage"`
}
type GetBoardsPublicOptions struct {
Search string `json:"search"`
Sort string `json:"sort"`
SortDirection string `json:"direction"`
Page int `json:"page"`
LocalFirst bool `json:"local_first"`
Sites []string `json:"sites"`
Page int `json:"page"`
}
func (c *Client) GetBoardsPublic(ctx context.Context, options *GetBoardsPublicOptions) (*GetBoardsResponse, error) {
type GetBoardsResponse struct {
Boards []Boards `json:"boards"`
Page int `json:"page"`
MaxPage int `json:"maxPage"`
}
func (c *Client) GetBoards(ctx context.Context, options *GetBoardsOptions) (*GetBoardsResponse, error) {
page := 1
search := ""
sort := "popularity"
@ -61,16 +62,96 @@ func (c *Client) GetBoards(ctx context.Context, options *GetBoardsOptions) (*Get
sites = options.Sites
page = options.Page
}
url := fmt.Sprintf("%s/boards.json?search=%s&page=%d&sort=%s&direction=%s&local_first=%t&sites=%s", c.BaseURL, search, page, sort, direction, local_first, sites)
// fmt.Printf("GETting %s\n", url)
query := url.Values{}
query.Set("search", search)
query.Set("page", fmt.Sprintf("%d", page))
query.Set("sort", sort)
query.Set("direction", direction)
if local_first {
query.Set("local_first", "true")
}
for _, site := range sites {
query.Add("sites", site)
}
url := fmt.Sprintf("%s/boards.json?%s", c.BaseURL, query.Encode())
req, err := http.NewRequest("GET", url, nil);
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
res := GetBoardsResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
type GetBoardsGlobalmanageOptions struct {
Search string `json:"search"`
Sort string `json:"sort"`
SortDirection string `json:"direction"`
Page int `json:"page"`
FilterUnlisted bool `json:"filter_unlisted"`
FilterSfw bool `json:"filter_sfw"`
FilterAbandoned bool `json:"filter_abandoned"`
}
func (c *Client) GetBoardsGlobalmanage(ctx context.Context, options *GetBoardsGlobalmanageOptions) (*GetBoardsResponse, error) {
page := 1
search := ""
sort := "popularity"
direction := "desc"
filter_unlisted := false
filter_sfw := false
filter_abandoned := false
if options != nil {
search = options.Search
sort = options.Sort
direction = options.SortDirection
filter_unlisted = options.FilterUnlisted
filter_sfw = options.FilterSfw
filter_abandoned = options.FilterAbandoned
page = options.Page
}
query := url.Values{}
query.Set("search", search)
query.Set("page", fmt.Sprintf("%d", page))
query.Set("sort", sort)
query.Set("direction", direction)
if filter_unlisted {
query.Set("filter_unlisted", "true")
}
if filter_sfw {
query.Set("filter_sfw", "true")
}
if filter_abandoned {
query.Set("filter_abandoned", "true")
}
url := fmt.Sprintf("%s/boards.json?%s", c.BaseURL, query.Encode())
req, err := http.NewRequest("GET", url, nil);
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
res := GetBoardsResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}

@ -9,10 +9,11 @@ import (
func main() {
jschanClient := jschan.NewClient("https://ptchan.org")
ctx := context.Background()
options := &jschan.GetBoardsOptions{
options := &jschan.GetBoardsPublicOptions{
Search: "internet",
Sites: []string{"Test", "TEst2"},
}
res, err := jschanClient.GetBoards(ctx, options)
res, err := jschanClient.GetBoardsPublic(ctx, options)
if err != nil {
fmt.Println("an error occurred")
return

Loading…
Cancel
Save