Files
gitea-tea/internal/services/raw.go
T
bircni 81b0c1b99e fix: reconcile the pull request stream
Share one shortSHA helper, keep Markdown raw in view tests through the
markdown preference, and drop rawWindow now that pull request lists use
the paged helpers.

Signed-off-by: bircni <bircni@icloud.com>
2026-10-11 23:56:25 +02:00

58 lines
1.5 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package services
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"gitea.dev/tea/internal/transport"
)
var errInvalidJSON = errors.New("Gitea returned an invalid JSON response")
// getJSON decodes a raw GET response into out.
func getJSON(ctx context.Context, client *transport.Client, endpoint string, out any) error {
_, err := rawJSON(ctx, client, http.MethodGet, endpoint, nil, out)
return err
}
// sendJSON sends payload as a JSON body, or no body when it is nil, and decodes the response
// into out unless out is nil.
func sendJSON(ctx context.Context, client *transport.Client, method, endpoint string, payload, out any) error {
var body io.Reader
if payload != nil {
data, err := json.Marshal(payload)
if err != nil {
return err
}
body = bytes.NewReader(data)
}
_, err := rawJSON(ctx, client, method, endpoint, body, out)
return err
}
func rawJSON(ctx context.Context, client *transport.Client, method, endpoint string, body io.Reader, out any) (http.Header, error) {
resp, err := client.Do(ctx, method, endpoint, body, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if out == nil || resp.StatusCode == http.StatusNoContent {
_, err = io.Copy(io.Discard, resp.Body)
return resp.Header, err
}
if err := json.NewDecoder(resp.Body).Decode(out); err != nil {
if ctx.Err() != nil {
return nil, ctx.Err()
}
return nil, errInvalidJSON
}
return resp.Header, nil
}