Skip to content

Commit b14ad43

Browse files
committed
docs: better documentation and param input handling
1 parent b420281 commit b14ad43

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ __pycache__
2525
/pyproject.toml
2626
/style_suppressions.xml
2727
bin
28+
bulkins
2829
node_modules
2930
target
3031
vendor
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
# Concurrently Buy Insurance
22

3-
This script allows you to concurrently buy EasyPost insurance. It will work with EasyPost Shipments or standalone Insurance. Use the `sample.csv` file as a template, fill in the details, and run the script.
3+
This script allows you to concurrently buy EasyPost Insurance. It works when insuring EasyPost Shipments or when buying standalone Insurance for a package bought outside of the EasyPost ecosystem. Use the `sample.csv` file as a template, fill in the details (do not delete the header), and run the script.
4+
5+
## Things to Know
6+
7+
- You must follow the `sample.csv` template or the script will not work correctly. Do not delete the header row
8+
- Only run the script once! Running the script multiple times with the same data will create duplicate insurance objects and fees
9+
- The script will spin up 20 concurrent requests at a time
10+
- Because requests are sent concurrently, they may complete in a different order than they were specified in the original CSV. Sorting the results CSV by Tracking Code and the original `sample.csv` will allow you to match up requests with input data for debugging purchases in the event of errors
11+
- The status of each request will be saved to a CSV once the script is complete. Each line will include error messages if there were any, the time each request took, and the status of the request
12+
- If there are errors reported in the results CSV, correct input data as necessary and run the script again
13+
- NOTE: Ensure you delete successful rows from the `sample.csv` file before running the script again. Failure to do so will result in duplicate insurance objects and fees (eg: If I had 5 rows, 3 succeeded, 2 failed - I would remove the 3 rows with a success status of true so that my sample CSV contained the 2 rows that initially failed, I'd correct the data as needed, and re-run the script with only those two failed rows)
14+
- It's recommended to run the `sample.csv` file as-is with a test API key to get a feel for how it works prior to loading real data and using a production API key
415

516
## Usage
617

7-
After running the script, a CSV file will be saved with the success status of each insurance purchase attempt and the time it took. If there are errors, those messages will be included.
18+
```shell
19+
EASYPOST_API_KEY=123... CSV=path/to/sample.csv go run concurrent_insurance_buy.go
20+
```
21+
22+
## Development
823

924
```shell
10-
EASYPOST_API_KEY=123... CSV=sample.csv go run concurrent_insurance_buy.go
25+
# To build a standalone binary of this tool (eg: call it bulkins)
26+
go build -o bulkins
1127
```

community/golang/concurrent_insurance_buy/concurrent_insurance_buy.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ import (
1313
"github.com/EasyPost/easypost-go/v4"
1414
)
1515

16-
/*
17-
* Concurrently buy EasyPost Insurance via a CSV file
18-
*
19-
* Usage: EASYPOST_API_KEY=123... CSV=sample.csv go run concurrent_insurance_buy.go
20-
*/
16+
// Concurrently buy EasyPost Insurance via a CSV file
17+
// Usage: EASYPOST_API_KEY=123... CSV=sample.csv go run concurrent_insurance_buy.go
2118

2219
func main() {
2320
scriptStart := time.Now()
2421

2522
records := getCsvRecords()
2623

27-
client := easypost.New(os.Getenv("EASYPOST_API_KEY"))
24+
apiKeyParam := os.Getenv("EASYPOST_API_KEY")
25+
if apiKeyParam == "" {
26+
handleGoErr(errors.New("EASYPOST_API_KEY param not set"))
27+
}
28+
29+
client := easypost.New(apiKeyParam)
2830
numOfGoroutines := int(math.Min(float64(len(records)), 20))
2931
semaphore := make(chan bool, numOfGoroutines)
3032
lineMessageList := make([][]string, 0)
@@ -44,6 +46,7 @@ func main() {
4446
to_address_id := currentLine[4]
4547
from_address_id := currentLine[5]
4648

49+
fmt.Printf("Sending request for %s...\n", tracking_code)
4750
success, message := createInsurance(client, reference, tracking_code, carrier_string, amount, to_address_id, from_address_id)
4851

4952
elapsedTime := time.Since(goroutineStartTime)
@@ -72,7 +75,12 @@ func main() {
7275

7376
// getCsvRecords builds the set of data without including the header and validates it
7477
func getCsvRecords() [][]string {
75-
file, err := os.Open(os.Getenv("CSV"))
78+
csvParam := os.Getenv("CSV")
79+
if csvParam == "" {
80+
handleGoErr(errors.New("CSV parameter not set"))
81+
}
82+
83+
file, err := os.Open(csvParam)
7684
handleGoErr(err)
7785
defer file.Close()
7886
reader := csv.NewReader(file)
@@ -129,7 +137,6 @@ func createInsurance(client *easypost.Client, reference string, tracking_code st
129137
}
130138

131139
if tracker.ShipmentID != "" {
132-
// shipment.Tracker is just the amount and not the object
133140
_, err := client.InsureShipment(tracker.ShipmentID, amount)
134141
var eperr *easypost.APIError
135142
if errors.As(err, &eperr) {

community/golang/concurrent_insurance_buy/sample.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Reference,Tracking Code,Carrier String,Amount,To Address (optional),From Address (optional)
1+
Reference,Tracking Code,Carrier String,Amount,To Address ID (optional),From Address ID (optional)
22
ref_1,EZ1000000001,USPS,100.00,,
33
ref_2,EZ2000000002,USPS,200.00,,
44
ref_3,EZ3000000003,USPS,300.00,,

0 commit comments

Comments
 (0)