generated from koddr/template-go
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate_fields.go
96 lines (82 loc) Β· 2.57 KB
/
update_fields.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
package main
import (
"bytes"
"fmt"
"net/http"
"net/textproto"
"net/url"
"strings"
"time"
"github.com/koddr/gosl"
)
// updateField provides update process for the given transaction ID.
func (app *App) updateField(id, fieldName string, fieldValues []string) error {
// Set primary key (PK) to the endpoint body.
_, body := gosl.ModifyByValue(app.Config.API.UpdateEndpoint.EndpointBody, app.Config.ColumnsOrder[0], id)
// Check, if field has a many values.
if len(fieldValues) > 1 {
// If true, set many values to the field.
_, body = gosl.ModifyByValue(body, fieldName, fieldValues)
} else {
// If false, set the only one value to the field.
_, body = gosl.ModifyByValue(body, fieldName, fieldValues[0])
}
// Marshaling endpoint body.
reqBody, err := gosl.Marshal(&body)
if err != nil {
return err
}
// Set endpoint from the config.
endpoint := app.Config.API.UpdateEndpoint.EndpointName
// If endpoint want to set primary key (PK) to it.
if app.Config.API.UpdateEndpoint.AddPKToEndpointName {
// Set primary key (PK) to the endpoint from the config.
endpoint = fmt.Sprintf(app.Config.API.UpdateEndpoint.EndpointName, id)
}
// Build API URL.
apiUrl := url.URL{
Scheme: strings.ToLower(app.Config.API.BaseURLSchema),
Host: strings.ToLower(app.Config.API.BaseURL),
Path: strings.ToLower(endpoint),
}
// Create HTTP request to API URL with endpoint body.
req, err := http.NewRequest(app.Config.API.UpdateEndpoint.HTTPMethod, apiUrl.String(), bytes.NewBuffer(reqBody))
if err != nil {
return err
}
defer req.Body.Close()
// Set request headers.
req.Header.Set(
textproto.CanonicalMIMEHeaderKey("authorization"),
gosl.Concat(app.Config.API.AuthMethod, " ", app.Config.API.Token),
)
req.Header.Set(
textproto.CanonicalMIMEHeaderKey("content-type"),
app.Config.API.UpdateEndpoint.ContentType,
)
// Create a new HTTP client.
client := &http.Client{
// Set timeout count from the config.
Timeout: time.Second * time.Duration(app.Config.API.RequestTimeout),
}
// Make HTTP request by the client.
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// Check, if response HTTP status < 300.
if resp.StatusCode < http.StatusMultipleChoices {
printStyled(
fmt.Sprintf(
"β Field '%s' with values '%s' in the transaction '%s' has been successfully updated (HTTP %d)",
fieldName, fieldValues, id, resp.StatusCode,
),
"margin-left",
)
} else {
// Else, return error message with the raw response body.
return fmt.Errorf("transaction '%s' returned HTTP %d", id, resp.StatusCode)
}
return nil
}