Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add XML support #79

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions body.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package requests
import (
"bytes"
"encoding/json"
"encoding/xml"
"io"
"net/url"
"os"
Expand Down Expand Up @@ -57,6 +58,17 @@ func BodyJSON(v any) BodyGetter {
}
}

// BodyXML is a BodyGetter that marshals a XML object.
func BodyXML(v any) BodyGetter {
return func() (io.ReadCloser, error) {
b, err := xml.Marshal(v)
if err != nil {
return nil, err
}
return core.RC(bytes.NewReader(b)), nil
}
}

// BodyForm is a BodyGetter that builds an encoded form body.
func BodyForm(data url.Values) BodyGetter {
return func() (r io.ReadCloser, err error) {
Expand Down
4 changes: 2 additions & 2 deletions builder_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ import (
// If no validator has been added, Builder will use [DefaultValidator].
//
// Set a handler for a response with [Builder.Handle]
// or use the built in [Builder.ToHeaders], [Builder.ToJSON], [Builder.ToString],
// [Builder.ToBytesBuffer], or [Builder.ToWriter].
// or use the built in [Builder.ToHeaders], [Builder.ToJSON], [Builder.ToXML],
// [Builder.ToString], [Builder.ToBytesBuffer], or [Builder.ToWriter].
//
// [Builder.Fetch] creates an http.Request with [Builder.Request]
// and validates and handles it with [Builder.Do].
Expand Down
13 changes: 13 additions & 0 deletions builder_extras.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ func (rb *Builder) BodyJSON(v any) *Builder {
ContentType("application/json")
}

// BodyXML sets the Builder's request body to the marshaled XML.
// It also sets ContentType to "application/xml".
func (rb *Builder) BodyXML(v any) *Builder {
return rb.
Body(BodyXML(v)).
ContentType("application/xml")
}

// BodyForm sets the Builder's request body to the encoded form.
// It also sets the ContentType to "application/x-www-form-urlencoded".
func (rb *Builder) BodyForm(data url.Values) *Builder {
Expand Down Expand Up @@ -151,6 +159,11 @@ func (rb *Builder) ToJSON(v any) *Builder {
return rb.Handle(ToJSON(v))
}

// ToXML sets the Builder to decode a response as an XML object
func (rb *Builder) ToXML(v any) *Builder {
return rb.Handle(ToXML(v))
}

// ToString sets the Builder to write the response body to the provided string pointer.
func (rb *Builder) ToString(sp *string) *Builder {
return rb.Handle(ToString(sp))
Expand Down
15 changes: 15 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"encoding/json"
"encoding/xml"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -53,6 +54,20 @@ func ToJSON(v any) ResponseHandler {
}
}

// ToXML decodes a response as an XML object.
func ToXML(v any) ResponseHandler {
return func(res *http.Response) error {
data, err := io.ReadAll(res.Body)
if err != nil {
return err
}
if err = xml.Unmarshal(data, v); err != nil {
return err
}
return nil
}
}

// ToString writes the response body to the provided string pointer.
func ToString(sp *string) ResponseHandler {
return func(res *http.Response) error {
Expand Down
7 changes: 7 additions & 0 deletions validator_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ var ErrInvalidHandled = errors.New("handled recovery from invalid response")
func ErrorJSON(v any) ResponseHandler {
return ValidatorHandler(DefaultValidator, ToJSON(v))
}

// ErrorXML is a ValidatorHandler that applies DefaultValidator
// and decodes the response as an XML object
// if the DefaultValidator check fails.
func ErrorXML(v any) ResponseHandler {
return ValidatorHandler(DefaultValidator, ToXML(v))
}