mirror of
https://github.com/dstotijn/go-notion.git
synced 2025-06-29 00:41:34 +02:00
Add context.Context
arg for Client methods, move methods to client.go
This commit is contained in:
@ -10,8 +10,8 @@ Go client for the [Notion API](https://developers.notion.com/reference).
|
||||
|
||||
### Databases
|
||||
|
||||
- [x] [Retrieve a database](database.go)
|
||||
- [x] [Query a database](database.go)
|
||||
- [x] [Retrieve a database](client.go)
|
||||
- [x] [Query a database](client.go)
|
||||
|
||||
## Pages
|
||||
|
||||
|
66
client.go
66
client.go
@ -1,6 +1,9 @@
|
||||
package notion
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@ -42,8 +45,8 @@ func WithHTTPClient(httpClient *http.Client) ClientOption {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) newRequest(method, url string, body io.Reader) (*http.Request, error) {
|
||||
req, err := http.NewRequest(method, baseURL+url, body)
|
||||
func (c *Client) newRequest(ctx context.Context, method, url string, body io.Reader) (*http.Request, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, method, baseURL+url, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -58,3 +61,62 @@ func (c *Client) newRequest(method, url string, body io.Reader) (*http.Request,
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// FindDatabaseByID fetches a database by ID.
|
||||
// See: https://developers.notion.com/reference/get-database
|
||||
func (c *Client) FindDatabaseByID(ctx context.Context, id string) (db Database, err error) {
|
||||
req, err := c.newRequest(ctx, http.MethodGet, "/databases/"+id, nil)
|
||||
if err != nil {
|
||||
return Database{}, fmt.Errorf("notion: invalid URL: %w", err)
|
||||
}
|
||||
|
||||
res, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return Database{}, fmt.Errorf("notion: failed to make HTTP request: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return Database{}, fmt.Errorf("notion: failed to find database: %w", parseErrorResponse(res))
|
||||
}
|
||||
|
||||
err = json.NewDecoder(res.Body).Decode(&db)
|
||||
if err != nil {
|
||||
return Database{}, fmt.Errorf("notion: failed to parse HTTP response: %w", err)
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// QueryDatabase returns database contents, with optional filters, sorts and pagination.
|
||||
// See: https://developers.notion.com/reference/post-database-query
|
||||
func (c *Client) QueryDatabase(ctx context.Context, id string, query DatabaseQuery) (result DatabaseQueryResponse, err error) {
|
||||
body := &bytes.Buffer{}
|
||||
|
||||
err = json.NewEncoder(body).Encode(query)
|
||||
if err != nil {
|
||||
return DatabaseQueryResponse{}, fmt.Errorf("notion: failed to encode filter to JSON: %w", err)
|
||||
}
|
||||
|
||||
req, err := c.newRequest(ctx, http.MethodPost, fmt.Sprintf("/databases/%v/query", id), body)
|
||||
if err != nil {
|
||||
return DatabaseQueryResponse{}, fmt.Errorf("notion: invalid URL: %w", err)
|
||||
}
|
||||
|
||||
res, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return DatabaseQueryResponse{}, fmt.Errorf("notion: failed to make HTTP request: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return DatabaseQueryResponse{}, fmt.Errorf("notion: failed to find database: %w", parseErrorResponse(res))
|
||||
}
|
||||
|
||||
err = json.NewDecoder(res.Body).Decode(&result)
|
||||
if err != nil {
|
||||
return DatabaseQueryResponse{}, fmt.Errorf("notion: failed to parse HTTP response: %w", err)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
63
database.go
63
database.go
@ -1,10 +1,6 @@
|
||||
package notion
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -222,62 +218,3 @@ func (prop DatabaseProperty) Metadata() interface{} {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// FindDatabaseByID fetches a database by ID.
|
||||
// See: https://developers.notion.com/reference/get-database
|
||||
func (c *Client) FindDatabaseByID(id string) (db Database, err error) {
|
||||
req, err := c.newRequest(http.MethodGet, "/databases/"+id, nil)
|
||||
if err != nil {
|
||||
return Database{}, fmt.Errorf("notion: invalid URL: %w", err)
|
||||
}
|
||||
|
||||
res, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return Database{}, fmt.Errorf("notion: failed to make HTTP request: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return Database{}, fmt.Errorf("notion: failed to find database: %w", parseErrorResponse(res))
|
||||
}
|
||||
|
||||
err = json.NewDecoder(res.Body).Decode(&db)
|
||||
if err != nil {
|
||||
return Database{}, fmt.Errorf("notion: failed to parse HTTP response: %w", err)
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// QueryDatabase returns database contents, with optional filters, sorts and pagination.
|
||||
// See: https://developers.notion.com/reference/post-database-query
|
||||
func (c *Client) QueryDatabase(id string, query DatabaseQuery) (result DatabaseQueryResponse, err error) {
|
||||
body := &bytes.Buffer{}
|
||||
|
||||
err = json.NewEncoder(body).Encode(query)
|
||||
if err != nil {
|
||||
return DatabaseQueryResponse{}, fmt.Errorf("notion: failed to encode filter to JSON: %w", err)
|
||||
}
|
||||
|
||||
req, err := c.newRequest(http.MethodPost, fmt.Sprintf("/databases/%v/query", id), body)
|
||||
if err != nil {
|
||||
return DatabaseQueryResponse{}, fmt.Errorf("notion: invalid URL: %w", err)
|
||||
}
|
||||
|
||||
res, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return DatabaseQueryResponse{}, fmt.Errorf("notion: failed to make HTTP request: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return DatabaseQueryResponse{}, fmt.Errorf("notion: failed to find database: %w", parseErrorResponse(res))
|
||||
}
|
||||
|
||||
err = json.NewDecoder(res.Body).Decode(&result)
|
||||
if err != nil {
|
||||
return DatabaseQueryResponse{}, fmt.Errorf("notion: failed to parse HTTP response: %w", err)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user