Skip to content

Commit 0fb2c69

Browse files
committed
feat: add docs and fix types
- interface{} -> *string in MethodList
1 parent 79c9c22 commit 0fb2c69

12 files changed

+41
-27
lines changed

client.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ type ClientOption func(*Client)
2222
func WithAPIToken(apiToken string) ClientOption {
2323
return func(client *Client) {
2424
client.apiToken = apiToken
25-
client.HTTPClient.Transport = transport.NewTransportWithAuthHeader(apiToken)
25+
client.httpClient.Transport = transport.NewTransportWithAuthHeader(apiToken)
2626
}
2727
}
2828

2929
// NewClient creates a Client object.
3030
func NewClient(opts ...ClientOption) *Client {
3131
defaultClient := &Client{
32-
BaseURL: BaseURL,
33-
HTTPClient: &http.Client{
32+
baseURL: BaseURL,
33+
httpClient: &http.Client{
3434
Timeout: time.Minute,
3535
},
3636
}
@@ -43,11 +43,13 @@ func NewClient(opts ...ClientOption) *Client {
4343
}
4444

4545
type Client struct {
46-
BaseURL string
47-
HTTPClient *http.Client
46+
baseURL string
47+
httpClient *http.Client
4848
apiToken string
4949
}
5050

51+
// GetPaperIDFromPaperTitle generates a paper ID from paper title.
52+
// WARNING: This function does not cover all cases.
5153
func GetPaperIDFromPaperTitle(paperTitle string) string {
5254
return strings.ToLower(whiteSpaceRegexp.ReplaceAllString(paperTitle, "-"))
5355
}

client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ func TestWithAPIToken(t *testing.T) {
2828
emptyRequest, err := http.NewRequest(http.MethodGet, server.URL, nil)
2929

3030
assert.NoError(t, err)
31-
_, err = c.HTTPClient.Transport.RoundTrip(emptyRequest)
31+
_, err = c.httpClient.Transport.RoundTrip(emptyRequest)
3232
assert.NoError(t, err)
3333
assert.Equal(t, "Token MY_TOKEN", emptyRequest.Header.Get("Authorization"))
3434
}
3535

3636
func TestTransportIsNotProvidedWhenNoAPIIsProvided(t *testing.T) {
3737
c := NewClient()
38-
assert.Nil(t, c.HTTPClient.Transport)
38+
assert.Nil(t, c.httpClient.Transport)
3939
}
4040

4141
func ExampleGetPaperIDFromPaperTitle() {

models/methods.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ type Method struct {
1010

1111
// MethodList represents methods used in the paper.
1212
type MethodList struct {
13-
Count int `json:"count"`
14-
Next interface{} `json:"next"`
15-
Previous interface{} `json:"previous"`
16-
Results []*Method `json:"results"`
13+
Count int `json:"count"`
14+
Next *string `json:"next"`
15+
Previous *string `json:"previous"`
16+
Results []*Method `json:"results"`
1717
}

models/results.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package models
22

3+
// Result is the evaluation result from a paper.
34
type Result struct {
45
ID string `json:"id"`
56
BestRank *int `json:"best_rank"`

models/yyyymmdd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77
)
88

9+
// YyyyMmDdDashed is the special date type to parse YYYY-MM-DD format.
910
type YyyyMmDdDashed time.Time
1011

1112
func (y *YyyyMmDdDashed) UnmarshalJSON(bytes []byte) error {

models/yyyymmdd_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package models
22

33
import (
44
"testing"
5-
"time"
65
)
76

87
func TestYyyyMmDdDashed_UnmarshalJSON(t *testing.T) {
@@ -17,15 +16,15 @@ func TestYyyyMmDdDashed_UnmarshalJSON(t *testing.T) {
1716
}{
1817
{
1918
name: "2021-05-01 is a valid YyyyMmDdDashed type",
20-
expected: YyyyMmDdDashed(time.Date(2021, 5, 1, 0, 0, 0, 0, time.UTC)),
19+
expected: NewYyyyMmDdDashed(2021, 5, 1),
2120
args: args{
2221
inputDateString: "2021-05-01",
2322
},
2423
wantErr: false,
2524
},
2625
{
2726
name: "20210501 is NOT a valid YyyyMmDdDashed type",
28-
expected: YyyyMmDdDashed(time.Date(2021, 5, 1, 0, 0, 0, 0, time.UTC)),
27+
expected: NewYyyyMmDdDashed(2021, 5, 1),
2928
args: args{
3029
inputDateString: "20210501",
3130
},

paper_get.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99

1010
// PaperGet returns a single paper. Note that paperID is hyphen cased (e.g., generative-adversarial-networks).
1111
func (c *Client) PaperGet(paperID string) (*models.Paper, error) {
12-
paperGetURL := fmt.Sprintf("%s/papers/%s/", c.BaseURL, url.QueryEscape(paperID))
13-
response, err := c.HTTPClient.Get(paperGetURL)
12+
paperGetURL := fmt.Sprintf("%s/papers/%s/", c.baseURL, url.QueryEscape(paperID))
13+
response, err := c.httpClient.Get(paperGetURL)
1414
if err != nil {
1515
return nil, err
1616
}

paper_list.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"net/url"
88
)
99

10+
// PaperList returns multiple papers.
1011
func (c *Client) PaperList(params PaperListParams) (*models.PaperList, error) {
11-
papersListURL := c.BaseURL + "/papers?" + params.Build()
12+
papersListURL := c.baseURL + "/papers?" + params.build()
1213

13-
response, err := c.HTTPClient.Get(papersListURL)
14+
response, err := c.httpClient.Get(papersListURL)
1415
if err != nil {
1516
return nil, err
1617
}
@@ -25,19 +26,25 @@ func (c *Client) PaperList(params PaperListParams) (*models.PaperList, error) {
2526
return &paperListResult, nil
2627
}
2728

29+
// PaperListParams is the parameter for PaperList method.
2830
type PaperListParams struct {
31+
// Query to search papers (default: "")
32+
// If empty, it returns all papers.
2933
Query string
30-
Page int
34+
// Page is the number of page to search (default: 1)
35+
Page int
36+
// Limit returns how many papers are returned in a single response.
3137
Limit int
3238
}
3339

34-
func (p PaperListParams) Build() string {
40+
func (p PaperListParams) build() string {
3541
if p.Query == "" {
3642
return fmt.Sprintf("items_per_page=%d&page=%d", p.Limit, p.Page)
3743
}
3844
return fmt.Sprintf("q=%s&items_per_page=%d&page=%d", url.QueryEscape(p.Query), p.Limit, p.Page)
3945
}
4046

47+
// PaperListParamsDefault returns the default PaperListParams.
4148
func PaperListParamsDefault() PaperListParams {
4249
return PaperListParams{
4350
Query: "",

paper_method_list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88
"net/url"
99
)
1010

11+
// PaperMethodList returns the methods used in the given paper.
1112
func (c *Client) PaperMethodList(paperID string) (*models.MethodList, error) {
12-
pURL := fmt.Sprintf("%s/papers/%s/methods", c.BaseURL, url.QueryEscape(paperID))
13+
pURL := fmt.Sprintf("%s/papers/%s/methods", c.baseURL, url.QueryEscape(paperID))
1314
response, err := http.Get(pURL)
1415
if err != nil {
1516
return nil, err

paper_repository_list.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77
"net/url"
88
)
99

10+
// PaperRepositoryList returns repositories related to the given paper.
1011
func (c *Client) PaperRepositoryList(paperID string) (*models.RepositoryList, error) {
11-
paperURL := fmt.Sprintf("%s/papers/%s/repositories", c.BaseURL, url.QueryEscape(paperID))
12-
response, err := c.HTTPClient.Get(paperURL)
12+
paperURL := fmt.Sprintf("%s/papers/%s/repositories", c.baseURL, url.QueryEscape(paperID))
13+
response, err := c.httpClient.Get(paperURL)
1314
if err != nil {
1415
return nil, err
1516
}

paper_result_list.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"net/url"
88
)
99

10+
// PaperResultList returns the evaluation results of the paper.
1011
func (c *Client) PaperResultList(paperID string) (*models.ResultList, error) {
11-
pURL := fmt.Sprintf("%s/papers/%s/results", c.BaseURL, url.QueryEscape(paperID))
12+
pURL := fmt.Sprintf("%s/papers/%s/results", c.baseURL, url.QueryEscape(paperID))
1213

13-
resp, err := c.HTTPClient.Get(pURL)
14+
resp, err := c.httpClient.Get(pURL)
1415
if err != nil {
1516
return nil, err
1617
}

paper_task_list.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77
"net/url"
88
)
99

10+
// PaperTaskList returns tasks (an area of research) for the given paper.
1011
func (c *Client) PaperTaskList(paperID string) (*models.TaskList, error) {
11-
pURL := fmt.Sprintf("%s/papers/%s/tasks/", c.BaseURL, url.QueryEscape(paperID))
12-
resp, err := c.HTTPClient.Get(pURL)
12+
pURL := fmt.Sprintf("%s/papers/%s/tasks/", c.baseURL, url.QueryEscape(paperID))
13+
resp, err := c.httpClient.Get(pURL)
1314
if err != nil {
1415
return nil, err
1516
}

0 commit comments

Comments
 (0)