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 (
"bytes"
"context"
"encoding/xml"
"fmt"
"io/ioutil"
@ -33,13 +34,13 @@ func NewSOAPClient(endpointURL url.URL) *SOAPClient {
// PerformSOAPAction makes a SOAP request, with the given action.
// inAction and outAction must both be pointers to structs with string fields
// 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)
if err != nil {
return err
}
response, err := client.HTTPClient.Do(&http.Request{
req := &http.Request{
Method: "POST",
URL: &client.EndpointURL,
Header: http.Header{
@ -49,7 +50,9 @@ func (client *SOAPClient) PerformAction(actionNamespace, actionName string, inAc
Body: ioutil.NopCloser(bytes.NewBuffer(requestBytes)),
// Set ContentLength to avoid chunked encoding - some servers might not support it.
ContentLength: int64(len(requestBytes)),
})
}
req = req.WithContext(ctx)
response, err := client.HTTPClient.Do(req)
if err != nil {
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
}
// 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.
func newSOAPEnvelope() *soapEnvelope {
return &soapEnvelope{