Rearrange Client.as a helper function.

This narrows the API surface of the `Client` type.
This commit is contained in:
John Beisley 2022-03-29 07:36:18 +01:00
parent ca91893cc0
commit 5d0813cf55
2 changed files with 19 additions and 20 deletions

View File

@ -57,21 +57,6 @@ func New(endpointURL string, opts ...Option) *Client {
}
}
// PerformAction makes a SOAP request, with the given `argsIn` as input
// arguments, and `argsOut` to capture the output arguments into.
// `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(
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(
@ -100,6 +85,22 @@ func (c *Client) Do(
return ParseResponseAction(resp, actionOut)
}
// PerformAction makes a SOAP request, with the given `argsIn` as input
// arguments, and `argsOut` to capture the output arguments into.
// `serviceType` is the SOAP service type URN, `actionName` is the action to
// call.
//
// This is a convenience for calling `Client.Do` without creating `*Action` values.
func PerformAction(
ctx context.Context, c *Client,
serviceType, actionName string,
argsIn, argsOut any,
) error {
actionIn := envelope.NewSendAction(serviceType, actionName, argsIn)
actionOut := &envelope.Action{Args: argsOut}
return c.Do(ctx, actionIn, actionOut)
}
// SetRequestAction updates fields in `req` with the given SOAP action.
// Specifically it sets Body, ContentLength, Method, and the SOAPACTION and
// CONTENT-TYPE headers.

View File

@ -75,7 +75,7 @@ func (fss *fakeSoapServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
func TestDo(t *testing.T) {
func TestPerformAction(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
t.Cleanup(cancel)
@ -91,12 +91,10 @@ func TestDo(t *testing.T) {
c := New(ts.URL + "/endpointpath")
reqAction := envelope.NewSendAction("http://example.com/endpointns", "Foo",
&ActionArgs{Name: "World"})
reply := &ActionReply{}
replyAction := &envelope.Action{Args: reply}
if err := c.Do(ctx, reqAction, replyAction); err != nil {
if err := PerformAction(ctx, c, "http://example.com/endpointns", "Foo",
&ActionArgs{Name: "World"}, reply); err != nil {
t.Errorf("got error: %v, want success", err)
} else {
if got, want := reply.Greeting, "Hello, World!"; got != want {