From 373dfc2ff2d1a29c00652fe42b8d69abd651f189 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 9 Dec 2016 16:45:49 +0000 Subject: [PATCH] Move client creation examples to client examples file --- client_examples_test.go | 49 ++++++++++++++++++++++++++++++++++++++++- example_test.go | 28 ----------------------- 2 files changed, 48 insertions(+), 29 deletions(-) delete mode 100644 example_test.go diff --git a/client_examples_test.go b/client_examples_test.go index 8f2c361..7c20c77 100644 --- a/client_examples_test.go +++ b/client_examples_test.go @@ -1,6 +1,53 @@ package gomatrix -import "fmt" +import ( + "fmt" + "net/http" +) + +func Example_sync() { + cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "MDAefhiuwehfuiwe") + cli.Store.SaveFilterID("@example:matrix.org", "2") // Optional: if you know it already + cli.Store.SaveNextBatch("@example:matrix.org", "111_222_333_444") // Optional: if you know it already + syncer := cli.Syncer.(*DefaultSyncer) + syncer.OnEventType("m.room.message", func(ev *Event) { + fmt.Println("Message: ", ev) + }) + + // Blocking version + if err := cli.Sync(); err != nil { + fmt.Println("Sync() returned ", err) + } + + // Non-blocking version + go func() { + for { + if err := cli.Sync(); err != nil { + fmt.Println("Sync() returned ", err) + } + // Optional: Wait a period of time before trying to sync again. + } + }() +} + +func Example_customInterfaces() { + // Custom interfaces must be set prior to calling functions on the client. + cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "MDAefhiuwehfuiwe") + + // anything which implements the Storer interface + customStore := NewInMemoryStore() + cli.Store = customStore + + // anything which implements the Syncer interface + customSyncer := NewDefaultSyncer("@example:matrix.org", customStore) + cli.Syncer = customSyncer + + // any http.Client + cli.Client = http.DefaultClient + + // Once you call a function, you can't safely change the interfaces. + cli.SendText("!foo:bar", "Down the rabbit hole") +} func ExampleClient_BuildURLWithQuery() { cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "abcdef123456") diff --git a/example_test.go b/example_test.go deleted file mode 100644 index 3655b9b..0000000 --- a/example_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package gomatrix - -import ( - "fmt" -) - -func Example_sync() { - cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "MDAefhiuwehfuiwe") - syncer := cli.Syncer.(*DefaultSyncer) - syncer.OnEventType("m.room.message", func(ev *Event) { - fmt.Println("Message: ", ev) - }) - - // Blocking version - if err := cli.Sync(); err != nil { - fmt.Println("Sync() returned ", err) - } - - // Non-blocking version - go func() { - for { - if err := cli.Sync(); err != nil { - fmt.Println("Sync() returned ", err) - } - // Optional: Wait a period of time before trying to sync again. - } - }() -}