mirror of https://github.com/matrix-org/gomatrix
Merge pull request #3 from matrix-org/kegan/state-event
Add Client.StateEvent method with test and example
This commit is contained in:
commit
45fc1b9fdc
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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,29 @@ 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"`
|
||||
}{}
|
||||
|
||||
if err := cli.StateEvent("!foo:bar", "m.room.name", "", &content); err != nil {
|
||||
t.Fatalf("StateEvent: error, got %s", err.Error())
|
||||
}
|
||||
if content.Name != "Room Name Goes Here" {
|
||||
t.Fatalf("StateEvent: got %s, want %s", content.Name, "Room Name Goes Here")
|
||||
}
|
||||
}
|
||||
|
||||
func mockClient(fn func(*http.Request) (*http.Response, error)) *Client {
|
||||
mrt := MockRoundTripper{
|
||||
RT: fn,
|
||||
|
|
Loading…
Reference in New Issue