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

Corrige erro ao enviar corpo de requisição vazio e adiciona o status 204 ao checar se resposta foi sucedida #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 12 additions & 4 deletions gerencianet/pix/requester_pix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -58,9 +59,6 @@ func authenticate(requester *requester) (bool, error) {
}

func (requester requester) request(endpoint string, httpVerb string, requestParams map[string]string, body map[string]interface{}) (string, error) {
requestBody := new(bytes.Buffer)
json.NewEncoder(requestBody).Encode(body)

_, authErr := authenticate(&requester)
if authErr != nil {
_, authErr = authenticate(&requester)
Expand All @@ -71,6 +69,16 @@ func (requester requester) request(endpoint string, httpVerb string, requestPara

route := getRoute(endpoint, requestParams)
route += getQueryString(requestParams)

var requestBody io.Reader
if body != nil {
requestBodyBytes := new(bytes.Buffer)
err := json.NewEncoder(requestBodyBytes).Encode(body)
if err != nil {
return "", err
}
requestBody = requestBodyBytes
}
req, _ := http.NewRequest(httpVerb, requester.url+route, requestBody)

if ( httpVerb == "POST" || httpVerb == "PUT" ) && body != nil {
Expand All @@ -90,7 +98,7 @@ func (requester requester) request(endpoint string, httpVerb string, requestPara
reqResp, _ := ioutil.ReadAll(res.Body)
response := string(reqResp)

if (res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated) {
if (res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated && res.StatusCode != http.StatusNoContent) {
return "", errors.New(response)
}

Expand Down