-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.go
More file actions
61 lines (57 loc) · 2 KB
/
sync.go
File metadata and controls
61 lines (57 loc) · 2 KB
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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
srvConfig "github.com/CHESSComputing/golib/config"
)
// helper function to get sync recods from metadata services
func getSyncRecords(suuid string) ([]map[string]any, error) {
var records []map[string]any
// fetch records matching our did
_httpReadRequest.GetToken()
rurl := fmt.Sprintf("%s/records", srvConfig.Config.Services.SyncServiceURL)
if suuid != "" {
rurl = fmt.Sprintf("%s/record/%s", srvConfig.Config.Services.SyncServiceURL, suuid)
}
resp, err := _httpReadRequest.Get(rurl)
if err != nil {
log.Println("ERROR: unable to GET to MetaData service, error", err)
return records, fmt.Errorf("[Frontend.main.getSyncRecords] _httpReadRequest.Get error: %w", err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("ERROR: unable to read response body, error", err)
return records, fmt.Errorf("[Frontend.main.getSyncRecords] io.ReadAll error: %w", err)
}
err = json.Unmarshal(data, &records)
if err != nil {
log.Println("ERROR: unable to unmarshal service response, error", err)
return records, fmt.Errorf("[Frontend.main.getSyncRecords] json.Unmarshal error: %w", err)
}
return records, nil
}
// helper function to delete sync record
func deleteSyncRecord(suuid string) error {
// fetch records matching our did
_httpDeleteRequest.GetToken()
rurl := fmt.Sprintf("%s/record/%s", srvConfig.Config.Services.SyncServiceURL, suuid)
log.Println("DELETE", rurl, _httpDeleteRequest)
resp, err := _httpDeleteRequest.Delete(rurl, "application/json", bytes.NewBuffer([]byte("")))
log.Println("DELETE response", resp, err)
defer resp.Body.Close()
if err != nil {
log.Println("ERROR: unable to GET to MetaData service, error", err)
return fmt.Errorf("[Frontend.main.deleteSyncRecord] _httpDeleteRequest.Delete error: %w", err)
}
if resp.StatusCode != 200 {
msg := fmt.Sprintf("unable to delete sync record %s", suuid)
log.Println("ERROR: " + msg)
return errors.New(msg)
}
return nil
}