github.com/CyCoreSystems/ari@v4.8.4+incompatible/client/native/bridge.go (about)

     1  package native
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"github.com/CyCoreSystems/ari"
     8  	"github.com/CyCoreSystems/ari/rid"
     9  )
    10  
    11  // Bridge provides the ARI Bridge accessors for the native client
    12  type Bridge struct {
    13  	client *Client
    14  }
    15  
    16  // Create creates a bridge and returns the lazy handle for the bridge
    17  func (b *Bridge) Create(key *ari.Key, t string, name string) (bh *ari.BridgeHandle, err error) {
    18  	bh, err = b.StageCreate(key, t, name)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  	return bh, bh.Exec()
    23  }
    24  
    25  // StageCreate creates a new bridge handle, staged with a bridge `Create` operation.
    26  func (b *Bridge) StageCreate(key *ari.Key, btype, name string) (*ari.BridgeHandle, error) {
    27  	if key.ID == "" {
    28  		key.ID = rid.New(rid.Bridge)
    29  	}
    30  
    31  	req := struct {
    32  		ID   string `json:"bridgeId,omitempty"`
    33  		Type string `json:"type,omitempty"`
    34  		Name string `json:"name,omitempty"`
    35  	}{
    36  		ID:   key.ID,
    37  		Type: btype,
    38  		Name: name,
    39  	}
    40  
    41  	return ari.NewBridgeHandle(b.client.stamp(key), b, func(bh *ari.BridgeHandle) (err error) {
    42  		return b.client.post("/bridges/"+key.ID, nil, &req)
    43  	}), nil
    44  }
    45  
    46  // Get gets the lazy handle for the given bridge id
    47  func (b *Bridge) Get(key *ari.Key) *ari.BridgeHandle {
    48  	return ari.NewBridgeHandle(b.client.stamp(key), b, nil)
    49  }
    50  
    51  // List lists the current bridges and returns a list of lazy handles
    52  func (b *Bridge) List(filter *ari.Key) (bx []*ari.Key, err error) {
    53  	// native client ignores filter
    54  
    55  	var bridges = []struct {
    56  		ID string `json:"id"`
    57  	}{}
    58  
    59  	err = b.client.get("/bridges", &bridges)
    60  	for _, i := range bridges {
    61  		k := b.client.stamp(ari.NewKey(ari.BridgeKey, i.ID))
    62  		if filter.Match(k) {
    63  			bx = append(bx, k)
    64  		}
    65  	}
    66  	return
    67  }
    68  
    69  // Data returns the details of a bridge
    70  // Equivalent to Get /bridges/{bridgeId}
    71  func (b *Bridge) Data(key *ari.Key) (*ari.BridgeData, error) {
    72  	if key == nil || key.ID == "" {
    73  		return nil, errors.New("bridge key not supplied")
    74  	}
    75  
    76  	var data = new(ari.BridgeData)
    77  	if err := b.client.get("/bridges/"+key.ID, data); err != nil {
    78  		return nil, dataGetError(err, "bridge", "%v", key.ID)
    79  	}
    80  
    81  	data.Key = b.client.stamp(key)
    82  
    83  	return data, nil
    84  }
    85  
    86  // AddChannel adds a channel to a bridge
    87  // Equivalent to Post /bridges/{id}/addChannel
    88  func (b *Bridge) AddChannel(key *ari.Key, channelID string) (err error) {
    89  	id := key.ID
    90  
    91  	type request struct {
    92  		ChannelID string `json:"channel"`
    93  		Role      string `json:"role,omitempty"`
    94  	}
    95  
    96  	req := request{channelID, ""}
    97  	err = b.client.post("/bridges/"+id+"/addChannel", nil, &req)
    98  	return
    99  }
   100  
   101  // RemoveChannel removes the specified channel from a bridge
   102  // Equivalent to Post /bridges/{id}/removeChannel
   103  func (b *Bridge) RemoveChannel(key *ari.Key, channelID string) (err error) {
   104  	id := key.ID
   105  
   106  	req := struct {
   107  		ChannelID string `json:"channel"`
   108  	}{
   109  		ChannelID: channelID,
   110  	}
   111  
   112  	//pass request
   113  	err = b.client.post("/bridges/"+id+"/removeChannel", nil, &req)
   114  	return
   115  }
   116  
   117  // Delete shuts down a bridge. If any channels are in this bridge,
   118  // they will be removed and resume whatever they were doing beforehand.
   119  // This means that the channels themselves are not deleted.
   120  // Equivalent to DELETE /bridges/{id}
   121  func (b *Bridge) Delete(key *ari.Key) (err error) {
   122  	return b.client.del("/bridges/"+key.ID, nil, "")
   123  }
   124  
   125  // MOH requests that the given musiconhold class be played to the bridge
   126  func (b *Bridge) MOH(key *ari.Key, class string) error {
   127  	req := struct {
   128  		Class string `json:"mohClass"`
   129  	}{
   130  		Class: class,
   131  	}
   132  	return b.client.post("/bridges/"+key.ID+"/moh", nil, &req)
   133  }
   134  
   135  // StopMOH requests that any MusicOnHold which is playing to the bridge be stopped.
   136  func (b *Bridge) StopMOH(key *ari.Key) error {
   137  	return b.client.del("/bridges/"+key.ID+"/moh", nil, "")
   138  }
   139  
   140  // Play attempts to play the given mediaURI on the bridge, using the playbackID
   141  // as the identifier to the created playback handle
   142  func (b *Bridge) Play(key *ari.Key, playbackID string, mediaURI string) (*ari.PlaybackHandle, error) {
   143  	h, err := b.StagePlay(key, playbackID, mediaURI)
   144  	if err != nil {
   145  		return nil, err
   146  	}
   147  	return h, h.Exec()
   148  }
   149  
   150  // StagePlay stages a `Play` operation on the bridge
   151  func (b *Bridge) StagePlay(key *ari.Key, playbackID string, mediaURI string) (*ari.PlaybackHandle, error) {
   152  	if playbackID == "" {
   153  		playbackID = rid.New(rid.Playback)
   154  	}
   155  
   156  	resp := make(map[string]interface{})
   157  	req := struct {
   158  		Media string `json:"media"`
   159  	}{
   160  		Media: mediaURI,
   161  	}
   162  	playbackKey := b.client.stamp(ari.NewKey(ari.PlaybackKey, playbackID))
   163  
   164  	return ari.NewPlaybackHandle(playbackKey, b.client.Playback(), func(h *ari.PlaybackHandle) error {
   165  		return b.client.post("/bridges/"+key.ID+"/play/"+playbackID, &resp, &req)
   166  	}), nil
   167  }
   168  
   169  // Record attempts to record audio on the bridge, using name as the identifier for
   170  // the created live recording handle
   171  func (b *Bridge) Record(key *ari.Key, name string, opts *ari.RecordingOptions) (*ari.LiveRecordingHandle, error) {
   172  	h, err := b.StageRecord(key, name, opts)
   173  	if err != nil {
   174  		return nil, err
   175  	}
   176  	return h, h.Exec()
   177  }
   178  
   179  // StageRecord stages a `Record` opreation
   180  func (b *Bridge) StageRecord(key *ari.Key, name string, opts *ari.RecordingOptions) (*ari.LiveRecordingHandle, error) {
   181  	if opts == nil {
   182  		opts = &ari.RecordingOptions{}
   183  	}
   184  
   185  	resp := make(map[string]interface{})
   186  	req := struct {
   187  		Name        string `json:"name"`
   188  		Format      string `json:"format"`
   189  		MaxDuration int    `json:"maxDurationSeconds"`
   190  		MaxSilence  int    `json:"maxSilenceSeconds"`
   191  		IfExists    string `json:"ifExists,omitempty"`
   192  		Beep        bool   `json:"beep"`
   193  		TerminateOn string `json:"terminateOn,omitempty"`
   194  	}{
   195  		Name:        name,
   196  		Format:      opts.Format,
   197  		MaxDuration: int(opts.MaxDuration / time.Second),
   198  		MaxSilence:  int(opts.MaxSilence / time.Second),
   199  		IfExists:    opts.Exists,
   200  		Beep:        opts.Beep,
   201  		TerminateOn: opts.Terminate,
   202  	}
   203  
   204  	recordingKey := b.client.stamp(ari.NewKey(ari.LiveRecordingKey, name))
   205  
   206  	return ari.NewLiveRecordingHandle(recordingKey, b.client.LiveRecording(), func(h *ari.LiveRecordingHandle) error {
   207  		return b.client.post("/bridges/"+key.ID+"/record", &resp, &req)
   208  	}), nil
   209  }
   210  
   211  // Subscribe creates an event subscription for events related to the given
   212  // bridge␃entity
   213  func (b *Bridge) Subscribe(key *ari.Key, n ...string) ari.Subscription {
   214  	return b.client.Bus().Subscribe(key, n...)
   215  }