Browse Source

added support for calling actions with arguments

pull/4/head
sberk42 4 years ago
parent
commit
86d59aecd8
  1. 30
      fritzbox_upnp/service.go

30
fritzbox_upnp/service.go

@ -18,6 +18,7 @@ package fritzbox_upnp
import ( import (
"encoding/xml" "encoding/xml"
"errors" "errors"
"bytes"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -96,6 +97,12 @@ type Action struct {
ArgumentMap map[string]*Argument // Map of arguments indexed by .Name ArgumentMap map[string]*Argument // Map of arguments indexed by .Name
} }
// An Inüut Argument to pass to an action
type ActionArgument struct {
Name string
Value string
}
// structs to unmarshal SOAP faults // structs to unmarshal SOAP faults
type SoapEnvelope struct { type SoapEnvelope struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"` XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
@ -255,11 +262,19 @@ func (d *Device) fillServices(r *Root) error {
const SoapActionXML = `<?xml version="1.0" encoding="utf-8"?>` + const SoapActionXML = `<?xml version="1.0" encoding="utf-8"?>` +
`<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">` + `<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">` +
`<s:Body><u:%s xmlns:u=%s /></s:Body>` + `<s:Body><u:%s xmlns:u=%s>%s</u:%s xmlns:u=%s></s:Body>` +
`</s:Envelope>` `</s:Envelope>`
func (a *Action) createCallHttpRequest() (*http.Request, error) { const SoapActionParamXML = `<%s>%s</%s>`
bodystr := fmt.Sprintf(SoapActionXML, a.Name, a.service.ServiceType)
func (a *Action) createCallHttpRequest(actionArgs []ActionArgument) (*http.Request, error) {
argsString := ""
for _, aa := range actionArgs{
var buf bytes.Buffer
xml.EscapeText(&buf, []byte(aa.Value))
argsString += fmt.Sprintf(SoapActionParamXML, aa.Name, buf.String(), aa.Name)
}
bodystr := fmt.Sprintf(SoapActionXML, a.Name, a.service.ServiceType, argsString, a.Name, a.service.ServiceType)
url := a.service.Device.root.BaseUrl + a.service.ControlUrl url := a.service.Device.root.BaseUrl + a.service.ControlUrl
body := strings.NewReader(bodystr) body := strings.NewReader(bodystr)
@ -278,9 +293,12 @@ func (a *Action) createCallHttpRequest() (*http.Request, error) {
} }
// Call an action. // Call an action.
// Currently only actions without input arguments are supported.
func (a *Action) Call() (Result, error) { func (a *Action) Call() (Result, error) {
req, err := a.createCallHttpRequest() return a.CallWithParams([]ActionArgument{});
}
// Currently only actions without input arguments are supported.
func (a *Action) CallWithArguments(actionArgs []ActionArgument) (Result, error) {
req, err := a.createCallHttpRequest(actionArgs)
if err != nil { if err != nil {
return nil, err return nil, err
@ -304,7 +322,7 @@ func (a *Action) Call() (Result, error) {
return nil, err return nil, err
} }
req, err = a.createCallHttpRequest() req, err = a.createCallHttpRequest(actionArgs)
if err != nil { if err != nil {
return nil, err return nil, err
} }

Loading…
Cancel
Save