From 8bd1c5a89b0da1c045ff1b30cb69f7388bce41ed Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 2 Dec 2016 15:36:09 +0000 Subject: [PATCH] Add Client.StateEvent method with test and example. Move examples to different file --- client.go | 9 ++++++++ client_examples_test.go | 39 +++++++++++++++++++++++++++++++ client_test.go | 51 +++++++++++++++++++++-------------------- 3 files changed, 74 insertions(+), 25 deletions(-) create mode 100644 client_examples_test.go diff --git a/client.go b/client.go index d60cadf..2ada98e 100644 --- a/client.go +++ b/client.go @@ -303,6 +303,15 @@ func (cli *Client) LeaveRoom(roomID string) (resp *RespLeaveRoom, err error) { return } +// StateEvent gets a single state event in a room. It will attempt to JSON unmarshal into the given "outContent" struct with +// the HTTP response body, or return an error. +// See http://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-rooms-roomid-state-eventtype-statekey +func (cli *Client) StateEvent(roomID, eventType, stateKey string, outContent interface{}) (err error) { + u := cli.BuildURL("rooms", roomID, "state", eventType, stateKey) + _, err = cli.MakeRequest("GET", u, nil, outContent) + return +} + // UploadLink uploads an HTTP URL and then returns an MXC URI. func (cli *Client) UploadLink(link string) (*RespMediaUpload, error) { res, err := cli.Client.Get(link) diff --git a/client_examples_test.go b/client_examples_test.go new file mode 100644 index 0000000..0d98022 --- /dev/null +++ b/client_examples_test.go @@ -0,0 +1,39 @@ +package gomatrix + +import "fmt" + +func ExampleClient_BuildURLWithQuery() { + cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "abcdef123456") + out := cli.BuildURLWithQuery([]string{"sync"}, map[string]string{ + "filter_id": "5", + }) + fmt.Println(out) + // Output: https://matrix.org/_matrix/client/r0/sync?access_token=abcdef123456&filter_id=5 +} + +func ExampleClient_BuildURL() { + userID := "@example:matrix.org" + cli, _ := NewClient("https://matrix.org", userID, "abcdef123456") + out := cli.BuildURL("user", userID, "filter") + fmt.Println(out) + // Output: https://matrix.org/_matrix/client/r0/user/@example:matrix.org/filter?access_token=abcdef123456 +} + +func ExampleClient_BuildBaseURL() { + userID := "@example:matrix.org" + cli, _ := NewClient("https://matrix.org", userID, "abcdef123456") + out := cli.BuildBaseURL("_matrix", "client", "r0", "directory", "room", "#matrix:matrix.org") + fmt.Println(out) + // Output: https://matrix.org/_matrix/client/r0/directory/room/%23matrix:matrix.org?access_token=abcdef123456 +} + +// Compiled, not run. +func ExampleClient_StateEvent() { + content := struct { + name string `json:"name"` + }{} + cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "abcdef123456") + if err := cli.StateEvent("!foo:bar", "m.room.name", "", &content); err != nil { + panic(err) + } +} diff --git a/client_test.go b/client_test.go index 081dc12..93bd896 100644 --- a/client_test.go +++ b/client_test.go @@ -8,31 +8,6 @@ import ( "testing" ) -func ExampleClient_BuildURLWithQuery() { - cli, _ := NewClient("https://matrix.org", "@example:matrix.org", "abcdef123456") - out := cli.BuildURLWithQuery([]string{"sync"}, map[string]string{ - "filter_id": "5", - }) - fmt.Println(out) - // Output: https://matrix.org/_matrix/client/r0/sync?access_token=abcdef123456&filter_id=5 -} - -func ExampleClient_BuildURL() { - userID := "@example:matrix.org" - cli, _ := NewClient("https://matrix.org", userID, "abcdef123456") - out := cli.BuildURL("user", userID, "filter") - fmt.Println(out) - // Output: https://matrix.org/_matrix/client/r0/user/@example:matrix.org/filter?access_token=abcdef123456 -} - -func ExampleClient_BuildBaseURL() { - userID := "@example:matrix.org" - cli, _ := NewClient("https://matrix.org", userID, "abcdef123456") - out := cli.BuildBaseURL("_matrix", "client", "r0", "directory", "room", "#matrix:matrix.org") - fmt.Println(out) - // Output: https://matrix.org/_matrix/client/r0/directory/room/%23matrix:matrix.org?access_token=abcdef123456 -} - func TestClient_LeaveRoom(t *testing.T) { cli := mockClient(func(req *http.Request) (*http.Response, error) { if req.Method == "POST" && req.URL.Path == "/_matrix/client/r0/rooms/!foo:bar/leave" { @@ -49,6 +24,32 @@ func TestClient_LeaveRoom(t *testing.T) { } } +func TestClient_StateEvent(t *testing.T) { + cli := mockClient(func(req *http.Request) (*http.Response, error) { + if req.Method == "GET" && req.URL.Path == "/_matrix/client/r0/rooms/!foo:bar/state/m.room.name" { + return &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBufferString(`{"name":"Room Name Goes Here"}`)), + }, nil + } + return nil, fmt.Errorf("unhandled URL: %s", req.URL.Path) + }) + + content := struct { + Name string `json:"name"` + }{} + expectContent := struct { + Name string `json:"name"` + }{"Room Name Goes Here"} + + if err := cli.StateEvent("!foo:bar", "m.room.name", "", &content); err != nil { + t.Fatalf("StateEvent: error, got %s", err.Error()) + } + if content.Name != expectContent.Name { + t.Fatalf("StateEvent: got %s, want %s", content.Name, expectContent.Name) + } +} + func mockClient(fn func(*http.Request) (*http.Response, error)) *Client { mrt := MockRoundTripper{ RT: fn,