Created
February 20, 2023 17:50
-
-
Save dewey/a897c04ab88c6f5013f8a7c526201666 to your computer and use it in GitHub Desktop.
Batch delete customers from Gambio Shop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"net/http" | |
"strconv" | |
"strings" | |
) | |
func main() { | |
client := &http.Client{} | |
req, err := http.NewRequest("GET", "https://shop.example.com/api.php/v2/customers", nil) | |
req.Header.Add("Authorization", "Basic changeme") | |
resp, err := client.Do(req) | |
if err != nil { | |
fmt.Println("err", err) | |
return | |
} | |
var customers []Customer | |
if err := json.NewDecoder(resp.Body).Decode(&customers); err != nil { | |
fmt.Println("err", err) | |
return | |
} | |
var toDeleteIds []string | |
for _, customer := range customers { | |
// criteria by which you want to wipe customers | |
if customer.Id > 250 { | |
toDeleteIds = append(toDeleteIds, strconv.Itoa(customer.Id)) | |
fmt.Println(customer.Firstname, customer.Email) | |
} | |
} | |
param := strings.Join(toDeleteIds, ",") | |
req, err = http.NewRequest("DELETE", "https://shop.example.com/api.php/v2/customers/"+param, nil) | |
req.Header.Add("Authorization", "Basic changeme") | |
resp, err = client.Do(req) | |
if err != nil { | |
fmt.Println("err", err) | |
return | |
} | |
if resp.StatusCode != 200 { | |
fmt.Println("err", "unexpected status code, not 200") | |
fmt.Println("status code", resp.Status) | |
return | |
} | |
fmt.Println("deleted", param) | |
} | |
type Customer struct { | |
Id int `json:"id"` | |
Number string `json:"number"` | |
Gender string `json:"gender"` | |
Firstname string `json:"firstname"` | |
Lastname string `json:"lastname"` | |
DateOfBirth string `json:"dateOfBirth"` | |
VatNumber string `json:"vatNumber"` | |
VatNumberStatus int `json:"vatNumberStatus"` | |
Telephone string `json:"telephone"` | |
Fax string `json:"fax"` | |
Email string `json:"email"` | |
StatusId int `json:"statusId"` | |
IsGuest bool `json:"isGuest"` | |
AddressId int `json:"addressId"` | |
Links struct { | |
Address string `json:"address"` | |
Status string `json:"status"` | |
} `json:"_links"` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment