mirror of
https://github.com/jech/galene.git
synced 2024-11-13 20:25:57 +01:00
Refactor updateJSON.
This commit is contained in:
parent
04be28fa5b
commit
5557c07f1e
1 changed files with 20 additions and 34 deletions
|
@ -319,24 +319,27 @@ func setAuthorization(req *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getJSON(url string, value any) error {
|
func getJSON[T any](url string, value T) (string, error) {
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
setAuthorization(req)
|
setAuthorization(req)
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
etag := resp.Header.Get("ETag")
|
||||||
|
|
||||||
if resp.StatusCode >= 300 {
|
if resp.StatusCode >= 300 {
|
||||||
return fmt.Errorf("%v %v", resp.StatusCode, resp.Status)
|
return etag, fmt.Errorf("%v %v", resp.StatusCode, resp.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
decoder := json.NewDecoder(resp.Body)
|
decoder := json.NewDecoder(resp.Body)
|
||||||
return decoder.Decode(value)
|
return etag, decoder.Decode(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func putJSON(url string, value any, overwrite bool) error {
|
func putJSON(url string, value any, overwrite bool) error {
|
||||||
|
@ -392,49 +395,32 @@ func postJSON(url string, value any) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateJSON[T any](url string, update func(T) T) error {
|
func updateJSON[T any](url string, update func(T) T) error {
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
var old T
|
||||||
|
etag, err := getJSON(url, &old)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
setAuthorization(req)
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
if resp.StatusCode >= 300 {
|
|
||||||
return fmt.Errorf("%v %v", resp.StatusCode, resp.Status)
|
|
||||||
}
|
|
||||||
etag := resp.Header.Get("ETag")
|
|
||||||
if etag == "" {
|
if etag == "" {
|
||||||
return errors.New("missing ETag")
|
return errors.New("missing ETag")
|
||||||
}
|
}
|
||||||
|
|
||||||
decoder := json.NewDecoder(req.Body)
|
|
||||||
var old T
|
|
||||||
err = decoder.Decode(&old)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
value := update(old)
|
value := update(old)
|
||||||
|
|
||||||
j, err := json.Marshal(value)
|
j, err := json.Marshal(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
req2, err := http.NewRequest("PUT", url, bytes.NewReader(j))
|
req, err := http.NewRequest("PUT", url, bytes.NewReader(j))
|
||||||
setAuthorization(req2)
|
setAuthorization(req)
|
||||||
req2.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req2.Header.Set("If-Match", etag)
|
req.Header.Set("If-Match", etag)
|
||||||
|
|
||||||
resp2, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer resp2.Body.Close()
|
defer resp.Body.Close()
|
||||||
if resp2.StatusCode >= 300 {
|
if resp.StatusCode >= 300 {
|
||||||
return fmt.Errorf("%v %v", resp.StatusCode, resp.Status)
|
return fmt.Errorf("%v %v", resp.StatusCode, resp.Status)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -714,7 +700,7 @@ func listUsersCmd(cmdname string, args []string) {
|
||||||
log.Fatalf("Build URL: %v", err)
|
log.Fatalf("Build URL: %v", err)
|
||||||
}
|
}
|
||||||
var users []string
|
var users []string
|
||||||
err = getJSON(u, &users)
|
_, err = getJSON(u, &users)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Get users: %v", err)
|
log.Fatalf("Get users: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -731,7 +717,7 @@ func listUsersCmd(cmdname string, args []string) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var d group.UserDescription
|
var d group.UserDescription
|
||||||
err = getJSON(uu, &d)
|
_, err = getJSON(uu, &d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("%-12s (ERROR=%v)\n", user, err)
|
fmt.Printf("%-12s (ERROR=%v)\n", user, err)
|
||||||
continue
|
continue
|
||||||
|
@ -920,7 +906,7 @@ func listGroupsCmd(cmdname string, args []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var groups []string
|
var groups []string
|
||||||
err = getJSON(u, &groups)
|
_, err = getJSON(u, &groups)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Get groups: %v", err)
|
log.Fatalf("Get groups: %v", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue