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

     1  package native
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/CyCoreSystems/ari"
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  // Asterisk provides the ARI Asterisk accessors for a native client
    11  type Asterisk struct {
    12  	client *Client
    13  }
    14  
    15  // Logging provides the ARI Asterisk Logging accessors for a native client
    16  func (a *Asterisk) Logging() ari.Logging {
    17  	return &Logging{a.client}
    18  }
    19  
    20  // Modules provides the ARI Asterisk Modules accessors for a native client
    21  func (a *Asterisk) Modules() ari.Modules {
    22  	return &Modules{a.client}
    23  }
    24  
    25  // Config provides the ARI Asterisk Config accessors for a native client
    26  func (a *Asterisk) Config() ari.Config {
    27  	return &Config{a.client}
    28  }
    29  
    30  /*
    31  	conn    *Conn
    32  	logging ari.Logging
    33  	modules ari.Modules
    34  	config  ari.Config
    35  }
    36  */
    37  
    38  // Info returns various data about the Asterisk system
    39  // Equivalent to GET /asterisk/info
    40  func (a *Asterisk) Info(key *ari.Key) (*ari.AsteriskInfo, error) {
    41  	var m ari.AsteriskInfo
    42  	return &m, errors.Wrap(
    43  		a.client.get("/asterisk/info", &m),
    44  		"failed to get asterisk info",
    45  	)
    46  }
    47  
    48  // AsteriskVariables provides the ARI Variables accessors for server-level variables
    49  type AsteriskVariables struct {
    50  	client *Client
    51  }
    52  
    53  // Variables returns the variables interface for the Asterisk server
    54  func (a *Asterisk) Variables() ari.AsteriskVariables {
    55  	return &AsteriskVariables{a.client}
    56  }
    57  
    58  // Get returns the value of the given global variable
    59  // Equivalent to GET /asterisk/variable
    60  func (a *AsteriskVariables) Get(key *ari.Key) (string, error) {
    61  	var m struct {
    62  		Value string `json:"value"`
    63  	}
    64  	err := a.client.get(fmt.Sprintf("/asterisk/variable?variable=%s", key.ID), &m)
    65  	if err != nil {
    66  		return "", errors.Wrapf(err, "Error getting asterisk variable '%v'", key.ID)
    67  	}
    68  	return m.Value, nil
    69  }
    70  
    71  // Set sets a global channel variable
    72  // (Equivalent to POST /asterisk/variable)
    73  func (a *AsteriskVariables) Set(key *ari.Key, value string) (err error) {
    74  	req := struct {
    75  		Variable string `json:"variable"`
    76  		Value    string `json:"value,omitempty"`
    77  	}{
    78  		Variable: key.ID,
    79  		Value:    value,
    80  	}
    81  
    82  	return errors.Wrapf(
    83  		a.client.post("/asterisk/variable", nil, &req),
    84  		"Error setting asterisk variable '%s' to '%s'", key.ID, value,
    85  	)
    86  }