Skip to content

Refactor to follow the standard Go package guide #2

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 1 commit into from
May 29, 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
31 changes: 16 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
/.idea/
42 changes: 21 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
MIT License

Copyright (c) 2021 코딩냄비

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
MIT License
Copyright (c) 2021 코딩냄비
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# paperswithcode-go
client code repository for paperswithcode's official APIs
# paperswithcode-go
client code repository for paperswithcode's official APIs

```go
import "github.com/codingpot/paperswithcode-go"
```

```go
APIToken := "<your-api-token>" // from https://paperswithcode.com/accounts/generate_api_token
c := paperswithcode_go.NewClient(paperswithcode_go.WithAPIToken(token))
```
36 changes: 34 additions & 2 deletions internal/client.go → client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package paperswithcode_go

import (
"context"
Expand All @@ -7,19 +7,51 @@ import (
"fmt"
"net/http"
"net/url"
"time"
)

const (
BaseURL = "https://paperswithcode.com/api/v1"
)

// ClientOption can be used to swap the default http client or swap the API key
type ClientOption func(*Client)

// WithAPIToken sets the client API token.
func WithAPIToken(apiToken string) ClientOption {
return func(client *Client) {
client.apiToken = apiToken
}
}

// NewClient creates a Client object.
func NewClient(opts ...ClientOption) *Client {
defaultClient := &Client{
BaseURL: BaseURL,
HTTPClient: &http.Client{
Timeout: time.Minute,
},
}

for _, opt := range opts {
opt(defaultClient)
}

return defaultClient
}

type Client struct {
BaseURL string
HTTPClient *http.Client
apiToken string
}

type errorResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}

// Specific Paper's Information by the title
// Paper represents a specific Paper's Information by the title
type Paper struct {
ID string `json:"id"`
ArxivID string `json:"arxiv_id,omitempty"`
Expand Down
24 changes: 7 additions & 17 deletions main.go → cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,18 @@ package main
import (
"context"
"fmt"
"net/http"
"github.com/codingpot/paperswithcode-go"
"os"
"strings"
"time"

client "paperswithcode/internal"
)

const (
BASE_URL = "https://paperswithcode.com/api/v1"
)

func NewClient() *client.Client {
return &client.Client{
BaseURL: BASE_URL,
HTTPClient: &http.Client{
Timeout: time.Minute,
},
func main() {
token, ok := os.LookupEnv("PAPERSWITHCODE_API_TOKEN")
if !ok {
panic("PAPERSWITHCODE_API_TOKEN environment variable is not found")
}
}

func main() {
c := NewClient()
c := paperswithcode_go.NewClient(paperswithcode_go.WithAPIToken(token))
ctx := context.Background()
paper := "generative adversarial networks"
paper = strings.ReplaceAll(paper, " ", "-")
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module paperswithcode
module github.com/codingpot/paperswithcode-go

go 1.16
go 1.16
Binary file removed main
Binary file not shown.