-
Notifications
You must be signed in to change notification settings - Fork 0
/
do.go
108 lines (103 loc) · 2.92 KB
/
do.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package pocketic
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
func (pic PocketIC) do(method, url string, input, output any) error {
start := time.Now()
for {
if pic.timeout < time.Since(start) {
return fmt.Errorf("timeout exceeded")
}
pic.logger.Printf("[POCKETIC] %s %s %+v", method, url, input)
req, err := newRequest(method, url, input)
if err != nil {
return err
}
resp, err := pic.client.Do(req)
if err != nil {
return err
}
switch resp.StatusCode {
case http.StatusOK, http.StatusCreated:
if resp.Body == nil || output == nil {
// No need to decode the response body.
return nil
}
if err := json.NewDecoder(resp.Body).Decode(output); err != nil {
return fmt.Errorf("failed to decode response body: %w", err)
}
return nil
case http.StatusAccepted:
var response startedOrBusyResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return fmt.Errorf("failed to decode accepted response body: %w", err)
}
pic.logger.Printf("[POCKETIC] Accepted: %s %s", response.StateLabel, response.OpID)
if method == http.MethodGet {
continue
}
for {
pic.logger.Printf("[POCKETIC] Waiting for %s %s", response.StateLabel, response.OpID)
if pic.timeout < time.Since(start) {
return fmt.Errorf("timeout exceeded")
}
req, err := newRequest(
http.MethodGet,
fmt.Sprintf(
"%s/read_graph/%s/%s",
pic.server.URL(),
response.StateLabel,
response.OpID,
),
nil,
)
if err != nil {
return err
}
resp, err := pic.client.Do(req)
if err != nil {
return err
}
switch resp.StatusCode {
case http.StatusOK, http.StatusCreated:
if resp.Body == nil || output == nil {
// No need to decode the response body.
return nil
}
if err := json.NewDecoder(resp.Body).Decode(output); err != nil {
return fmt.Errorf("failed to decode response body: %w", err)
}
return nil
case http.StatusAccepted, http.StatusConflict:
default:
var errResp ErrorMessage
if err := json.NewDecoder(resp.Body).Decode(&errResp); err != nil {
return fmt.Errorf("failed to decode accepted/conflict response body: %w", err)
}
return errResp
}
}
case http.StatusConflict:
var response startedOrBusyResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return fmt.Errorf("failed to decode conflict response body: %w", err)
}
pic.logger.Printf("[POCKETIC] Conflict: %s %s", response.StateLabel, response.OpID)
time.Sleep(pic.delay) // Retry after a short delay.
continue
default:
var errResp ErrorMessage
if err := json.NewDecoder(resp.Body).Decode(&errResp); err != nil {
return fmt.Errorf("failed to decode error response body: %w", err)
}
return errResp
}
}
}
type startedOrBusyResponse struct {
StateLabel string `json:"state_label"`
OpID string `json:"op_id"`
}