diff --git a/examples/http/main.go b/examples/basic/main.go similarity index 90% rename from examples/http/main.go rename to examples/basic/main.go index 90a44b2..ad84bdf 100644 --- a/examples/http/main.go +++ b/examples/basic/main.go @@ -9,7 +9,7 @@ import ( func main() { ctx := context.Background() - c, err := lemmy.New("https://lemmygrad.ml") + c, err := lemmy.New("https://lemmy.ml") if err != nil { panic(err) } diff --git a/examples/comments/main.go b/examples/comments/main.go new file mode 100644 index 0000000..476e7c0 --- /dev/null +++ b/examples/comments/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "context" + "log" + + "go.elara.ws/go-lemmy" +) + +func main() { + ctx := context.Background() + + c, err := lemmy.New("https://lemmy.ml") + if err != nil { + panic(err) + } + + err = c.ClientLogin(ctx, lemmy.Login{ + UsernameOrEmail: "user@example.com", + Password: `TestPwd`, + }) + if err != nil { + panic(err) + } + + cr, err := c.CreateComment(ctx, lemmy.CreateComment{ + PostID: 2, + Content: "Hello from go-lemmy!", + }) + if err != nil { + panic(err) + } + + cr2, err := c.CreateComment(ctx, lemmy.CreateComment{ + PostID: 2, + ParentID: lemmy.NewOptional(cr.CommentView.Comment.ID), + Content: "Reply from go-lemmy", + }) + if err != nil { + panic(err) + } + + log.Printf( + "Created comment %d and replied to it with comment %d", + cr.CommentView.Comment.ID, + cr2.CommentView.Comment.ID, + ) +} diff --git a/examples/posts/main.go b/examples/posts/main.go new file mode 100644 index 0000000..64216e2 --- /dev/null +++ b/examples/posts/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "context" + "log" + + "go.elara.ws/go-lemmy" +) + +func main() { + ctx := context.Background() + + c, err := lemmy.New("https://lemmy.ml") + if err != nil { + panic(err) + } + + // Log in to lemmy.ml + err = c.ClientLogin(ctx, lemmy.Login{ + UsernameOrEmail: "user@example.com", + Password: `TestPwd`, + }) + if err != nil { + panic(err) + } + + // Get the linux community to get its ID. + gcr, err := c.Community(ctx, lemmy.GetCommunity{ + Name: lemmy.NewOptional("linux"), + }) + if err != nil { + panic(err) + } + + // Create a Hello World post in the linux community. + pr, err := c.CreatePost(ctx, lemmy.CreatePost{ + CommunityID: gcr.CommunityView.Community.ID, + Name: "Hello, World!", + Body: lemmy.NewOptional("This is an example post"), + }) + + log.Println("Created post:", pr.PostView.Post.ID) +}