Skip to content

Commit

Permalink
Merge pull request #13 from codingpot/feat-add-method-list
Browse files Browse the repository at this point in the history
feat: add c.MethodList()
  • Loading branch information
kkweon authored Jul 1, 2021
2 parents 87730c8 + dbc8958 commit 725a9d7
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
36 changes: 36 additions & 0 deletions method_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package paperswithcode_go

import (
"encoding/json"
"fmt"
"github.com/codingpot/paperswithcode-go/v2/models"
)

type MethodListParams struct {
// Page (default: 1)
Page int
// ItemsPerPage (default: 50)
ItemsPerPage int
}

func (m MethodListParams) String() string {
return fmt.Sprintf("page=%d&items_per_page=%d", m.Page, m.ItemsPerPage)
}

func (c *Client) MethodList(params MethodListParams) (*models.MethodList, error) {
url := c.baseURL + "/methods?" + params.String()

response, err := c.httpClient.Get(url)
if err != nil {
return nil, err
}

var listResult models.MethodList

err = json.NewDecoder(response.Body).Decode(&listResult)
if err != nil {
return nil, err
}

return &listResult, nil
}
64 changes: 64 additions & 0 deletions method_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package paperswithcode_go

import (
"github.com/codingpot/paperswithcode-go/v2/models"
"github.com/stretchr/testify/assert"
"testing"
)

func TestClient_MethodList(t *testing.T) {
tests := []struct {
name string
params MethodListParams
want *models.MethodList
wantErr bool
}{
{
name: "MethodList returns methods",
params: MethodListParams{
Page: 1,
ItemsPerPage: 2,
},
want: &models.MethodList{
Count: 1129,
Next: toPtr("https://paperswithcode.com/api/v1/methods/?items_per_page=2&page=2"),
Previous: nil,
Results: []*models.Method{
{
ID: "1cycle",
Name: "1cycle",
FullName: "1cycle learning rate scheduling policy",
Description: "",
Paper: toPtr("a-disciplined-approach-to-neural-network"),
},
{
ID: "1d-cnn",
Name: "1D CNN",
FullName: "1-Dimensional Convolutional Neural Networks",
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.",
Paper: toPtr("convolutional-neural-network-and-rule-based"),
},
},
},
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewClient()
got, err := c.MethodList(tt.params)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}

assert.Equal(t, tt.want, got)
})
}
}

func toPtr(s string) *string {
return &s
}

0 comments on commit 725a9d7

Please sign in to comment.