-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdoc.go
More file actions
127 lines (127 loc) · 4.23 KB
/
Copy pathdoc.go
File metadata and controls
127 lines (127 loc) · 4.23 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Package walmart provides a robust Go client for accessing Walmart's order
// history and purchase data through their GraphQL API.
//
// This library enables programmatic access to order information, purchase history,
// payment details, and more through Walmart's internal API endpoints.
//
// # Features
//
// - Complete order history access (in-store, delivery, and pickup orders)
// - Detailed order information with items, pricing, and payment details
// - Driver tip tracking for delivery orders
// - Order ledger API for payment reconciliation with bank transactions
// - Search orders by item name or other criteria
// - Automatic cookie management with rotation to prevent staleness
// - Persistent cookie storage in ~/.walmart-api/cookies.json
// - Optional structured logging support with log/slog
// - Built-in rate limiting to respect API throttling
//
// # Quick Start
//
// Initialize the client with a configuration:
//
// config := walmart.ClientConfig{
// RateLimit: 2 * time.Second,
// AutoSave: true,
// }
// client, err := walmart.NewWalmartClient(config)
// if err != nil {
// log.Fatal(err)
// }
//
// Get your authentication cookies from Walmart.com by copying a request as cURL:
//
// err = client.InitializeFromCurl("curl.txt")
// if err != nil {
// log.Fatal(err)
// }
//
// Fetch recent orders with context support for cancellation and timeouts:
//
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
// defer cancel()
//
// orders, err := client.GetRecentOrders(ctx, 10)
// if err != nil {
// log.Fatal(err)
// }
//
// for _, order := range orders {
// fmt.Printf("Order %s: %d items\n", order.OrderID, order.ItemCount)
// }
//
// Get detailed order information:
//
// order, err := client.GetOrder(ctx, orderID, true)
// if err != nil {
// log.Fatal(err)
// }
//
// fmt.Printf("Total: $%.2f\n", order.PriceDetails.GrandTotal.Value)
// fmt.Printf("Tax: $%.2f\n", order.PriceDetails.TaxTotal.Value)
//
// # Authentication
//
// The client requires cookies from an authenticated Walmart.com browser session.
// All 61 cookies are required to avoid bot detection (429/418 errors), though
// only CID and SPID contain actual authentication data.
//
// Cookies are automatically updated from API responses and persisted to disk
// for reuse across sessions.
//
// # Logging
//
// The client supports optional structured logging using Go's standard log/slog:
//
// logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
// config := walmart.ClientConfig{
// Logger: logger, // Pass nil to disable logging
// }
//
// All log messages include a "client=walmart" attribute for filtering in
// multi-service environments.
//
// # Rate Limiting
//
// A default 2-second delay is enforced between requests to prevent rate limiting
// by Walmart's servers. This can be configured via ClientConfig.RateLimit.
//
// # Context Support
//
// All public API methods accept a context.Context as the first parameter,
// enabling proper cancellation, timeouts, and graceful shutdown:
//
// - Rate limiter waits are cancellable via context
// - HTTP requests respect context cancellation
// - Long-running operations like GetAllOrders check cancellation between pages
//
// Recommended timeouts:
// - Single order/history fetch: 1-2 minutes
// - Pagination operations (GetAllOrders): 5-10 minutes
// - Ledger requests: 2-5 minutes (has stricter rate limits)
//
// # Thread Safety
//
// WalmartClient is NOT safe for concurrent use from multiple goroutines.
// The rate limiter state is not protected by a mutex. If you need concurrent
// access, create separate client instances for each goroutine.
//
// # Order Types
//
// The API supports three order types:
// - IN_STORE: Physical store purchases (requires orderIsInStore: true)
// - DELIVERY: Online orders delivered to home
// - PICKUP: Online orders picked up at store
//
// # Examples
//
// For complete examples, see the examples/ directory:
// - examples/basic/ - Basic usage
// - examples/ledger/ - Payment ledger reconciliation
// - examples/logger/ - Structured logging
//
// # Note
//
// This library is for personal use to access your own order history.
// Please be respectful of Walmart's servers and adhere to their terms of service.
package walmart