mirror of https://github.com/matrix-org/gomatrix
Add Client.StateEvent method with test and example. Move examples to different file
This commit is contained in:
parent
1842392399
commit
8bd1c5a89b
|
@ -303,6 +303,15 @@ func (cli *Client) LeaveRoom(roomID string) (resp *RespLeaveRoom, err error) {
|
||||||
return
|
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.
|
// UploadLink uploads an HTTP URL and then returns an MXC URI.
|
||||||
func (cli *Client) UploadLink(link string) (*RespMediaUpload, error) {
|
func (cli *Client) UploadLink(link string) (*RespMediaUpload, error) {
|
||||||
res, err := cli.Client.Get(link)
|
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"
|
"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) {
|
func TestClient_LeaveRoom(t *testing.T) {
|
||||||
cli := mockClient(func(req *http.Request) (*http.Response, error) {
|
cli := mockClient(func(req *http.Request) (*http.Response, error) {
|
||||||
if req.Method == "POST" && req.URL.Path == "/_matrix/client/r0/rooms/!foo:bar/leave" {
|
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 {
|
func mockClient(fn func(*http.Request) (*http.Response, error)) *Client {
|
||||||
mrt := MockRoundTripper{
|
mrt := MockRoundTripper{
|
||||||
RT: fn,
|
RT: fn,
|
||||||
|
|
Loading…
Reference in New Issue