-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.go
More file actions
93 lines (81 loc) · 2.15 KB
/
Copy pathmain.go
File metadata and controls
93 lines (81 loc) · 2.15 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
package main
import (
"fmt"
"github.com/svvu/gomws/mws"
"github.com/svvu/gomws/mws/products"
"github.com/svvu/gomws/mws/reports"
)
func main() {
config := mws.Config{
SellerId: "SellerId",
AuthToken: "AuthToken",
Region: "US",
// Optional if set in env variable
AccessKey: "AKey",
SecretKey: "SKey",
}
productsClient, err := products.NewClient(config)
if err != nil {
fmt.Println(err)
return
}
// Example 1
fmt.Println("------GetServiceStatus------")
statusResponse, err := productsClient.GetServiceStatus()
// Check http client error.
if err != nil {
fmt.Println(err.Error())
}
defer statusResponse.Close()
// Check whether or not the API return errors.
if statusResponse.Error != nil {
fmt.Println(statusResponse.Error.Error())
} else {
xmlNode, _ := statusResponse.ResultParser()
xmlNode.PrintXML() // Print the xml response with indention.
}
// Example 2
fmt.Println("------GetMatchingProduct------")
proResponse, err := productsClient.GetMatchingProduct([]string{"B00ON8R5EO", "B000EVOSE4"})
if err != nil {
fmt.Println(err.Error())
}
defer proResponse.Close()
if proResponse.Error != nil {
fmt.Println(proResponse.Error.Error())
return
}
// Create a result parser for the response.
parser, _ := proResponse.ResultParser()
// Get the first product from response.
productOne := parser.FindByKey("Product")[0]
// Find the title node.
productNameNode := productOne.FindByKey("Title")
// Get the name value.
name, err := productNameNode[0].ToString()
if err != nil {
fmt.Println(err)
}
fmt.Printf("Product name: %v \n", name)
// Find the height for package dimensions.
heightNode := productOne.FindByKeys("PackageDimensions", "Height")
// Inspect the heightNode map.
mws.Inspect(heightNode)
// Example 3
fmt.Println("------GetReport------")
reportClient, err := reports.NewClient(config)
rpResponse, err := reportClient.GetReport("Report-ID")
if err != nil {
fmt.Println(err.Error())
}
defer rpResponse.Close()
if rpResponse.Error != nil {
fmt.Println(rpResponse.Error.Error())
return
}
// Write report to file.
err = rpResponse.ExportTo("./output.txt")
if err != nil {
fmt.Println(err)
}
}