diff --git a/.gitignore b/.gitignore index 66fd13c..0a8865a 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/LICENSE b/LICENSE index 1fa0e58..c563a4d 100644 --- a/LICENSE +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md index bf72563..afa8318 100644 --- a/README.md +++ b/README.md @@ -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 := "" // from https://paperswithcode.com/accounts/generate_api_token +c := paperswithcode_go.NewClient(paperswithcode_go.WithAPIToken(token)) +``` diff --git a/internal/client.go b/client.go similarity index 86% rename from internal/client.go rename to client.go index 8ab3823..70b2e7c 100644 --- a/internal/client.go +++ b/client.go @@ -1,4 +1,4 @@ -package client +package paperswithcode_go import ( "context" @@ -7,11 +7,43 @@ 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 { @@ -19,7 +51,7 @@ type errorResponse struct { 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"` diff --git a/main.go b/cmd/client/main.go similarity index 66% rename from main.go rename to cmd/client/main.go index f0ec906..46b6d43 100644 --- a/main.go +++ b/cmd/client/main.go @@ -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, " ", "-") diff --git a/go.mod b/go.mod index 90a9ffe..a2f4467 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module paperswithcode +module github.com/codingpot/paperswithcode-go -go 1.16 +go 1.16 \ No newline at end of file diff --git a/main b/main deleted file mode 100755 index 9991e70..0000000 Binary files a/main and /dev/null differ