Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add get_comments api #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add get_comments api #16

wants to merge 2 commits into from

Conversation

reavessm
Copy link

No description provided.

Signed-off-by: Stephen Reaves <reaves735@gmail.com>
Signed-off-by: Stephen Reaves <reaves735@gmail.com>
@b3ngg
Copy link
Member

b3ngg commented Dec 24, 2024

Hey there,

thanks a lot for your contribution!

Is there any particular reason you added two new JSON request methods instead of using the existing one with generics, just like with the other API endpoints for pages, databases, etc.?

Other than that, it looks great.

Merry Christmas!

@reavessm
Copy link
Author

Hey,

Yeah I was having some issues parsing the ListResponse<Comment> using the original function, but when I specified the type manually it seemed to work fine. I'm open to using one function if it works. And the same applies to the User bits.

Merry Christmas to you too!

@b3ngg
Copy link
Member

b3ngg commented Jan 8, 2025

Hey,

sorry once again for the late response. The new year started quite busy for me unfortunately.

I see. The make_json_request method just returns a json Object as type at the moment that you can just parse however you want. This is how I would implement the method (not tested):

    pub async fn get_comments<T: AsIdentifier<BlockId>>(
        &self,
        block_id: T,
    ) -> Result<ListResponse<Comment>, Error> {
        let result = self
            .make_json_request(self.client.get(format!(
                "https://api.notion.com/v1/comments?block_id={block_id}",
                block_id = block_id.as_id()
            )))
            .await?;

        match result {
            Object::List { list } => Ok(list.expect_comments()?),
            response => Err(Error::UnexpectedResponse { response }),
        }
    }

with this implemented on ListResponse<Object>:

    pub(crate) fn expect_comments(self) -> Result<ListResponse<Comment>, crate::Error> {
        let items: Result<Vec<_>, _> = self
            .results
            .into_iter()
            .map(|object| match object {
                Object::Comment { comment } => Ok(comment),
                response => Err(Error::UnexpectedResponse { response }),
            })
            .collect();

        Ok(ListResponse {
            results: items?,
            has_more: self.has_more,
            next_cursor: self.next_cursor,
        })
    }

Should work in a similar way for Users.

BTW, I think comments.rs could be simplified by using the Comment struct directly without an additional enum. But not to sure about that.

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
#[serde(tag = "object", rename_all = "snake_case")]
pub struct Comment {
    /// Unique identifier for the database
    pub id: CommentId,
    /// Body of comment
    pub rich_text: Vec<RichText>,
    pub created_time: DateTime<Utc>,
    pub last_edited_time: DateTime<Utc>,
    pub parent: Parent,
    pub created_by: User,
    pub discussion_id: String,
}

Thanks!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants