-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bd0b88
commit 53d7816
Showing
4 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ¬ionPage) | ||
if err != nil { | ||
return nil, fmt.Errorf("error unmarshalling response body: %v", err) | ||
} | ||
|
||
return ¬ionPage, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |