Skip to content

Commit

Permalink
Modified json ingress code changes with variadic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AsabuHere committed Oct 19, 2023
1 parent 6e3ede3 commit 57ce701
Showing 1 changed file with 15 additions and 28 deletions.
43 changes: 15 additions & 28 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,9 @@ func extractContentTypeHeader(headers map[string]interface{}) (cType string){
return headerType.(string)
}

func decodeJsonPayload(jsonBody string)(s []byte){
prefixTrimmedString := jsonBody[2:]
suffixTrimmedString := prefixTrimmedString[:len(prefixTrimmedString)-2]
finalJsonPayload := jsonPrefix + suffixTrimmedString + jsonSuffix
finalJsonPayloadArray := []byte(finalJsonPayload)
return finalJsonPayloadArray
}

const (
urlEncodedContentType = "application/x-www-form-urlencoded"
jsonContentType = "application/json"
jsonPrefix = "{"
jsonSuffix = "}"
keepZeros = true
delimiter = '.'
escapee = '\\'
Expand Down Expand Up @@ -109,20 +99,24 @@ func (c *Client) doWithErr(req *http.Request) (*http.Response, error) {

// SendRequest verifies, constructs, and authorizes an HTTP request.
func (c *Client) SendRequest(method string, rawURL string, data url.Values,
headers map[string]interface{}) (*http.Response, error) {
contentType := extractContentTypeHeader(headers)
headers map[string]interface{}, body ...byte) (*http.Response, error) {

contentType := extractContentTypeHeader(headers)

u, err := url.Parse(rawURL)
if err != nil {
return nil, err
}

valueReader := &strings.Reader{}
goVersion := runtime.Version()
var req *http.Request

if method == http.MethodGet {
//For HTTP GET Method there are no body parameters. All other parameters like query, path etc
// are added as information in the url itself. Also while Content-Type is json, we are sending
// json body. In that case, data variable conatins all other parameters than body, which is the
//same case as GET method. In that case as well all parameters will be added to url
if method == http.MethodGet || contentType == jsonContentType{
if data != nil {
v, _ := form.EncodeToStringWith(data, delimiter, escapee, keepZeros)
regex := regexp.MustCompile(`\.\d+`)
Expand All @@ -132,23 +126,16 @@ func (c *Client) SendRequest(method string, rawURL string, data url.Values,
}
}

//data is already processed and information will be added to u(the url) in the
//previous step. Now body will solely contain json payload
if contentType == jsonContentType {
var jsonData []byte
var err error
var encodedString string
for _, value := range data {
jsonData, err = json.Marshal(value[0])
encodedString, err = url.QueryUnescape(string(jsonData))
if err != nil {
return nil, err
}
}
jsonBody := decodeJsonPayload(encodedString)
req, err = http.NewRequest(method, u.String(), bytes.NewBuffer(jsonBody))
req, err = http.NewRequest(method, u.String(), bytes.NewBuffer(body))
if err != nil {
return nil, err
}
} else {
//Here the HTTP POST methods which is not having json content type are processed
//All the values will be added in data and encoded (all body, query, path parameters)
if method == http.MethodPost {
valueReader = strings.NewReader(data.Encode())
}
Expand Down Expand Up @@ -188,4 +175,4 @@ func (c *Client) SetAccountSid(sid string) {
// Returns the Account SID.
func (c *Client) AccountSid() string {
return c.accountSid
}
}

0 comments on commit 57ce701

Please sign in to comment.