github.com/CyCoreSystems/ari@v4.8.4+incompatible/application.go (about)

     1  package ari
     2  
     3  // Application represents a communication path interacting with an Asterisk
     4  // server for application-level resources
     5  type Application interface {
     6  
     7  	// List returns the list of applications in Asterisk, optionally using the key for filtering
     8  	List(*Key) ([]*Key, error)
     9  
    10  	// Get returns a handle to the application for further interaction
    11  	Get(key *Key) *ApplicationHandle
    12  
    13  	// Data returns the applications data
    14  	Data(key *Key) (*ApplicationData, error)
    15  
    16  	// Subscribe subscribes the given application to an event source
    17  	// event source may be one of:
    18  	//  - channel:<channelId>
    19  	//  - bridge:<bridgeId>
    20  	//  - endpoint:<tech>/<resource> (e.g. SIP/102)
    21  	//  - deviceState:<deviceName>
    22  	Subscribe(key *Key, eventSource string) error
    23  
    24  	// Unsubscribe unsubscribes (removes a subscription to) a given
    25  	// ARI application from the provided event source
    26  	// Equivalent to DELETE /applications/{applicationName}/subscription
    27  	Unsubscribe(key *Key, eventSource string) error
    28  }
    29  
    30  // ApplicationData describes the data for a Stasis (Ari) application
    31  type ApplicationData struct {
    32  	// Key is the unique identifier for this application instance in the cluster
    33  	Key *Key `json:"key"`
    34  
    35  	BridgeIDs   []string `json:"bridge_ids"`   // Subscribed BridgeIds
    36  	ChannelIDs  []string `json:"channel_ids"`  // Subscribed ChannelIds
    37  	DeviceNames []string `json:"device_names"` // Subscribed Device names
    38  	EndpointIDs []string `json:"endpoint_ids"` // Subscribed Endpoints (tech/resource format)
    39  	Name        string   `json:"name"`         // Name of the application
    40  }
    41  
    42  // ApplicationHandle provides a wrapper to an Application interface for
    43  // operations on a specific application
    44  type ApplicationHandle struct {
    45  	key *Key
    46  	a   Application
    47  }
    48  
    49  // NewApplicationHandle creates a new handle to the application name
    50  func NewApplicationHandle(key *Key, app Application) *ApplicationHandle {
    51  	return &ApplicationHandle{
    52  		key: key,
    53  		a:   app,
    54  	}
    55  }
    56  
    57  // ID returns the identifier for the application
    58  func (ah *ApplicationHandle) ID() string {
    59  	return ah.key.ID
    60  }
    61  
    62  // Key returns the key of the application
    63  func (ah *ApplicationHandle) Key() *Key {
    64  	return ah.key
    65  }
    66  
    67  // Data retrives the data for the application
    68  func (ah *ApplicationHandle) Data() (ad *ApplicationData, err error) {
    69  	ad, err = ah.a.Data(ah.key)
    70  	return
    71  }
    72  
    73  // Subscribe subscribes the application to an event source
    74  // event source may be one of:
    75  //  - channel:<channelId>
    76  //  - bridge:<bridgeId>
    77  //  - endpoint:<tech>/<resource> (e.g. SIP/102)
    78  //  - deviceState:<deviceName>
    79  func (ah *ApplicationHandle) Subscribe(eventSource string) (err error) {
    80  	err = ah.a.Subscribe(ah.key, eventSource)
    81  	return
    82  }
    83  
    84  // Unsubscribe unsubscribes (removes a subscription to) a given
    85  // ARI application from the provided event source
    86  // Equivalent to DELETE /applications/{applicationName}/subscription
    87  func (ah *ApplicationHandle) Unsubscribe(eventSource string) (err error) {
    88  	err = ah.a.Unsubscribe(ah.key, eventSource)
    89  	return
    90  }
    91  
    92  // Match returns true fo the event matches the application
    93  func (ah *ApplicationHandle) Match(e Event) bool {
    94  	return e.GetApplication() == ah.key.ID
    95  }