package gatewayclient import ( "bytes" "context" "encoding/json" "io" "net/http" "time" "git.ipao.vip/rogee/creator-hub/internal/environment" ) const ( DefaultResponseLimit = 1 << 20 LargeResponseLimit = 16 << 20 ) // Call performs one authenticated native gateway request with a bounded response body. func Call(ctx context.Context, target environment.Gateway, method, path string, body any, timeout time.Duration) (int, []byte, error) { return CallWithLimit(ctx, target, method, path, body, timeout, DefaultResponseLimit) } // CallWithLimit performs one authenticated native gateway request with an explicit body limit. func CallWithLimit(ctx context.Context, target environment.Gateway, method, path string, body any, timeout time.Duration, responseLimit int) (int, []byte, error) { callCtx, cancel := context.WithTimeout(ctx, timeout) defer cancel() var payload io.Reader if body != nil { encoded, err := json.Marshal(body) if err != nil { return 0, nil, err } payload = bytes.NewReader(encoded) } request, err := http.NewRequestWithContext(callCtx, method, target.Endpoint+path, payload) if err != nil { return 0, nil, err } request.Header.Set("Authorization", "Bearer "+target.Token) if body != nil { request.Header.Set("Content-Type", "application/json") } response, err := http.DefaultClient.Do(request) if err != nil { return 0, nil, err } defer response.Body.Close() responseBody, err := io.ReadAll(io.LimitReader(response.Body, int64(responseLimit))) return response.StatusCode, responseBody, err }