Created
August 7, 2021 12:43
-
-
Save rodrigorodriguescosta/b180d0b11e406b0a38f1aa96ad702c77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils | |
import ( | |
"bytes" | |
"comps/errors" | |
"encoding/json" | |
"fmt" | |
"github.com/sirupsen/logrus" | |
"io/ioutil" | |
"net/http" | |
) | |
//Get realiza uma request GET e joga o resultado da request para a variavel dest | |
func (r requestsUtil) Get(url string, headers map[string]string, dest interface{}) error { | |
return r.Request(http.MethodGet, url, headers, nil, dest) | |
} | |
//Post realiza uma request POST e joga o resultado da request para a variavel dest | |
func (r requestsUtil) Post(url string, headers map[string]string, params, dest interface{}) (err error) { | |
return r.Request(http.MethodPost, url, headers, params, dest) | |
} | |
//Put realiza uma request PUT e joga o resultado da request para a variavel dest | |
func (r requestsUtil) Put(url string, headers map[string]string, params, dest interface{}) (err error) { | |
return r.Request(http.MethodPut, url, headers, params, dest) | |
} | |
//Request realiza uma request e joga o resultado da request para o parametro `dest` | |
func (r requestsUtil) Request(method, url string, headers map[string]string, params, dest interface{}) (err error) { | |
logrus.WithFields(map[string]interface{}{ | |
"Method": method, | |
"Url": url, | |
}).Info("Realizando request") | |
var paramsBytes []byte | |
if params!=nil { | |
paramsBytes, err = json.Marshal(params) | |
if err != nil { | |
return | |
} | |
} | |
// cria o objeto request | |
req, err := http.NewRequest(method, url, bytes.NewBuffer(paramsBytes)) | |
if err != nil { | |
logrus.WithFields(map[string]interface{}{ | |
"Error": err, | |
}).Error("Erro ao criar objeto request") | |
return | |
} | |
for key, value := range headers { | |
req.Header.Set(key, value) | |
} | |
// realiza a request | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
logrus.WithFields(map[string]interface{}{ | |
"Error": err, | |
}).Error("Erro ao realizar request") | |
return | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
logrus.WithFields(map[string]interface{}{ | |
"Error": err, | |
}).Error("Erro ao ler resposta") | |
} | |
if resp.StatusCode >= 400 { | |
err = errors.NewError(fmt.Sprintf("Status Code %d , Message: %s", resp.StatusCode, string(body))) | |
return | |
} | |
// grava a resposta na variavel dest | |
if dest != nil { | |
err = json.Unmarshal(body, dest) | |
if err != nil { | |
logrus.WithFields(map[string]interface{}{ | |
"Error": err, | |
}).Error("Erro ao fazer unmarshal de json da request") | |
return err | |
} | |
} | |
return nil | |
} | |
//GraphQl realiza uma request POST para uma api e graphql e joga o resultado da request para a variavel dest | |
/* exemplo do parametro query | |
jsonData := map[string]string{ | |
"query": ` | |
{ | |
people { | |
firstname, | |
lastname, | |
website | |
} | |
} | |
`, | |
} | |
*/ | |
func (r requestsUtil) GraphQl(url string, headers map[string]string, query map[string]string, dest interface{}) (err error) { | |
return r.Post(url, headers, query, dest) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment