Skip to content

Commit

Permalink
fix: update PaperListParams to match Python
Browse files Browse the repository at this point in the history
  • Loading branch information
kkweon committed Jun 5, 2021
1 parent 70ac12b commit 2b45ea8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 14 deletions.
41 changes: 28 additions & 13 deletions paper_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package paperswithcode_go
import (
"encoding/json"
"fmt"
"strings"

"github.com/codingpot/paperswithcode-go/v2/models"
"net/url"
)

// PaperList returns multiple papers.
func (c *Client) PaperList(params PaperListParams) (*models.PaperList, error) {
papersListURL := c.baseURL + "/papers?" + params.build()
papersListURL := c.baseURL + "/papers?" + params.Build()

response, err := c.httpClient.Get(papersListURL)
if err != nil {
Expand All @@ -28,27 +29,41 @@ func (c *Client) PaperList(params PaperListParams) (*models.PaperList, error) {

// PaperListParams is the parameter for PaperList method.
type PaperListParams struct {
// Query to search papers (default: "")
// Q to search papers (default: "")
// If empty, it returns all papers.
Query string
Q string
ArxivID string
Title string
Abstract string
// Page is the number of page to search (default: 1)
Page int
// Limit returns how many papers are returned in a single response.
Limit int
// ItemsPerPage returns how many papers are returned in a single response.
ItemsPerPage int
}

func (p PaperListParams) Build() string {
var b strings.Builder
b.WriteString(fmt.Sprintf("page=%d&items_per_page=%d", p.Page, p.ItemsPerPage))

addParamsIfValid(&b, "q", p.Q)
addParamsIfValid(&b, "arxiv_id", p.ArxivID)
addParamsIfValid(&b, "title", p.Title)
addParamsIfValid(&b, "abstract", p.Abstract)

return b.String()
}

func (p PaperListParams) build() string {
if p.Query == "" {
return fmt.Sprintf("items_per_page=%d&page=%d", p.Limit, p.Page)
func addParamsIfValid(b *strings.Builder, key string, value string) {
if value != "" {
b.WriteString(fmt.Sprintf("&%s=%s", key, value))
}
return fmt.Sprintf("q=%s&items_per_page=%d&page=%d", url.QueryEscape(p.Query), p.Limit, p.Page)
}

// PaperListParamsDefault returns the default PaperListParams.
func PaperListParamsDefault() PaperListParams {
return PaperListParams{
Query: "",
Page: 1,
Limit: 50,
Q: "",
Page: 1,
ItemsPerPage: 50,
}
}
55 changes: 54 additions & 1 deletion paper_list_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,65 @@
package paperswithcode_go

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestClient_PaperList(t *testing.T) {
client := NewClient(WithAPIToken(apiToken))
_, err := client.PaperList(PaperListParamsDefault())
assert.NoError(t, err)
}

func TestPaperListParams_Build(t *testing.T) {
type fields struct {
Q string
ArxivID string
Title string
Abstract string
Page int
ItemsPerPage int
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Q is given, it passes Q",
fields: fields{
Q: "wow",
Page: 1,
ItemsPerPage: 50,
},
want: "page=1&items_per_page=50&q=wow",
},
{
name: "Q is not given, it shouldn't add Q param",
fields: fields{
Page: 1,
ItemsPerPage: 50,
},
want: "page=1&items_per_page=50",
},
{
name: "Default Param is valid",
fields: fields(PaperListParamsDefault()),
want: "page=1&items_per_page=50",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := PaperListParams{
Q: tt.fields.Q,
ArxivID: tt.fields.ArxivID,
Title: tt.fields.Title,
Abstract: tt.fields.Abstract,
Page: tt.fields.Page,
ItemsPerPage: tt.fields.ItemsPerPage,
}
assert.Equal(t, tt.want, p.Build())
})
}
}

0 comments on commit 2b45ea8

Please sign in to comment.