-
Notifications
You must be signed in to change notification settings - Fork 14
/
client_sendrecvfirst_sequential.go
42 lines (36 loc) · 1.68 KB
/
client_sendrecvfirst_sequential.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright 2021-2024 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
package restful
import (
"context"
"errors"
"net/http"
)
// SendRecvListFirst2xxSequential acts similarly to SendRecv2xx, but sends the request to the targets one-by-one, till a positive (2xx) response is received.
// If all the responses are negative, then error is returned.
// You may feed the list to a shuffle function before calling, if order is not defined.
func (c *Client) SendRecvListFirst2xxSequential(ctx context.Context, method string, targets []string, headers http.Header, reqData, respData any) (*http.Response, error) {
body, err := c.makeBodyBytes(reqData)
if err != nil {
return nil, err
}
for i := range targets {
resp, err := c.sendRequestBytes(ctx, method, targets[i], headers, &body, false)
if err != nil || resp.StatusCode >= 300 { // Errors are silently omitted
continue
}
return resp, GetResponseData(resp, c.maxBytesToParse, respData)
}
return nil, errors.New("no positive response")
}
// SendRecvResolveFirst2xxSequential acts similarly to SendRecv2xx, but sends the request to the resolved targets one-by-one, till a positive (2xx) response is received.
// If all the responses are negative, then error is returned.
// You may feed the list to a shuffle function before calling, if order is not defined.
func (c *Client) SendRecvResolveFirst2xxSequential(ctx context.Context, method string, target string, headers http.Header, reqData, respData any) (*http.Response, error) {
targets, err := c.target2URLs(target)
if err != nil {
return nil, err
}
return c.SendRecvListFirst2xxSequential(ctx, method, targets, headers, reqData, respData)
}