InternalServerAPI에 추가 header를 인자로 받도록 추가

This commit is contained in:
2023-11-21 16:52:06 +09:00
parent 49dabb35f0
commit 6ad727c728

View File

@ -791,22 +791,34 @@ func (hc *HttpApiBroker) call(funcname string, w http.ResponseWriter, r *http.Re
}
}
func CallInternalServiceAPI[T any](url string, apitoken string, method string, data T) error {
reqURL := fmt.Sprintf("%s/api?call=%s", url, method)
func CallInternalServiceAPI[T any](url string, apitoken string, method string, data T, headers ...string) error {
tempHeader := make(http.Header)
tempHeader.Set("MG-X-API-TOKEN", apitoken)
tempHeader.Set("Content-Type", "application/gob")
for i := 1; i < len(headers); i += 2 {
tempHeader.Set(headers[i-1], headers[i])
}
buff := new(bytes.Buffer)
ct := tempHeader.Get("Content-Type")
if ct == "application/gob" {
enc := gob.NewEncoder(buff)
enc.Encode(data)
} else if ct == "application/json" {
enc := json.NewEncoder(buff)
enc.Encode(data)
}
reqURL := fmt.Sprintf("%s/api?call=%s", url, method)
req, err := http.NewRequest("POST", reqURL, buff)
if err != nil {
return err
}
req.Header.Set("MG-X-API-TOKEN", apitoken)
req.Header.Set("Content-Type", "application/gob")
req.Header = tempHeader
resp, err := http.DefaultClient.Do(req)
defer func() {
if resp != nil && resp.Body != nil {
resp.Body.Close()
@ -822,21 +834,32 @@ func CallInternalServiceAPI[T any](url string, apitoken string, method string, d
return err
}
func CallInternalServiceAPIAs[Tin any, Tout any](url string, apitoken string, method string, data Tin, out *Tout) error {
reqURL := fmt.Sprintf("%s/api?call=%s", url, method)
func CallInternalServiceAPIAs[Tin any, Tout any](url string, apitoken string, method string, data Tin, out *Tout, headers ...string) error {
tempHeader := make(http.Header)
tempHeader.Set("MG-X-API-TOKEN", apitoken)
tempHeader.Set("Content-Type", "application/gob")
for i := 1; i < len(headers); i += 2 {
tempHeader.Set(headers[i-1], headers[i])
}
buff := new(bytes.Buffer)
ct := tempHeader.Get("Content-Type")
if ct == "application/gob" {
enc := gob.NewEncoder(buff)
enc.Encode(data)
} else if ct == "application/json" {
enc := json.NewEncoder(buff)
enc.Encode(data)
}
reqURL := fmt.Sprintf("%s/api?call=%s", url, method)
req, err := http.NewRequest("POST", reqURL, buff)
if err != nil {
return err
}
req.Header.Set("MG-X-API-TOKEN", apitoken)
req.Header.Set("Content-Type", "application/gob")
req.Header = tempHeader
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err