-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathclient.go
64 lines (54 loc) · 1.54 KB
/
client.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log/slog"
"net"
"net/http"
"time"
"github.com/google/uuid"
)
const RESTATE_URL = "http://localhost:8080"
// The upload client calls the data upload workflow and awaits the result for 5 seconds.
// If the workflow doesn't complete within that time, it asks the
// workflow to send the upload url via email instead.
func upload(id string, email string) error {
slog.Info("Start upload for " + id)
client := &http.Client{Timeout: 5 * time.Second}
url := fmt.Sprintf("%s/DataUploadService/%s/Run", RESTATE_URL, id)
// Send a request with a timeout of 5 seconds
resp, err := client.Get(url)
if err != nil {
// Timeout hit; Request the workflow to send us an email with the response instead
if err, ok := err.(net.Error); ok && err.Timeout() {
slog.Info("Slow upload... Mail the link later")
emailHandlerUrl := fmt.Sprintf("%s/DataUploadService/%s/ResultAsEmail/send", RESTATE_URL, id)
emailData, _ := json.Marshal(email)
resp2, err := client.Post(emailHandlerUrl, "application/json", bytes.NewBuffer(emailData))
if err != nil {
return err
}
defer resp2.Body.Close()
return nil
}
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
// ... process result directly ...
slog.Info("Fast upload: URL was " + string(body))
return nil
}
func main() {
id := uuid.New().String()
err := upload(id, fmt.Sprintf("%[email protected]", id))
if err != nil {
slog.Error("Upload failed", "err", err.Error())
return
}
}