-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
55 lines (44 loc) · 1.19 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package paperswithcode_go
import (
"net/http"
"regexp"
"strings"
"time"
"github.com/codingpot/paperswithcode-go/v2/internal/transport"
)
const (
BaseURL = "https://paperswithcode.com/api/v1"
)
var whiteSpaceRegexp = regexp.MustCompile(`\s+`)
// 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
client.httpClient.Transport = transport.NewTransportWithAuthHeader(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
}
// GetPaperIDFromPaperTitle generates a paper ID from paper title.
// WARNING: This function does not cover all cases.
func GetPaperIDFromPaperTitle(paperTitle string) string {
return strings.ToLower(whiteSpaceRegexp.ReplaceAllString(paperTitle, "-"))
}