diff --git a/rooms/rooms.go b/rooms/rooms.go index 6550721..de56b3a 100644 --- a/rooms/rooms.go +++ b/rooms/rooms.go @@ -2,6 +2,7 @@ package rooms import ( "context" + "encoding/json" "github.com/karim-w/go-azure-communication-services/clientv2" ) @@ -66,77 +67,100 @@ func (c *_RoomsClient) CreateRoom( return nil, ERR_ROOMS_NIL_OPTIONS } responseModel := &RoomModel{} - err := c.client.Post( + response, err := c.client.Post( ctx, c.host, "/rooms", - "api-version="+apiVersion, + map[string][]string{ + "api-version": {apiVersion}, + }, options, - &responseModel, ) if err != nil { return nil, err } + + err = json.Unmarshal(response, &responseModel) + if err != nil { + return nil, err + } + return responseModel, nil } func (c *_RoomsClient) GetRoom( ctx context.Context, roomId string, -) (*RoomModel, error) { - responseModel := &RoomModel{} - err := c.client.Get( +) (result *RoomModel, err error) { + res, err := c.client.Get( ctx, c.host, "/rooms/"+roomId, - "api-version="+apiVersion, - &responseModel, + map[string][]string{ + "api-version": {apiVersion}, + }, ) if err != nil { return nil, err } - return responseModel, nil + + err = json.Unmarshal(res, &result) + if err != nil { + return + } + + return } func (c *_RoomsClient) UpdateRoom( ctx context.Context, roomId string, options *UpdateRoomOptions, -) (*RoomModel, error) { +) (res *RoomModel, err error) { if options == nil { return nil, ERR_ROOMS_NIL_OPTIONS } - responseModel := &RoomModel{} - err := c.client.Patch( + + body, err := c.client.Patch( ctx, c.host, "/rooms/"+roomId, - "api-version="+apiVersion, + map[string][]string{ + "api-version": {apiVersion}, + }, RoomModel{ ValidFrom: options.ValidFrom, ValidUntil: options.ValidUntil, Participants: options.Participants, RoomJoinPolicy: options.RoomJoinPolicy, }, - &responseModel, ) if err != nil { return nil, err } - return responseModel, nil + + err = json.Unmarshal(body, &res) + if err != nil { + return nil, err + } + + return } func (c *_RoomsClient) DeleteRoom( ctx context.Context, roomId string, ) error { - return c.client.Delete( + _, err := c.client.Delete( ctx, c.host, "/rooms/"+roomId, - "api-version="+apiVersion, - nil, + map[string][]string{ + "api-version": {apiVersion}, + }, ) + + return err } func (c *_RoomsClient) AddParticipants( @@ -145,17 +169,25 @@ func (c *_RoomsClient) AddParticipants( Participants ...RoomParticipant, ) (*[]RoomParticipant, error) { responseModel := &roomParticipantsUpdate{} - err := c.client.Post( + + body, err := c.client.Post( ctx, c.host, "/rooms/"+roomId+"/participants:add", - "api-version="+apiVersion, + map[string][]string{ + "api-version": {apiVersion}, + }, roomParticipantsUpdate{Participants}, - &responseModel, ) if err != nil { return nil, err } + + err = json.Unmarshal(body, &responseModel) + if err != nil { + return nil, err + } + return &responseModel.Participants, nil } @@ -164,16 +196,23 @@ func (c *_RoomsClient) GetParticipants( roomId string, ) (*[]RoomParticipant, error) { responseModel := &roomParticipantsUpdate{} - err := c.client.Get( + body, err := c.client.Get( ctx, c.host, "/rooms/"+roomId+"/participants", - "api-version="+apiVersion, - &responseModel, + map[string][]string{ + "api-version": {apiVersion}, + }, ) if err != nil { return nil, err } + + err = json.Unmarshal(body, &responseModel) + if err != nil { + return nil, err + } + return &responseModel.Participants, nil } @@ -183,17 +222,25 @@ func (c *_RoomsClient) UpdateParticipants( Participants ...RoomParticipant, ) (*[]RoomParticipant, error) { responseModel := &roomParticipantsUpdate{} - err := c.client.Post( + body, err := c.client.Post( ctx, c.host, "/rooms/"+roomId+"/participants:update", - "api-version="+apiVersion, + map[string][]string{ + "api-version": {apiVersion}, + }, + roomParticipantsUpdate{Participants}, - &responseModel, ) if err != nil { return nil, err } + + err = json.Unmarshal(body, &responseModel) + if err != nil { + return nil, err + } + return &responseModel.Participants, nil } @@ -203,16 +250,23 @@ func (c *_RoomsClient) RemoveParticipants( Participants ...RoomParticipant, ) (*[]RoomParticipant, error) { responseModel := &roomParticipantsUpdate{} - err := c.client.Post( + body, err := c.client.Post( ctx, c.host, "/rooms/"+roomId+"/participants:remove", - "api-version="+apiVersion, + map[string][]string{ + "api-version": {apiVersion}, + }, roomParticipantsUpdate{Participants}, - &responseModel, ) if err != nil { return nil, err } + + err = json.Unmarshal(body, &responseModel) + if err != nil { + return nil, err + } + return &responseModel.Participants, nil } diff --git a/rooms/rooms_test.go b/rooms/rooms_test.go index de3ff39..fb18183 100644 --- a/rooms/rooms_test.go +++ b/rooms/rooms_test.go @@ -2,6 +2,7 @@ package rooms import ( "context" + "os" "testing" "time" @@ -15,7 +16,19 @@ var ( roomid = "" ) +func precheck() { + host = os.Getenv("ACS_HOST") + key = os.Getenv("ACS_KEY") + id = os.Getenv("ACS_USER_ID") + roomid = os.Getenv("ACS_ROOM_ID") +} + func TestCreateRoom(t *testing.T) { + precheck() + if host == "" || key == "" || id == "" { + t.Skip("no host, key or id") + } + client := New(host, key) room, err := client.CreateRoom( context.TODO(), @@ -28,11 +41,19 @@ func TestCreateRoom(t *testing.T) { }, }, ) + + t.Log("created room", room.Id) + assert.Nil(t, err) assert.NotNil(t, room) } func TestGetRoom(t *testing.T) { + precheck() + if host == "" || key == "" || roomid == "" { + t.Skip("no host, key or roomid") + } + client := New(host, key) room, err := client.GetRoom( context.TODO(), @@ -43,6 +64,11 @@ func TestGetRoom(t *testing.T) { } func TestUpdateRoom(t *testing.T) { + precheck() + if host == "" || key == "" || roomid == "" || id == "" { + t.Skip("no host, key, roomid or id") + } + client := New(host, key) room, err := client.UpdateRoom( context.TODO(), @@ -61,6 +87,11 @@ func TestUpdateRoom(t *testing.T) { } func TestDeleteRoom(t *testing.T) { + precheck() + if host == "" || key == "" || roomid == "" { + t.Skip("no host, key or roomid") + } + client := New(host, key) err := client.DeleteRoom( context.TODO(), @@ -70,6 +101,11 @@ func TestDeleteRoom(t *testing.T) { } func TestAddParticipant(t *testing.T) { + precheck() + if host == "" || key == "" || roomid == "" || id == "" { + t.Skip("no host, key, roomid or id") + } + client := New(host, key) room, err := client.AddParticipants( context.TODO(), @@ -81,6 +117,10 @@ func TestAddParticipant(t *testing.T) { } func TestGetParticipants(t *testing.T) { + precheck() + if host == "" || key == "" || roomid == "" { + t.Skip("no host, key or roomid") + } client := New(host, key) room, err := client.GetParticipants( context.TODO(), @@ -91,6 +131,11 @@ func TestGetParticipants(t *testing.T) { } func TestUpdateParticipants(t *testing.T) { + precheck() + if host == "" || key == "" || roomid == "" || id == "" { + t.Skip("no host, key, roomid or id") + } + client := New(host, key) room, err := client.UpdateParticipants( context.TODO(), @@ -102,6 +147,11 @@ func TestUpdateParticipants(t *testing.T) { } func TestRemoveParticipant(t *testing.T) { + precheck() + if host == "" || key == "" || roomid == "" || id == "" { + t.Skip("no host, key, roomid or id") + } + client := New(host, key) room, err := client.RemoveParticipants( context.TODO(),