Clean up SOAP client API.

This commit is contained in:
John Beisley 2022-03-26 18:17:39 +00:00
parent d4fdaef967
commit 767078168a
2 changed files with 33 additions and 13 deletions

View File

@ -55,18 +55,32 @@ func New(endpointURL string, opts ...Option) *Client {
} }
} }
// PerformAction makes a SOAP request, with the given action values to provide // PerformAction makes a SOAP request, with the given `argsIn` as input
// arguments (`args`) and capture the `reply` into. If `client` is nil, then // arguments, and `argsOut` to capture the output arguments into.
// http.DefaultClient is used. // `serviceType` is the SOAP service type URN, `actionName` is the action to
// call.
//
// This is a convenience for calling `c.Do` without creating `*Action` values.
func (c *Client) PerformAction( func (c *Client) PerformAction(
ctx context.Context, serviceType, actionName string,
argsIn, argsOut any,
) error {
actionIn := envelope.NewSendAction(serviceType, actionName, argsIn)
actionOut := &envelope.Action{Args: argsOut}
return c.Do(ctx, actionIn, actionOut)
}
// PerformAction makes a SOAP request, with the given action values to provide
// arguments (`args`) and capture the `reply` into.
func (c *Client) Do(
ctx context.Context, ctx context.Context,
args, reply *envelope.Action, actionIn, actionOut *envelope.Action,
) error { ) error {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.endpointURL, nil) req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.endpointURL, nil)
if err != nil { if err != nil {
return err return err
} }
if err := SetRequestAction(req, args); err != nil { if err := SetRequestAction(req, actionIn); err != nil {
return err return err
} }
@ -81,15 +95,18 @@ func (c *Client) PerformAction(
resp.Status, resp.StatusCode) resp.Status, resp.StatusCode)
} }
return ParseResponseAction(resp, reply) return ParseResponseAction(resp, actionOut)
} }
// SetRequestAction updates fields in `req` with the given SOAP action. // SetRequestAction updates fields in `req` with the given SOAP action.
// Specifically it sets Body, ContentLength, Method, and the SOAPACTION and // Specifically it sets Body, ContentLength, Method, and the SOAPACTION and
// CONTENT-TYPE headers. // CONTENT-TYPE headers.
func SetRequestAction(req *http.Request, args *envelope.Action) error { func SetRequestAction(
req *http.Request,
actionIn *envelope.Action,
) error {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err := envelope.Write(buf, args) err := envelope.Write(buf, actionIn)
if err != nil { if err != nil {
return fmt.Errorf("encoding envelope: %w", err) return fmt.Errorf("encoding envelope: %w", err)
} }
@ -98,7 +115,7 @@ func SetRequestAction(req *http.Request, args *envelope.Action) error {
req.ContentLength = int64(buf.Len()) req.ContentLength = int64(buf.Len())
req.Method = http.MethodPost req.Method = http.MethodPost
req.Header["SOAPACTION"] = []string{fmt.Sprintf( req.Header["SOAPACTION"] = []string{fmt.Sprintf(
`"%s#%s"`, args.XMLName.Space, args.XMLName.Local)} `"%s#%s"`, actionIn.XMLName.Space, actionIn.XMLName.Local)}
req.Header["CONTENT-TYPE"] = []string{`text/xml; charset="utf-8"`} req.Header["CONTENT-TYPE"] = []string{`text/xml; charset="utf-8"`}
return nil return nil
@ -107,7 +124,10 @@ func SetRequestAction(req *http.Request, args *envelope.Action) error {
// ParseResponse extracts a parsed action from an HTTP response. // ParseResponse extracts a parsed action from an HTTP response.
// The caller is responsible for calling resp.Body.Close(), but this function // The caller is responsible for calling resp.Body.Close(), but this function
// will consume the entire response body. // will consume the entire response body.
func ParseResponseAction(resp *http.Response, reply *envelope.Action) error { func ParseResponseAction(
resp *http.Response,
actionOut *envelope.Action,
) error {
if resp.Body == nil { if resp.Body == nil {
return errors.New("missing response body") return errors.New("missing response body")
} }
@ -117,7 +137,7 @@ func ParseResponseAction(resp *http.Response, reply *envelope.Action) error {
return fmt.Errorf("reading response body: %w", err) return fmt.Errorf("reading response body: %w", err)
} }
if err := envelope.Read(buf, reply); err != nil { if err := envelope.Read(buf, actionOut); err != nil {
if _, ok := err.(*envelope.Fault); ok { if _, ok := err.(*envelope.Fault); ok {
// Parsed cleanly, got SOAP fault. // Parsed cleanly, got SOAP fault.
return err return err

View File

@ -76,7 +76,7 @@ func (fss *fakeSoapServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
func TestPerformAction(t *testing.T) { func TestDo(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
t.Cleanup(cancel) t.Cleanup(cancel)
@ -101,7 +101,7 @@ func TestPerformAction(t *testing.T) {
reply := &ActionReply{} reply := &ActionReply{}
replyAction := &envelope.Action{Args: reply} replyAction := &envelope.Action{Args: reply}
if err := c.PerformAction(ctx, reqAction, replyAction); err != nil { if err := c.Do(ctx, reqAction, replyAction); err != nil {
t.Errorf("got error: %v, want success", err) t.Errorf("got error: %v, want success", err)
} else { } else {
if got, want := reply.Greeting, "Hello, World!"; got != want { if got, want := reply.Greeting, "Hello, World!"; got != want {