Add method SOAPCliet.PerformActionCtx.

This commit is contained in:
John Beisley 2021-07-11 14:15:35 +01:00
parent a666909a2f
commit 6145404ab1

View File

@ -4,6 +4,7 @@ package soap
import ( import (
"bytes" "bytes"
"context"
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -33,13 +34,13 @@ func NewSOAPClient(endpointURL url.URL) *SOAPClient {
// PerformSOAPAction makes a SOAP request, with the given action. // PerformSOAPAction makes a SOAP request, with the given action.
// inAction and outAction must both be pointers to structs with string fields // inAction and outAction must both be pointers to structs with string fields
// only. // only.
func (client *SOAPClient) PerformAction(actionNamespace, actionName string, inAction interface{}, outAction interface{}) error { func (client *SOAPClient) PerformActionCtx(ctx context.Context, actionNamespace, actionName string, inAction interface{}, outAction interface{}) error {
requestBytes, err := encodeRequestAction(actionNamespace, actionName, inAction) requestBytes, err := encodeRequestAction(actionNamespace, actionName, inAction)
if err != nil { if err != nil {
return err return err
} }
response, err := client.HTTPClient.Do(&http.Request{ req := &http.Request{
Method: "POST", Method: "POST",
URL: &client.EndpointURL, URL: &client.EndpointURL,
Header: http.Header{ Header: http.Header{
@ -49,7 +50,9 @@ func (client *SOAPClient) PerformAction(actionNamespace, actionName string, inAc
Body: ioutil.NopCloser(bytes.NewBuffer(requestBytes)), Body: ioutil.NopCloser(bytes.NewBuffer(requestBytes)),
// Set ContentLength to avoid chunked encoding - some servers might not support it. // Set ContentLength to avoid chunked encoding - some servers might not support it.
ContentLength: int64(len(requestBytes)), ContentLength: int64(len(requestBytes)),
}) }
req = req.WithContext(ctx)
response, err := client.HTTPClient.Do(req)
if err != nil { if err != nil {
return fmt.Errorf("goupnp: error performing SOAP HTTP request: %v", err) return fmt.Errorf("goupnp: error performing SOAP HTTP request: %v", err)
} }
@ -79,6 +82,12 @@ func (client *SOAPClient) PerformAction(actionNamespace, actionName string, inAc
return nil return nil
} }
// PerformAction is the legacy version of PerformActionCtx, which uses
// context.Background.
func (client *SOAPClient) PerformAction(actionNamespace, actionName string, inAction interface{}, outAction interface{}) error {
return client.PerformActionCtx(context.Background(), actionNamespace, actionName, inAction, outAction)
}
// newSOAPAction creates a soapEnvelope with the given action and arguments. // newSOAPAction creates a soapEnvelope with the given action and arguments.
func newSOAPEnvelope() *soapEnvelope { func newSOAPEnvelope() *soapEnvelope {
return &soapEnvelope{ return &soapEnvelope{