Skip to content

Commit 95e6a74

Browse files
committed
Enable PaperList() method as a reference
- Keep the same names in Python (`paper_list` -> `PaperList`) - Create a correct type for `PaperListResult` - Add YYYY-MM-DD date type
1 parent 495a22e commit 95e6a74

8 files changed

+1097
-43
lines changed

client.go

+45-42
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"github.com/codingpot/paperswithcode-go/models"
89
"net/http"
910
"net/url"
1011
"time"
@@ -177,45 +178,47 @@ func (c *Client) sendRequest(req *http.Request, v interface{}) error {
177178
return nil
178179
}
179180

180-
/*
181-
GetPapers API is currently broken. need to wait until it gets fixed
182-
*/
183-
184-
// type PaperList struct {
185-
// Count int `json:"count"`
186-
// NextPage string `json:"next,omitempty"`
187-
// PrePage string `json:"previous,omitempty"`
188-
// Results []struct {
189-
// ID string `json:"id"`
190-
// ArxivID []string `json:"arxiv_id,omitempty"`
191-
// NipsID []string `json:"nips_id,omitempty"`
192-
// URLAbs string `json:"url_abs"`
193-
// URLPdf string `json:"url_pdf"`
194-
// Title string `json:"title"`
195-
// Abstract string `json:"abstract"`
196-
// Authors []string `json:"authors"`
197-
// Published string `json:"published"`
198-
// Conference []string `json:"conference,omitempty"`
199-
// ConferenceURLAbs []string `json:"conference_url_abs,omitempty"`
200-
// ConferenceURLPdf []string `json:"conference_url_pdf,omitempty"`
201-
// Proceeding []string `json:"proceeding,omitempty"`
202-
// } `json:"results"`
203-
// }
204-
205-
// func (c *Client) GetPapers(ctx context.Context, query string, page int, limit int) (*PaperList, error) {
206-
// url := fmt.Sprintf("%s/papers?q=%s&items_per_page=%d&page=%d", c.BaseURL, url.QueryEscape(query), limit, page)
207-
// req, err := http.NewRequest("GET", url, nil)
208-
209-
// if err != nil {
210-
// return nil, err
211-
// }
212-
213-
// req = req.WithContext(ctx)
214-
215-
// res := PaperList{}
216-
// if err := c.sendRequest(req, &res); err != nil {
217-
// return nil, err
218-
// }
219-
220-
// return &res, nil
221-
// }
181+
type PaperListParams struct {
182+
Query string
183+
Page int
184+
Limit int
185+
}
186+
187+
func (p PaperListParams) Build() string {
188+
if p.Query == "" {
189+
return fmt.Sprintf("items_per_page=%d&page=%d", p.Limit, p.Page)
190+
}
191+
return fmt.Sprintf("q=%s&items_per_page=%d&page=%d", url.QueryEscape(p.Query), p.Limit, p.Page)
192+
}
193+
194+
func (c *Client) PaperList(params PaperListParams) (*models.PaperListResult, error) {
195+
papersListURL := c.BaseURL + "/papers?" + params.Build()
196+
197+
request, err := http.NewRequest(http.MethodGet, papersListURL /*body=*/, nil)
198+
if err != nil {
199+
return nil, err
200+
}
201+
request.Header.Set("Authorization", "Token "+c.apiToken)
202+
203+
response, err := c.HTTPClient.Get(papersListURL)
204+
if err != nil {
205+
return nil, err
206+
}
207+
208+
var paperListResult models.PaperListResult
209+
210+
err = json.NewDecoder(response.Body).Decode(&paperListResult)
211+
if err != nil {
212+
return nil, err
213+
}
214+
215+
return &paperListResult, nil
216+
}
217+
218+
func PaperListParamsDefault() PaperListParams {
219+
return PaperListParams{
220+
Query: "",
221+
Page: 1,
222+
Limit: 50,
223+
}
224+
}

client_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package paperswithcode_go
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
var apiToken string
11+
12+
func init() {
13+
var ok bool
14+
apiToken, ok = os.LookupEnv("PAPERSWITHCODE_API_TOKEN")
15+
16+
if !ok {
17+
panic("expected PAPERSWITHCODE_API_TOKEN environment variable")
18+
}
19+
}
20+
21+
func TestClient_PaperList(t *testing.T) {
22+
client := NewClient(WithAPIToken(apiToken))
23+
_, err := client.PaperList(PaperListParamsDefault())
24+
assert.NoError(t, err)
25+
}

dummy/paper_list_response.json

+923
Large diffs are not rendered by default.

go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/codingpot/paperswithcode-go
22

3-
go 1.16
3+
go 1.16
4+
5+
require github.com/stretchr/testify v1.7.0

go.sum

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

models/paper_list_result.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package models
2+
3+
// PaperListResult is the result of PaperListResult() function.
4+
type PaperListResult struct {
5+
Count int64 `json:"count"`
6+
Next *string `json:"next"`
7+
Previous *string `json:"previous"`
8+
Results []PaperListResultItem `json:"results"`
9+
}
10+
11+
type PaperListResultItem struct {
12+
ID string `json:"id"`
13+
ArxivID *string `json:"arxiv_id"`
14+
NipsID *string `json:"nips_id"`
15+
URLAbs string `json:"url_abs"`
16+
URLPDF string `json:"url_pdf"`
17+
Title string `json:"title"`
18+
Abstract string `json:"abstract"`
19+
Authors []string `json:"authors"`
20+
Published YyyyMmDdDashed `json:"published"`
21+
Conference *string `json:"conference"`
22+
ConferenceURLAbs *string `json:"conference_url_abs"`
23+
ConferenceURLPDF *string `json:"conference_url_pdf"`
24+
Proceeding *string `json:"proceeding"`
25+
}

models/yyyymmdd.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package models
2+
3+
import (
4+
"encoding/json"
5+
"strings"
6+
"time"
7+
)
8+
9+
type YyyyMmDdDashed time.Time
10+
11+
func (y *YyyyMmDdDashed) UnmarshalJSON(bytes []byte) error {
12+
cleanString := strings.Trim(string(bytes), `"`)
13+
parsedTime, err := time.Parse("2006-01-02", cleanString)
14+
if err != nil {
15+
return err
16+
}
17+
*y = YyyyMmDdDashed(parsedTime)
18+
return nil
19+
}
20+
21+
func (y YyyyMmDdDashed) MarshalJSON() ([]byte, error) {
22+
return json.Marshal(y)
23+
}

models/yyyymmdd_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package models
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestYyyyMmDdDashed_UnmarshalJSON(t *testing.T) {
9+
type args struct {
10+
inputDateString string
11+
}
12+
tests := []struct {
13+
name string
14+
expected YyyyMmDdDashed
15+
args args
16+
wantErr bool
17+
}{
18+
{
19+
name: "2021-05-01 is a valid YyyyMmDdDashed type",
20+
expected: YyyyMmDdDashed(time.Date(2021, 5, 1, 0, 0, 0, 0, time.UTC)),
21+
args: args{
22+
inputDateString: "2021-05-01",
23+
},
24+
wantErr: false,
25+
},
26+
{
27+
name: "20210501 is NOT a valid YyyyMmDdDashed type",
28+
expected: YyyyMmDdDashed(time.Date(2021, 5, 1, 0, 0, 0, 0, time.UTC)),
29+
args: args{
30+
inputDateString: "20210501",
31+
},
32+
wantErr: true,
33+
},
34+
}
35+
for _, tt := range tests {
36+
t.Run(tt.name, func(t *testing.T) {
37+
if err := tt.expected.UnmarshalJSON([]byte(tt.args.inputDateString)); (err != nil) != tt.wantErr {
38+
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
39+
}
40+
})
41+
}
42+
}

0 commit comments

Comments
 (0)