From 4726db0bf22104a06993ec0d58da9a4c0b776c12 Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Sat, 15 May 2021 00:02:08 +0200 Subject: [PATCH] Add `context.Context` arg for Client methods, move methods to client.go --- README.md | 4 ++-- client.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++-- database.go | 63 -------------------------------------------------- 3 files changed, 66 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index a3d4d4a..f9a58dd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/client.go b/client.go index 501e90e..9691743 100644 --- a/client.go +++ b/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 +} diff --git a/database.go b/database.go index 23414e9..efa4149 100644 --- a/database.go +++ b/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 -}