From f7d3dab036542d4afa543b2616a1903989782e84 Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Sun, 8 Jan 2023 00:47:32 -0800 Subject: [PATCH] Move examples to examples directory --- README.md | 27 ++-------------- examples/http/main.go | 32 ++++++++++++++++++ examples/websocket/main.go | 66 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 24 deletions(-) create mode 100644 examples/http/main.go create mode 100644 examples/websocket/main.go diff --git a/README.md b/README.md index bb8e3dc..87e3530 100644 --- a/README.md +++ b/README.md @@ -4,31 +4,10 @@ Go bindings to the [Lemmy](https://join-lemmy.org) API, automatically generated directly from Lemmy's source code using the generator in [cmd/gen](cmd/gen). -Example: +Examples: -```go -ctx := context.Background() - -c, err := lemmy.New("https://lemmygrad.ml") -if err != nil { - panic(err) -} - -err = c.ClientLogin(ctx, types.Login{ - UsernameOrEmail: "user@example.com", - Password: `TestPwd`, -}) -if err != nil { - panic(err) -} - -_, err = c.SaveUserSettings(ctx, types.SaveUserSettings{ - BotAccount: types.NewOptional(true), -}) -if err != nil { - panic(err) -} -``` +- HTTP: [examples/http](examples/http) +- WebSocket: [examples/websocket](examples/websocket) ### How to generate diff --git a/examples/http/main.go b/examples/http/main.go new file mode 100644 index 0000000..9ff0224 --- /dev/null +++ b/examples/http/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "context" + + "go.arsenm.dev/go-lemmy" + "go.arsenm.dev/go-lemmy/types" +) + +func main() { + ctx := context.Background() + + c, err := lemmy.New("https://lemmygrad.ml") + if err != nil { + panic(err) + } + + err = c.ClientLogin(ctx, types.Login{ + UsernameOrEmail: "user@example.com", + Password: `TestPwd`, + }) + if err != nil { + panic(err) + } + + _, err = c.SaveUserSettings(ctx, types.SaveUserSettings{ + BotAccount: types.NewOptional(true), + }) + if err != nil { + panic(err) + } +} diff --git a/examples/websocket/main.go b/examples/websocket/main.go new file mode 100644 index 0000000..2e81d49 --- /dev/null +++ b/examples/websocket/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "context" + "log" + + "go.arsenm.dev/go-lemmy" + "go.arsenm.dev/go-lemmy/types" +) + +func main() { + ctx := context.Background() + + c, err := lemmy.NewWebSocket("https://lemmygrad.ml") + if err != nil { + panic(err) + } + + err = c.ClientLogin(ctx, types.Login{ + UsernameOrEmail: "user@example.com", + Password: `TestPwd`, + }) + if err != nil { + panic(err) + } + + // If nil is passed as data, go-lemmy will just send + // the auth token, which is all that's needed for + // the UserJoin operation. + c.Request(types.UserOperationUserJoin, nil) + + // Subscribe to all communities + c.Request(types.UserOperationCommunityJoin, types.CommunityJoin{ + CommunityID: 0, + }) + + go handleErrors(c) + handleResponses(c) +} + +func handleResponses(c *lemmy.WSClient) { + for res := range c.Responses() { + if res.IsOneOf(types.UserOperationCRUDCreateComment) { + var data types.CommentResponse + err := lemmy.DecodeResponse(res.Data, &data) + if err != nil { + log.Println("Error decoding response:", err) + continue + } + + err = c.Request(types.UserOperationCreateCommentLike, types.CreateCommentLike{ + CommentID: data.CommentView.Comment.ID, + }) + if err != nil { + log.Println("Error decoding response:", err) + continue + } + } + } +} + +func handleErrors(c *lemmy.WSClient) { + for err := range c.Errors() { + log.Println("Error decoding response:", err) + } +}