okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

Aly Raffaufrefactor(client): use generated protocol requests57b8eb0

main
2.0 KiB66 linesraw
1package daemon
2
3import (
4	"context"
5	"encoding/json"
6	"errors"
7	"net/http"
8	"time"
9)
10
11// executeMutation validates after requestBytes finishes authentication recovery.
12// Covered operations apply receipt recovery around this single attempt.
13func executeMutation(ctx context.Context, conn *Connection, operation operation, statuses []int, decode func([]byte, int) error) error {
14	if conn == nil {
15		return errors.New("not connected to Albedo")
16	}
17	if ctx == nil {
18		ctx = context.Background()
19	}
20	timeout := operation.Timeout
21	if timeout <= 0 {
22		timeout = 20 * time.Second
23	}
24	reqCtx, cancel := context.WithTimeout(ctx, timeout)
25	defer cancel()
26	status := 0
27	body, err := requestBytes(reqCtx, conn, operation, responseLimits{successStatuses: statuses, status: &status, bodyBytes: 1024 * 1024, errorBytes: 64 * 1024})
28	if err != nil {
29		return err
30	}
31	if err = decode(body, status); err != nil {
32		field := ""
33		if detail, ok := errors.AsType[*responseFieldError](err); ok {
34			field = detail.field
35		} else if detail, ok := errors.AsType[*json.UnmarshalTypeError](err); ok {
36			field = detail.Field
37		}
38		return invalidResponse(operation, field, err)
39	}
40	return nil
41}
42
43func executeRead(ctx context.Context, conn *Connection, operation operation, decode func([]byte) error) error {
44	if conn == nil {
45		return errors.New("not connected to Albedo")
46	}
47	if ctx == nil {
48		ctx = context.Background()
49	}
50	requestCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
51	defer cancel()
52	body, err := requestBytes(requestCtx, conn, operation, responseLimits{successStatus: http.StatusOK, bodyBytes: 1024 * 1024, errorBytes: 64 * 1024})
53	if err != nil {
54		return err
55	}
56	if err = decode(body); err != nil {
57		field := ""
58		if detail, ok := errors.AsType[*responseFieldError](err); ok {
59			field = detail.field
60		} else if detail, ok := errors.AsType[*json.UnmarshalTypeError](err); ok {
61			field = detail.Field
62		}
63		return &ProtocolError{Code: "invalid_response", Operation: operation.Name, Field: field, Cause: err}
64	}
65	return nil
66}