This commit is contained in:
kandrusyak
2023-09-25 21:57:43 +03:00
parent 1fb8c903dc
commit 075fbf89b0
3 changed files with 42 additions and 11 deletions

View File

@@ -3,11 +3,12 @@ package api
import (
"bytes"
"encoding/json"
"github.com/labstack/echo/v4"
"io"
"net/http"
)
func PostRequest(url string, model any, header http.Header) ([]byte, error, *http.Response) {
func PostRequest(url string, model any, header http.Header, c echo.Context) ([]byte, error, *http.Response) {
uJson, err := json.Marshal(model)
if err != nil {
@@ -19,6 +20,13 @@ func PostRequest(url string, model any, header http.Header) ([]byte, error, *htt
req.Header = header
req.Header.Set("Content-Type", "application/json")
debug, err := json.Marshal(req)
if err != nil {
c.Logger().Debug("Request---")
c.Logger().Debug(string(debug))
}
resp, err := client.Do(req)
if err != nil {
@@ -31,10 +39,17 @@ func PostRequest(url string, model any, header http.Header) ([]byte, error, *htt
return nil, err, nil
}
debug, err = json.Marshal(resp)
if err != nil {
c.Logger().Debug("Response---")
c.Logger().Debug(string(debug))
}
return data, nil, resp
}
func GetRequest(url string, header http.Header, args ...string) ([]byte, error, *http.Response) {
func GetRequest(url string, header http.Header, c echo.Context, args ...string) ([]byte, error, *http.Response) {
client := http.Client{}
req, err := http.NewRequest("GET", url, nil)
if len(args) > 0 {
@@ -42,6 +57,14 @@ func GetRequest(url string, header http.Header, args ...string) ([]byte, error,
}
req.Header = header
req.Header.Set("Content-Type", "application/json")
debug, err := json.Marshal(req)
if err != nil {
c.Logger().Debug("Request---")
c.Logger().Debug(string(debug))
}
resp, err := client.Do(req)
if err != nil {
@@ -54,6 +77,11 @@ func GetRequest(url string, header http.Header, args ...string) ([]byte, error,
return nil, err, nil
}
if err != nil {
c.Logger().Debug("Response---")
c.Logger().Debug(string(debug))
}
return data, nil, resp
}

View File

@@ -7,13 +7,14 @@ import (
"encoding/json"
"errors"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"net/http"
)
func GetPerson(id uuid.UUID, header http.Header) (*model.PersonalInformationApi, error) {
func GetPerson(id uuid.UUID, header http.Header, c echo.Context) (*model.PersonalInformationApi, error) {
data, err, _ := api.GetRequest(
config.Env.MSHosts.PersonsHost+"/persons/"+id.String(),
header)
header, c)
if err != nil {
return nil, errors.New("error Request to Personal Information Service")
@@ -30,11 +31,12 @@ func GetPerson(id uuid.UUID, header http.Header) (*model.PersonalInformationApi,
return pi, nil
}
func CreatePerson(person model.PersonalInformationApi, header http.Header) (*model.PersonalInformationApi, error) {
func CreatePerson(person model.PersonalInformationApi, header http.Header, c echo.Context) (*model.PersonalInformationApi, error) {
data, err, _ := api.PostRequest(
config.Env.MSHosts.PersonsHost+"/persons/",
person,
header,
c,
)
if err != nil {
return nil, errors.New("error Request to Personal Information Service")
@@ -51,7 +53,7 @@ func CreatePerson(person model.PersonalInformationApi, header http.Header) (*mod
return pi, nil
}
func GetPersons(ids []uuid.UUID, header http.Header) (map[uuid.UUID]*model.PersonalInformationApi, error) {
func GetPersons(ids []uuid.UUID, header http.Header, c echo.Context) (map[uuid.UUID]*model.PersonalInformationApi, error) {
var persons []model.PersonalInformationApi
personsMap := make(map[uuid.UUID]*model.PersonalInformationApi)
@@ -60,7 +62,7 @@ func GetPersons(ids []uuid.UUID, header http.Header) (map[uuid.UUID]*model.Perso
&map[string][]uuid.UUID{
"ids": ids,
},
header)
header, c)
if err != nil || req.StatusCode != 200 {
return nil, err

View File

@@ -7,17 +7,18 @@ import (
"encoding/json"
"errors"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"net/http"
"net/url"
"strconv"
)
func GetUser(ID uint, header http.Header) (*model.User, error) {
func GetUser(ID uint, header http.Header, c echo.Context) (*model.User, error) {
id := strconv.Itoa(int(ID))
data, err, _ := api.GetRequest(
config.Env.MSHosts.UsersHost+"/"+id,
header)
header, c)
if err != nil {
return nil, errors.New("error Request to Users Service")
@@ -34,7 +35,7 @@ func GetUser(ID uint, header http.Header) (*model.User, error) {
return u, nil
}
func GetUsers(ids []uuid.UUID, header http.Header) (map[uuid.UUID]model.User, error) {
func GetUsers(ids []uuid.UUID, header http.Header, c echo.Context) (map[uuid.UUID]model.User, error) {
var users []model.User
var usersMap = make(map[uuid.UUID]model.User)
Url, err := url.Parse(config.Env.MSHosts.UsersHost)
@@ -48,7 +49,7 @@ func GetUsers(ids []uuid.UUID, header http.Header) (map[uuid.UUID]model.User, er
data, err, req := api.GetRequest(
Url.String(),
header)
header, c)
if err != nil || req.StatusCode != 200 {
return nil, err