github.com/CyCoreSystems/ari@v4.8.4+incompatible/client/native/config.go (about) 1 package native 2 3 import ( 4 "github.com/CyCoreSystems/ari" 5 "github.com/pkg/errors" 6 ) 7 8 // Config provides the ARI Configuration accessors for a native client 9 type Config struct { 10 client *Client 11 } 12 13 // Get gets a lazy handle to a configuration object 14 func (c *Config) Get(key *ari.Key) *ari.ConfigHandle { 15 return ari.NewConfigHandle(key, c) 16 } 17 18 // Data retrieves the state of a configuration object 19 func (c *Config) Data(key *ari.Key) (*ari.ConfigData, error) { 20 if key == nil || key.ID == "" { 21 return nil, errors.New("config key not supplied") 22 } 23 24 class, kind, name, err := ari.ParseConfigID(key.ID) 25 if err != nil { 26 return nil, errors.Wrap(err, "failed to parse configuration key") 27 } 28 29 data := &ari.ConfigData{ 30 Key: c.client.stamp(key), 31 Class: class, 32 Type: kind, 33 Name: name, 34 } 35 err = c.client.get("/asterisk/config/dynamic/"+key.ID, &data.Fields) 36 if err != nil { 37 return nil, dataGetError(err, "config", "%v", key.ID) 38 } 39 return data, nil 40 } 41 42 // Update updates the given configuration object 43 func (c *Config) Update(key *ari.Key, tuples []ari.ConfigTuple) (err error) { 44 class, kind, name, err := ari.ParseConfigID(key.ID) 45 if err != nil { 46 return errors.Wrap(err, "failed to parse key") 47 } 48 cfgList := ari.ConfigTupleList{} 49 cfgList.Fields = append(cfgList.Fields, tuples...) 50 return c.client.put("/asterisk/config/dynamic/"+class+"/"+kind+"/"+name, nil, &cfgList) 51 } 52 53 // Delete deletes the configuration object 54 func (c *Config) Delete(key *ari.Key) error { 55 class, kind, name, err := ari.ParseConfigID(key.ID) 56 if err != nil { 57 return errors.Wrap(err, "failed to parse key") 58 } 59 return c.client.del("/asterisk/config/dynamic/"+class+"/"+kind+"/"+name, nil, "") 60 }