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

     1  package native
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/CyCoreSystems/ari"
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  // Application is a native implementation of ARI's Application functions
    11  type Application struct {
    12  	client *Client
    13  }
    14  
    15  // Get returns a managed handle to an ARI application
    16  func (a *Application) Get(key *ari.Key) *ari.ApplicationHandle {
    17  	return ari.NewApplicationHandle(a.client.stamp(key), a)
    18  }
    19  
    20  // List returns the list of applications managed by asterisk
    21  func (a *Application) List(filter *ari.Key) (ax []*ari.Key, err error) {
    22  
    23  	if filter == nil {
    24  		filter = ari.NewKey(ari.ApplicationKey, "")
    25  	}
    26  
    27  	var apps = []struct {
    28  		Name string `json:"name"`
    29  	}{}
    30  
    31  	err = a.client.get("/applications", &apps)
    32  
    33  	for _, i := range apps {
    34  		k := a.client.stamp(ari.NewKey(ari.ApplicationKey, i.Name))
    35  		if filter.Match(k) {
    36  			ax = append(ax, k)
    37  		}
    38  	}
    39  
    40  	err = errors.Wrap(err, "Error listing applications")
    41  	return
    42  }
    43  
    44  // Data returns the details of a given ARI application
    45  // Equivalent to GET /applications/{applicationName}
    46  func (a *Application) Data(key *ari.Key) (*ari.ApplicationData, error) {
    47  	if key == nil || key.ID == "" {
    48  		return nil, errors.New("application key not supplied")
    49  	}
    50  
    51  	var data = new(ari.ApplicationData)
    52  	if err := a.client.get("/applications/"+key.ID, data); err != nil {
    53  		return nil, dataGetError(err, "application", "%v", key.ID)
    54  	}
    55  
    56  	data.Key = a.client.stamp(key)
    57  	return data, nil
    58  }
    59  
    60  // Subscribe subscribes the given application to an event source
    61  // Equivalent to POST /applications/{applicationName}/subscription
    62  func (a *Application) Subscribe(key *ari.Key, eventSource string) error {
    63  	req := struct {
    64  		EventSource string `json:"eventSource"`
    65  	}{
    66  		EventSource: eventSource,
    67  	}
    68  	err := a.client.post("/applications/"+key.ID+"/subscription", nil, &req)
    69  	return errors.Wrapf(err, "Error subscribing application '%v' for event source '%v'", key.ID, eventSource)
    70  }
    71  
    72  // Unsubscribe unsubscribes (removes a subscription to) a given
    73  // ARI application from the provided event source
    74  // Equivalent to DELETE /applications/{applicationName}/subscription
    75  func (a *Application) Unsubscribe(key *ari.Key, eventSource string) error {
    76  	name := key.ID
    77  	err := a.client.del("/applications/"+name+"/subscription", nil, fmt.Sprintf("eventSource=%s", eventSource))
    78  	return errors.Wrapf(err, "Error unsubscribing application '%v' for event source '%v'", name, eventSource)
    79  }