Add RAW field to customAPIResponseData

pull/892/head
vanderock1 2025-12-03 14:30:18 +07:00 committed by GitHub
parent 36d5ae023f
commit feeb51e53e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 54 additions and 44 deletions

@ -195,6 +195,7 @@ func (req *CustomAPIRequest) initialize() error {
type customAPIResponseData struct { type customAPIResponseData struct {
JSON decoratedGJSONResult JSON decoratedGJSONResult
Response *http.Response Response *http.Response
RAW strings
} }
type customAPITemplateData struct { type customAPITemplateData struct {
@ -229,52 +230,61 @@ func (data *customAPITemplateData) Subrequest(key string) *customAPIResponseData
} }
func fetchCustomAPIResponse(ctx context.Context, req *CustomAPIRequest) (*customAPIResponseData, error) { func fetchCustomAPIResponse(ctx context.Context, req *CustomAPIRequest) (*customAPIResponseData, error) {
if req == nil || req.URL == "" { if req == nil || req.URL == "" {
return &customAPIResponseData{ return &customAPIResponseData{
JSON: decoratedGJSONResult{gjson.Result{}}, JSON: decoratedGJSONResult{gjson.Result{}},
Response: &http.Response{}, Response: &http.Response{},
}, nil RAW: nil,
} }, nil
}
if req.bodyReader != nil {
req.bodyReader.Seek(0, io.SeekStart) if req.bodyReader != nil {
} req.bodyReader.Seek(0, io.SeekStart)
}
client := ternary(req.AllowInsecure, defaultInsecureHTTPClient, defaultHTTPClient)
resp, err := client.Do(req.httpRequest.WithContext(ctx)) client := defaultHTTPClient
if err != nil { if req.AllowInsecure {
return nil, err client = defaultInsecureHTTPClient
} }
defer resp.Body.Close()
resp, err := client.Do(req.httpRequest.WithContext(ctx))
bodyBytes, err := io.ReadAll(resp.Body) if err != nil {
if err != nil { return nil, err
return nil, err }
} defer resp.Body.Close()
body := strings.TrimSpace(string(bodyBytes)) bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
if !req.SkipJSONValidation && body != "" && !gjson.Valid(body) { return nil, err
if 200 <= resp.StatusCode && resp.StatusCode < 300 { }
truncatedBody, isTruncated := limitStringLength(body, 100)
if isTruncated { body := strings.TrimSpace(string(bodyBytes))
truncatedBody += "... <truncated>"
} if !req.SkipJSONValidation && body != "" && !gjson.Valid(body) {
if 200 <= resp.StatusCode && resp.StatusCode < 300 {
slog.Error("Invalid response JSON in custom API widget", "url", req.httpRequest.URL.String(), "body", truncatedBody) truncatedBody, isTruncated := limitStringLength(body, 100)
return nil, errors.New("invalid response JSON") if isTruncated {
} truncatedBody += "... <truncated>"
}
return nil, fmt.Errorf("%d %s", resp.StatusCode, http.StatusText(resp.StatusCode))
slog.Error("Invalid response JSON in custom API widget",
} "url", req.httpRequest.URL.String(),
"body", truncatedBody,
return &customAPIResponseData{ )
JSON: decoratedGJSONResult{gjson.Parse(body)}, return nil, errors.New("invalid response JSON")
Response: resp, }
}, nil
return nil, fmt.Errorf("%d %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
return &customAPIResponseData{
JSON: decoratedGJSONResult{gjson.Parse(body)},
Response: resp,
RAW: body,
}, nil
} }
func fetchAndRenderCustomAPIRequest( func fetchAndRenderCustomAPIRequest(
primaryReq *CustomAPIRequest, primaryReq *CustomAPIRequest,
subReqs map[string]*CustomAPIRequest, subReqs map[string]*CustomAPIRequest,