Skip to content

feat: implement PaperRepositoryList() #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,39 +85,6 @@ func (c *Client) GetMethodList(ctx context.Context, id string) (*MethodList, err
return &res, nil
}

// Repository List implementing by Paper's ID
type RepositoryList struct {
Count int `json:"count"`
Results []Repository `json:"results"`
}

type Repository struct {
URL string `json:"url"`
IsOfficial bool `json:"is_official"`
Description string `json:"description"`
Stars int `json:"stars"`
Framework string `json:"framework"`
}

func (c *Client) GetRepositoryList(ctx context.Context, id string) (*RepositoryList, error) {
fmt.Println(id)
url := fmt.Sprintf("%s/papers/%s/repositories", c.BaseURL, url.QueryEscape(id))
req, err := http.NewRequest("GET", url, nil)

if err != nil {
return nil, err
}

req = req.WithContext(ctx)

res := RepositoryList{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}

return &res, nil
}

func (c *Client) sendRequest(req *http.Request, v interface{}) error {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Accept", "application/json; charset=utf-8")
Expand Down
2 changes: 1 addition & 1 deletion cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
fmt.Println()
fmt.Println()

repositoryList, _ := c.GetRepositoryList(ctx, paper)
repositoryList, _ := c.PaperRepositoryList(paper)
fmt.Println(repositoryList)
fmt.Println()
fmt.Println()
Expand Down
17 changes: 17 additions & 0 deletions models/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package models

// RepositoryList contains code repositories implemented by a certain paper.
type RepositoryList struct {
Count int64 `json:"count"`
Next *string `json:"next"`
Previous *string `json:"previous"`
Results []Repository `json:"results"`
}

type Repository struct {
URL string `json:"url"`
IsOfficial bool `json:"is_official"`
Description string `json:"description"`
Stars int `json:"stars"`
Framework string `json:"framework"`
}
24 changes: 24 additions & 0 deletions paper_repository_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package paperswithcode_go

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

func (c *Client) PaperRepositoryList(paperID string) (*models.RepositoryList, error) {
paperURL := fmt.Sprintf("%s/papers/%s/repositories", c.BaseURL, url.QueryEscape(paperID))
response, err := c.HTTPClient.Get(paperURL)
if err != nil {
return nil, err
}

var repoList models.RepositoryList
err = json.NewDecoder(response.Body).Decode(&repoList)
if err != nil {
return nil, err
}

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

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestClient_PaperRepositoryList(t *testing.T) {
c := NewClient()
list, err := c.PaperRepositoryList("generative-adversarial-networks")
assert.NoError(t, err)
assert.NotEmpty(t, list.Results[0].URL)
}