-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_customer.go
50 lines (38 loc) · 1.3 KB
/
delete_customer.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
package togai
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
func (c *TogaiClient) DeleteCustomer(customerId string) error {
deleteCustomerEndpoint := c.apiBaseUrl.JoinPath("customers", customerId)
req, err := http.NewRequest(http.MethodDelete, deleteCustomerEndpoint.String(), nil)
if err != nil {
return fmt.Errorf("error occurred while forming request: %v", err)
}
req.Header.Add(ACCEPT_HTTP_HEADER, JSON_TYPE)
req.Header.Add(AUTHORIZATION_HTTP_HEADER, fmt.Sprintf("Bearer %s", c.apiToken))
res, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("error occurred while sending request: %v", err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
body, _ := io.ReadAll(res.Body)
return fmt.Errorf("expected 200 OK response but got: \nstatus code: %v, status: %v, body: %v", res.StatusCode, res.Status, string(body))
}
body, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("error occurred while reading response body: %v", err)
}
var deleteCustomerOutput Status
err = json.Unmarshal(body, &deleteCustomerOutput)
if err != nil {
return fmt.Errorf("error occurred while parsing json response body: %v\n\njson body: %v", err, string(body))
}
if !deleteCustomerOutput.Success {
return fmt.Errorf("deletion of the customer did not succeed")
}
return nil
}