Skip to content

Commit 0ba9776

Browse files
authored
Merge pull request #6 from codingpot/refactor-2-paper-repository-list
feat: implement PaperRepositoryList()
2 parents f6ddddc + 7e932eb commit 0ba9776

File tree

5 files changed

+55
-34
lines changed

5 files changed

+55
-34
lines changed

client.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -85,39 +85,6 @@ func (c *Client) GetMethodList(ctx context.Context, id string) (*MethodList, err
8585
return &res, nil
8686
}
8787

88-
// Repository List implementing by Paper's ID
89-
type RepositoryList struct {
90-
Count int `json:"count"`
91-
Results []Repository `json:"results"`
92-
}
93-
94-
type Repository struct {
95-
URL string `json:"url"`
96-
IsOfficial bool `json:"is_official"`
97-
Description string `json:"description"`
98-
Stars int `json:"stars"`
99-
Framework string `json:"framework"`
100-
}
101-
102-
func (c *Client) GetRepositoryList(ctx context.Context, id string) (*RepositoryList, error) {
103-
fmt.Println(id)
104-
url := fmt.Sprintf("%s/papers/%s/repositories", c.BaseURL, url.QueryEscape(id))
105-
req, err := http.NewRequest("GET", url, nil)
106-
107-
if err != nil {
108-
return nil, err
109-
}
110-
111-
req = req.WithContext(ctx)
112-
113-
res := RepositoryList{}
114-
if err := c.sendRequest(req, &res); err != nil {
115-
return nil, err
116-
}
117-
118-
return &res, nil
119-
}
120-
12188
func (c *Client) sendRequest(req *http.Request, v interface{}) error {
12289
req.Header.Set("Content-Type", "application/json; charset=utf-8")
12390
req.Header.Set("Accept", "application/json; charset=utf-8")

cmd/client/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func main() {
3131
fmt.Println()
3232
fmt.Println()
3333

34-
repositoryList, _ := c.GetRepositoryList(ctx, paper)
34+
repositoryList, _ := c.PaperRepositoryList(paper)
3535
fmt.Println(repositoryList)
3636
fmt.Println()
3737
fmt.Println()

models/repository.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package models
2+
3+
// RepositoryList contains code repositories implemented by a certain paper.
4+
type RepositoryList struct {
5+
Count int64 `json:"count"`
6+
Next *string `json:"next"`
7+
Previous *string `json:"previous"`
8+
Results []Repository `json:"results"`
9+
}
10+
11+
type Repository struct {
12+
URL string `json:"url"`
13+
IsOfficial bool `json:"is_official"`
14+
Description string `json:"description"`
15+
Stars int `json:"stars"`
16+
Framework string `json:"framework"`
17+
}

paper_repository_list.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package paperswithcode_go
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/codingpot/paperswithcode-go/models"
7+
"net/url"
8+
)
9+
10+
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)
13+
if err != nil {
14+
return nil, err
15+
}
16+
17+
var repoList models.RepositoryList
18+
err = json.NewDecoder(response.Body).Decode(&repoList)
19+
if err != nil {
20+
return nil, err
21+
}
22+
23+
return &repoList, nil
24+
}

paper_repository_list_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package paperswithcode_go
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestClient_PaperRepositoryList(t *testing.T) {
9+
c := NewClient()
10+
list, err := c.PaperRepositoryList("generative-adversarial-networks")
11+
assert.NoError(t, err)
12+
assert.NotEmpty(t, list.Results[0].URL)
13+
}

0 commit comments

Comments
 (0)