Skip to content

Commit

Permalink
Merge pull request #5 from Karim-W/refactor/newHttpClient
Browse files Browse the repository at this point in the history
fix: fixed room client issues
  • Loading branch information
Karim-W authored Aug 14, 2024
2 parents 0acd354 + e14befb commit 3e51e60
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 30 deletions.
114 changes: 84 additions & 30 deletions rooms/rooms.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rooms

import (
"context"
"encoding/json"

"github.com/karim-w/go-azure-communication-services/clientv2"
)
Expand Down Expand Up @@ -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(
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}
50 changes: 50 additions & 0 deletions rooms/rooms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rooms

import (
"context"
"os"
"testing"
"time"

Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down

0 comments on commit 3e51e60

Please sign in to comment.