Skip to content

Commit 495a22e

Browse files
committed
Refactor to follow the standard Go package guide
- Module name is the repo name - main package should be in cmd/<binary-name>/main.go
1 parent 54320cf commit 495a22e

File tree

7 files changed

+91
-59
lines changed

7 files changed

+91
-59
lines changed

.gitignore

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
# Binaries for programs and plugins
2-
*.exe
3-
*.exe~
4-
*.dll
5-
*.so
6-
*.dylib
7-
8-
# Test binary, built with `go test -c`
9-
*.test
10-
11-
# Output of the go coverage tool, specifically when used with LiteIDE
12-
*.out
13-
14-
# Dependency directories (remove the comment below to include it)
15-
# vendor/
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/
16+
/.idea/

LICENSE

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
MIT License
2-
3-
Copyright (c) 2021 코딩냄비
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
1+
MIT License
2+
3+
Copyright (c) 2021 코딩냄비
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1-
# paperswithcode-go
2-
client code repository for paperswithcode's official APIs
1+
# paperswithcode-go
2+
client code repository for paperswithcode's official APIs
3+
4+
```go
5+
import "github.com/codingpot/paperswithcode-go"
6+
```
7+
8+
```go
9+
APIToken := "<your-api-token>" // from https://paperswithcode.com/accounts/generate_api_token
10+
c := paperswithcode_go.NewClient(paperswithcode_go.WithAPIToken(token))
11+
```

internal/client.go renamed to client.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package client
1+
package paperswithcode_go
22

33
import (
44
"context"
@@ -7,19 +7,51 @@ import (
77
"fmt"
88
"net/http"
99
"net/url"
10+
"time"
1011
)
1112

13+
const (
14+
BaseURL = "https://paperswithcode.com/api/v1"
15+
)
16+
17+
// ClientOption can be used to swap the default http client or swap the API key
18+
type ClientOption func(*Client)
19+
20+
// WithAPIToken sets the client API token.
21+
func WithAPIToken(apiToken string) ClientOption {
22+
return func(client *Client) {
23+
client.apiToken = apiToken
24+
}
25+
}
26+
27+
// NewClient creates a Client object.
28+
func NewClient(opts ...ClientOption) *Client {
29+
defaultClient := &Client{
30+
BaseURL: BaseURL,
31+
HTTPClient: &http.Client{
32+
Timeout: time.Minute,
33+
},
34+
}
35+
36+
for _, opt := range opts {
37+
opt(defaultClient)
38+
}
39+
40+
return defaultClient
41+
}
42+
1243
type Client struct {
1344
BaseURL string
1445
HTTPClient *http.Client
46+
apiToken string
1547
}
1648

1749
type errorResponse struct {
1850
Code int `json:"code"`
1951
Message string `json:"message"`
2052
}
2153

22-
// Specific Paper's Information by the title
54+
// Paper represents a specific Paper's Information by the title
2355
type Paper struct {
2456
ID string `json:"id"`
2557
ArxivID string `json:"arxiv_id,omitempty"`

main.go renamed to cmd/client/main.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,18 @@ package main
33
import (
44
"context"
55
"fmt"
6-
"net/http"
6+
"github.com/codingpot/paperswithcode-go"
7+
"os"
78
"strings"
8-
"time"
9-
10-
client "paperswithcode/internal"
11-
)
12-
13-
const (
14-
BASE_URL = "https://paperswithcode.com/api/v1"
159
)
1610

17-
func NewClient() *client.Client {
18-
return &client.Client{
19-
BaseURL: BASE_URL,
20-
HTTPClient: &http.Client{
21-
Timeout: time.Minute,
22-
},
11+
func main() {
12+
token, ok := os.LookupEnv("PAPERSWITHCODE_API_TOKEN")
13+
if !ok {
14+
panic("PAPERSWITHCODE_API_TOKEN environment variable is not found")
2315
}
24-
}
2516

26-
func main() {
27-
c := NewClient()
17+
c := paperswithcode_go.NewClient(paperswithcode_go.WithAPIToken(token))
2818
ctx := context.Background()
2919
paper := "generative adversarial networks"
3020
paper = strings.ReplaceAll(paper, " ", "-")

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module paperswithcode
1+
module github.com/codingpot/paperswithcode-go
22

3-
go 1.16
3+
go 1.16

main

-6.21 MB
Binary file not shown.

0 commit comments

Comments
 (0)