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

🔥 feat: Add support for CBOR encoding #3173

Merged
merged 24 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat(client): allow cbor in fiber client
  • Loading branch information
imsk17 committed Nov 6, 2024
commit 7ee95484c89302dea7fa7abf2faf94c1209ff941
24 changes: 24 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type Client struct {
jsonUnmarshal utils.JSONUnmarshal
xmlMarshal utils.XMLMarshal
xmlUnmarshal utils.XMLUnmarshal
cborMarshal utils.CBORMarshal
cborUnmarshal utils.CBORUnmarshal
gaby marked this conversation as resolved.
Show resolved Hide resolved

cookieJar *CookieJar

Expand Down Expand Up @@ -150,6 +152,28 @@ func (c *Client) SetXMLUnmarshal(f utils.XMLUnmarshal) *Client {
return c
}

// CBORMarshal returns xml marshal function in Core.
func (c *Client) CBORMarshal() utils.CBORMarshal {
return c.cborMarshal
}

// SetCBORMarshal Set xml encoder.
func (c *Client) SetCBORMarshal(f utils.CBORMarshal) *Client {
c.cborMarshal = f
return c
}

// CBORUnmarshal returns xml unmarshal function in Core.
func (c *Client) CBORUnmarshal() utils.CBORUnmarshal {
return c.cborUnmarshal
}

// SetCBORUnmarshal Set xml decoder.
func (c *Client) SetCBORUnmarshal(f utils.CBORUnmarshal) *Client {
c.cborUnmarshal = f
return c
}

// TLSConfig returns tlsConfig in client.
// If client don't have tlsConfig, this function will init it.
func (c *Client) TLSConfig() *tls.Config {
Expand Down
25 changes: 25 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"context"
"crypto/tls"
"encoding/hex"
"errors"
"io"
"net"
Expand Down Expand Up @@ -202,6 +203,30 @@ func Test_Client_Marshal(t *testing.T) {
require.Equal(t, errors.New("empty xml"), err)
})

efectn marked this conversation as resolved.
Show resolved Hide resolved
t.Run("set cbor marshal", func(t *testing.T) {
t.Parallel()
bs, _ := hex.DecodeString("f6")
client := New().
SetCBORMarshal(func(_ any) ([]byte, error) {
return bs, nil
})
val, err := client.CBORMarshal()(nil)

require.NoError(t, err)
require.Equal(t, bs, val)
})

t.Run("set cbor marshal error", func(t *testing.T) {
t.Parallel()
client := New().SetCBORMarshal(func(v any) ([]byte, error) {
return nil, errors.New("invalid struct")
})

val, err := client.CBORMarshal()(nil)
require.Nil(t, val)
require.Equal(t, errors.New("invalid struct"), err)
})

t.Run("set xml unmarshal", func(t *testing.T) {
t.Parallel()
client := New().
Expand Down
3 changes: 3 additions & 0 deletions client/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
headerAccept = "Accept"

applicationJSON = "application/json"
applicationCBOR = "application/cbor"
applicationXML = "application/xml"
applicationForm = "application/x-www-form-urlencoded"
multipartFormData = "multipart/form-data"
Expand Down Expand Up @@ -129,6 +130,8 @@ func parserRequestHeader(c *Client, req *Request) error {
req.RawRequest.Header.Set(headerAccept, applicationJSON)
case xmlBody:
req.RawRequest.Header.SetContentType(applicationXML)
case cborBody:
req.RawRequest.Header.SetContentType(applicationCBOR)
case formBody:
req.RawRequest.Header.SetContentType(applicationForm)
case filesBody:
Expand Down
7 changes: 7 additions & 0 deletions client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
formBody
filesBody
rawBody
cborBody
)

var ErrClientNil = errors.New("client can not be nil")
Expand Down Expand Up @@ -337,6 +338,12 @@ func (r *Request) SetXML(v any) *Request {
return r
}

func (r *Request) SetCBOR(v any) *Request {
efectn marked this conversation as resolved.
Show resolved Hide resolved
r.body = v
r.bodyType = cborBody
return r
}

// SetRawBody method sets body with raw data in request.
func (r *Request) SetRawBody(v []byte) *Request {
r.body = v
Expand Down
5 changes: 5 additions & 0 deletions client/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func (r *Response) JSON(v any) error {
return r.client.jsonUnmarshal(r.Body(), v)
}

// CBOR method will unmarshal body to cbor.
func (r *Response) CBOR(v any) error {
efectn marked this conversation as resolved.
Show resolved Hide resolved
return r.client.cborUnmarshal(r.Body(), v)
}

// XML method will unmarshal body to xml.
func (r *Response) XML(v any) error {
return r.client.xmlUnmarshal(r.Body(), v)
Expand Down