mirror of
https://github.com/dstotijn/go-notion.git
synced 2025-07-01 00:45:10 +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
|
### Databases
|
||||||
|
|
||||||
- [x] [Retrieve a database](database.go)
|
- [x] [Retrieve a database](client.go)
|
||||||
- [x] [Query a database](database.go)
|
- [x] [Query a database](client.go)
|
||||||
|
|
||||||
## Pages
|
## Pages
|
||||||
|
|
||||||
|
66
client.go
66
client.go
@ -1,6 +1,9 @@
|
|||||||
package notion
|
package notion
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"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) {
|
func (c *Client) newRequest(ctx context.Context, method, url string, body io.Reader) (*http.Request, error) {
|
||||||
req, err := http.NewRequest(method, baseURL+url, body)
|
req, err := http.NewRequestWithContext(ctx, method, baseURL+url, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -58,3 +61,62 @@ func (c *Client) newRequest(method, url string, body io.Reader) (*http.Request,
|
|||||||
|
|
||||||
return req, nil
|
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
|
package notion
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -222,62 +218,3 @@ func (prop DatabaseProperty) Metadata() interface{} {
|
|||||||
return nil
|
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