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

Adding ability to customize the XML marshal/unmarshal functions #481 #484

Merged
merged 1 commit into from Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions client.go
Expand Up @@ -10,6 +10,7 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -114,6 +115,8 @@ type Client struct {
RetryAfter RetryAfterFunc
JSONMarshal func(v interface{}) ([]byte, error)
JSONUnmarshal func(data []byte, v interface{}) error
XMLMarshal func(v interface{}) ([]byte, error)
XMLUnmarshal func(data []byte, v interface{}) error

// HeaderAuthorizationKey is used to set/access Request Authorization header
// value when `SetAuthToken` option is used.
Expand Down Expand Up @@ -1074,13 +1077,16 @@ func createClient(hc *http.Client) *Client {
Cookies: make([]*http.Cookie, 0),
RetryWaitTime: defaultWaitTime,
RetryMaxWaitTime: defaultMaxWaitTime,
PathParams: make(map[string]string),
JSONMarshal: json.Marshal,
JSONUnmarshal: json.Unmarshal,
XMLMarshal: xml.Marshal,
XMLUnmarshal: xml.Unmarshal,
HeaderAuthorizationKey: http.CanonicalHeaderKey("Authorization"),
jsonEscapeHTML: true,
httpClient: hc,
debugBodySizeLimit: math.MaxInt32,
PathParams: make(map[string]string),

jsonEscapeHTML: true,
httpClient: hc,
debugBodySizeLimit: math.MaxInt32,
}

// Logger
Expand Down
3 changes: 1 addition & 2 deletions middleware.go
Expand Up @@ -6,7 +6,6 @@ package resty

import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -462,7 +461,7 @@ func handleRequestBody(c *Client, r *Request) (err error) {
return
}
} else if IsXMLType(contentType) && (kind == reflect.Struct) {
bodyBytes, err = xml.Marshal(r.Body)
bodyBytes, err = c.XMLMarshal(r.Body)
if err != nil {
return
}
Expand Down
3 changes: 1 addition & 2 deletions util.go
Expand Up @@ -6,7 +6,6 @@ package resty

import (
"bytes"
"encoding/xml"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -109,7 +108,7 @@ func Unmarshalc(c *Client, ct string, b []byte, d interface{}) (err error) {
if IsJSONType(ct) {
err = c.JSONUnmarshal(b, d)
} else if IsXMLType(ct) {
err = xml.Unmarshal(b, d)
err = c.XMLUnmarshal(b, d)
}

return
Expand Down