...

Source file src/jschan/app/overboard.go

Documentation: jschan/app

     1  package jschan
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"jschan/app/models"
     7  	"net/http"
     8  	"net/url"
     9  	"strings"
    10  )
    11  
    12  type GetOverboardOptions struct {
    13  	AddBoards      []string
    14  	RemoveBoards   []string
    15  	IncludeDefault bool
    16  }
    17  
    18  type GetOverboardResponse struct {
    19  	Threads []models.Post `json:"threads"`
    20  }
    21  
    22  func getOverboardQuery(options *GetOverboardOptions) (*url.Values, error) {
    23  	include_default := false
    24  	add := []string{}
    25  	rem := []string{}
    26  	if options != nil {
    27  		add = options.AddBoards
    28  		rem = options.RemoveBoards
    29  	}
    30  	query := url.Values{}
    31  	query.Set("add", strings.Join(add, ","))
    32  	query.Set("rem", strings.Join(rem, ","))
    33  	if include_default {
    34  		query.Set("include_default", "true")
    35  	}
    36  	return &query, nil
    37  }
    38  
    39  func (c *Client) GetOverboardIndex(ctx context.Context, options *GetOverboardOptions) (*GetOverboardResponse, error) {
    40  
    41  	query, err := getOverboardQuery(options)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	url := fmt.Sprintf("%s/overboard.json?%s", c.BaseURL, query.Encode())
    47  
    48  	req, err := http.NewRequest(http.MethodGet, url, nil)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	req = req.WithContext(ctx)
    54  
    55  	res := GetOverboardResponse{}
    56  	if err := c.sendRequest(req, &res, nil); err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	return &res, nil
    61  
    62  }
    63  
    64  func (c *Client) GetOverboardCatalog(ctx context.Context, options *GetOverboardOptions) (*GetOverboardResponse, error) {
    65  
    66  	query, err := getOverboardQuery(options)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	url := fmt.Sprintf("%s/catalog.json?%s", c.BaseURL, query.Encode())
    72  
    73  	req, err := http.NewRequest(http.MethodGet, url, nil)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	req = req.WithContext(ctx)
    79  
    80  	res := GetOverboardResponse{}
    81  	if err := c.sendRequest(req, &res, nil); err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	return &res, nil
    86  
    87  }
    88  

View as plain text