mirror of
https://github.com/dstotijn/go-notion.git
synced 2025-12-23 23:51:17 +02:00
Add context.Context arg for Client methods, move methods to client.go
This commit is contained in:
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