mirror of
https://github.com/dstotijn/go-notion.git
synced 2024-11-24 08:42:26 +02:00
55 lines
1.0 KiB
Go
55 lines
1.0 KiB
Go
|
package notion
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
baseURL = "https://api.notion.com/v1"
|
||
|
apiVersion = "2021-05-13"
|
||
|
)
|
||
|
|
||
|
// 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)
|
||
|
|
||
|
return req, nil
|
||
|
}
|