Skip to content

Commit 725a9d7

Browse files
authored
Merge pull request #13 from codingpot/feat-add-method-list
feat: add c.MethodList()
2 parents 87730c8 + dbc8958 commit 725a9d7

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

method_list.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package paperswithcode_go
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/codingpot/paperswithcode-go/v2/models"
7+
)
8+
9+
type MethodListParams struct {
10+
// Page (default: 1)
11+
Page int
12+
// ItemsPerPage (default: 50)
13+
ItemsPerPage int
14+
}
15+
16+
func (m MethodListParams) String() string {
17+
return fmt.Sprintf("page=%d&items_per_page=%d", m.Page, m.ItemsPerPage)
18+
}
19+
20+
func (c *Client) MethodList(params MethodListParams) (*models.MethodList, error) {
21+
url := c.baseURL + "/methods?" + params.String()
22+
23+
response, err := c.httpClient.Get(url)
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
var listResult models.MethodList
29+
30+
err = json.NewDecoder(response.Body).Decode(&listResult)
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
return &listResult, nil
36+
}

method_list_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package paperswithcode_go
2+
3+
import (
4+
"github.com/codingpot/paperswithcode-go/v2/models"
5+
"github.com/stretchr/testify/assert"
6+
"testing"
7+
)
8+
9+
func TestClient_MethodList(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
params MethodListParams
13+
want *models.MethodList
14+
wantErr bool
15+
}{
16+
{
17+
name: "MethodList returns methods",
18+
params: MethodListParams{
19+
Page: 1,
20+
ItemsPerPage: 2,
21+
},
22+
want: &models.MethodList{
23+
Count: 1129,
24+
Next: toPtr("https://paperswithcode.com/api/v1/methods/?items_per_page=2&page=2"),
25+
Previous: nil,
26+
Results: []*models.Method{
27+
{
28+
ID: "1cycle",
29+
Name: "1cycle",
30+
FullName: "1cycle learning rate scheduling policy",
31+
Description: "",
32+
Paper: toPtr("a-disciplined-approach-to-neural-network"),
33+
},
34+
{
35+
ID: "1d-cnn",
36+
Name: "1D CNN",
37+
FullName: "1-Dimensional Convolutional Neural Networks",
38+
Description: "1D Convolutional Neural Networks are similar to well known and more established 2D Convolutional Neural Networks. 1D Convolutional Neural Networks are used mainly used on text and 1D signals.",
39+
Paper: toPtr("convolutional-neural-network-and-rule-based"),
40+
},
41+
},
42+
},
43+
wantErr: false,
44+
},
45+
}
46+
47+
for _, tt := range tests {
48+
t.Run(tt.name, func(t *testing.T) {
49+
c := NewClient()
50+
got, err := c.MethodList(tt.params)
51+
if tt.wantErr {
52+
assert.Error(t, err)
53+
} else {
54+
assert.NoError(t, err)
55+
}
56+
57+
assert.Equal(t, tt.want, got)
58+
})
59+
}
60+
}
61+
62+
func toPtr(s string) *string {
63+
return &s
64+
}

0 commit comments

Comments
 (0)