1
0
mirror of https://github.com/dstotijn/go-notion.git synced 2024-11-28 08:58:51 +02:00
go-notion/client.go
2021-05-14 18:25:44 +02:00

57 lines
1.1 KiB
Go

package notion
import (
"fmt"
"io"
"net/http"
)
const (
baseURL = "https://api.notion.com/v1"
apiVersion = "2021-05-13"
clientVersion = "0.0.0"
)
// Client is used for HTTP requests to the Notion API.
type Client struct {
apiKey string
httpClient *http.Client
}
// ClientOption is used to override default client behavior.
type ClientOption func(*Client)
// NewClient returns a new Client.
func NewClient(apiKey string, opts ...ClientOption) *Client {
c := &Client{
apiKey: apiKey,
httpClient: http.DefaultClient,
}
for _, opt := range opts {
opt(c)
}
return c
}
// WithHTTPClient overrides the default http.Client.
func WithHTTPClient(httpClient *http.Client) ClientOption {
return func(c *Client) {
c.httpClient = httpClient
}
}
func (c *Client) newRequest(method, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, baseURL+url, body)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", c.apiKey))
req.Header.Set("Notion-Version", apiVersion)
req.Header.Set("User-Agent", "go-notion/"+clientVersion)
return req, nil
}