Skip to content

Commit

Permalink
Add notion base implimentation
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Jul 5, 2024
1 parent 6bd0b88 commit 53d7816
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ go 1.22.5

require (
github.com/99designs/gqlgen v0.17.49
github.com/jomei/notionapi v1.13.1
github.com/labstack/echo/v4 v4.12.0
github.com/steinfletcher/apitest v1.5.16
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go v0.31.0
github.com/vektah/gqlparser/v2 v2.5.16
Expand Down
4 changes: 4 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jomei/notionapi v1.13.1 h1:LgL7H0pOg+kq2noFjvy6Hw9r9qqFIQUqgbLf+Yg46sc=
github.com/jomei/notionapi v1.13.1/go.mod h1:BqzP6JBddpBnXvMSIxiR5dCoCjKngmz5QNl1ONDlDoM=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.16.0 h1:iULayQNOReoYUe+1qtKOqw9CwJv3aNQu8ivo7lw1HU4=
Expand Down Expand Up @@ -140,6 +142,8 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sosodev/duration v1.3.1 h1:qtHBDMQ6lvMQsL15g4aopM4HEfOaYuhWBw3NPTtlqq4=
github.com/sosodev/duration v1.3.1/go.mod h1:RQIBBX0+fMLc/D9+Jb/fwvVmo0eZvDDEERAikUR6SDg=
github.com/steinfletcher/apitest v1.5.16 h1:J/ZoBmhgdzH4qfxPSw9kaXRBgzy3OsCoKh1gcc1h2zM=
github.com/steinfletcher/apitest v1.5.16/go.mod h1:mF+KnYaIkuHM0C4JgGzkIIOJAEjo+EA5tTjJ+bHXnQc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand Down
69 changes: 69 additions & 0 deletions backend/pkg/notion/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package notion

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

const apiVersion = "2022-06-28"

// NotionPage defines the structure to hold the response from the Notion API
type NotionPage struct {
Object string `json:"object"`
ID string `json:"id"`
CreatedTime string `json:"created_time"`
LastEditedTime string `json:"last_edited_time"`
Properties map[string]interface{} `json:"properties"`
}

// Client represents a client for interacting with the Notion API
type Client struct {
apiToken string
client *http.Client
baseURL string
}

// NewClient creates a new Notion client
func NewClient(apiToken string) *Client {
return &Client{
apiToken: apiToken,
client: &http.Client{},
baseURL: "https://api.notion.com/v1/pages/",
}
}

// GetPage retrieves the content of a specified page ID
func (c *Client) GetPage(pageID string) (*NotionPage, error) {
req, err := http.NewRequest("GET", c.baseURL+pageID, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}

// Set headers
req.Header.Set("Authorization", "Bearer "+c.apiToken)
req.Header.Set("Notion-Version", apiVersion)
req.Header.Set("Content-Type", "application/json")

resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()

// Read response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err)
}

// Decode response body into NotionPage struct
var notionPage NotionPage
err = json.Unmarshal(body, &notionPage)
if err != nil {
return nil, fmt.Errorf("error unmarshalling response body: %v", err)
}

return &notionPage, nil
}
75 changes: 75 additions & 0 deletions backend/pkg/notion/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package notion

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)

// TestGetPageSuccess tests the GetPage function for a successful response
func TestGetPageSuccess(t *testing.T) {
mockResponse := NotionPage{
Object: "page",
ID: "test_page_id",
CreatedTime: "2023-01-01T00:00:00.000Z",
LastEditedTime: "2023-01-02T00:00:00.000Z",
Properties: map[string]interface{}{
"title": "Test Title",
},
}

mockResponseBody, _ := json.Marshal(mockResponse)

// Create a new HTTP test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(mockResponseBody)
}))
defer ts.Close()

client := &Client{
apiToken: "test_token",
client: ts.Client(),
baseURL: ts.URL + "/",
}

page, err := client.GetPage("test_page_id")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}

if page.ID != mockResponse.ID {
t.Errorf("Expected page ID %s, got %s", mockResponse.ID, page.ID)
}
if page.CreatedTime != mockResponse.CreatedTime {
t.Errorf("Expected created time %s, got %s", mockResponse.CreatedTime, page.CreatedTime)
}
if page.LastEditedTime != mockResponse.LastEditedTime {
t.Errorf("Expected last edited time %s, got %s", mockResponse.LastEditedTime, page.LastEditedTime)
}
if page.Properties["title"] != mockResponse.Properties["title"] {
t.Errorf("Expected title %v, got %v", mockResponse.Properties["title"], page.Properties["title"])
}
}

// TestGetPageError tests the GetPage function for an error response
func TestGetPageError(t *testing.T) {
// Create a new HTTP test server that returns an error response
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer ts.Close()

client := &Client{
apiToken: "test_token",
client: ts.Client(),
baseURL: ts.URL + "/",
}

_, err := client.GetPage("test_page_id")
if err == nil {
t.Fatalf("Expected error, got none")
}
}

0 comments on commit 53d7816

Please sign in to comment.