@@ -3,13 +3,14 @@ package paperswithcode_go
33import (
44 "encoding/json"
55 "fmt"
6+ "strings"
7+
68 "github.com/codingpot/paperswithcode-go/v2/models"
7- "net/url"
89)
910
1011// PaperList returns multiple papers.
1112func (c * Client ) PaperList (params PaperListParams ) (* models.PaperList , error ) {
12- papersListURL := c .baseURL + "/papers?" + params .build ()
13+ papersListURL := c .baseURL + "/papers?" + params .Build ()
1314
1415 response , err := c .httpClient .Get (papersListURL )
1516 if err != nil {
@@ -28,27 +29,41 @@ func (c *Client) PaperList(params PaperListParams) (*models.PaperList, error) {
2829
2930// PaperListParams is the parameter for PaperList method.
3031type PaperListParams struct {
31- // Query to search papers (default: "")
32+ // Q to search papers (default: "")
3233 // If empty, it returns all papers.
33- Query string
34+ Q string
35+ ArxivID string
36+ Title string
37+ Abstract string
3438 // Page is the number of page to search (default: 1)
3539 Page int
36- // Limit returns how many papers are returned in a single response.
37- Limit int
40+ // ItemsPerPage returns how many papers are returned in a single response.
41+ ItemsPerPage int
42+ }
43+
44+ func (p PaperListParams ) Build () string {
45+ var b strings.Builder
46+ b .WriteString (fmt .Sprintf ("page=%d&items_per_page=%d" , p .Page , p .ItemsPerPage ))
47+
48+ addParamsIfValid (& b , "q" , p .Q )
49+ addParamsIfValid (& b , "arxiv_id" , p .ArxivID )
50+ addParamsIfValid (& b , "title" , p .Title )
51+ addParamsIfValid (& b , "abstract" , p .Abstract )
52+
53+ return b .String ()
3854}
3955
40- func ( p PaperListParams ) build () string {
41- if p . Query = = "" {
42- return fmt .Sprintf ("items_per_page=%d&page=%d " , p . Limit , p . Page )
56+ func addParamsIfValid ( b * strings. Builder , key string , value string ) {
57+ if value ! = "" {
58+ b . WriteString ( fmt .Sprintf ("&%s=%s " , key , value ) )
4359 }
44- return fmt .Sprintf ("q=%s&items_per_page=%d&page=%d" , url .QueryEscape (p .Query ), p .Limit , p .Page )
4560}
4661
4762// PaperListParamsDefault returns the default PaperListParams.
4863func PaperListParamsDefault () PaperListParams {
4964 return PaperListParams {
50- Query : "" ,
51- Page : 1 ,
52- Limit : 50 ,
65+ Q : "" ,
66+ Page : 1 ,
67+ ItemsPerPage : 50 ,
5368 }
5469}
0 commit comments