mws 0.12.0

Client library for Amazon Marketplace Web Service (Amazon MWS)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
pub struct AmazonRegion {
  pub id: &'static str,
  pub name: &'static str,
  pub endpoint: &'static str,
  pub marketplace_id_list: Vec<&'static str>,
  pub marketplaces: Vec<&'static AmazonMarketplace>,
}

pub struct AmazonMarketplace {
  pub id: &'static str,
  pub name: &'static str,
  pub region_id: &'static str,
  pub country_id: &'static str,
}

pub const REGION_ID_NA: &'static str = "na";
pub const REGION_ID_EU: &'static str = "eu";
pub const REGION_ID_IN: &'static str = "in";
pub const REGION_ID_CN: &'static str = "cn";
pub const REGION_ID_FE: &'static str = "fe";

pub const MARKETPLACE_ID_CA: &'static str = "A2EUQ1WTGCTBG2";
pub const MARKETPLACE_ID_MX: &'static str = "A1AM78C64UM0Y8";
pub const MARKETPLACE_ID_US: &'static str = "ATVPDKIKX0DER";
pub const MARKETPLACE_ID_DE: &'static str = "A1PA6795UKMFR9";
pub const MARKETPLACE_ID_ES: &'static str = "A1RKKUPIHCS9HS";
pub const MARKETPLACE_ID_FR: &'static str = "A13V1IB3VIYZZH";
pub const MARKETPLACE_ID_IT: &'static str = "APJ6JRA9NG5V4";
pub const MARKETPLACE_ID_GB: &'static str = "A1F83G8C2ARO7P";
pub const MARKETPLACE_ID_IN: &'static str = "A21TJRUUN4KGV";
pub const MARKETPLACE_ID_JP: &'static str = "A1VC38T7YXB528";
pub const MARKETPLACE_ID_CN: &'static str = "AAHKV2X7AFYLW";
pub const MARKETPLACE_ID_AU: &'static str = "A39IBJ37TRP1C6";
pub const MARKETPLACE_ID_SG: &'static str = "A19VAU5U5O7RUS";

lazy_static! {
  pub static ref MARKETPLACES: Vec<AmazonMarketplace> = {
    macro_rules! item {
      ($id:expr, $region_id:expr, $name:expr, $country_id:expr) => {
        AmazonMarketplace {
          id: $id,
          name: $name,
          region_id: $region_id,
          country_id: $country_id,
        }
      };
    }
    let mut items = vec![];
    items.push(item!(MARKETPLACE_ID_CA, "na", "Canada", "CA"));
    items.push(item!(MARKETPLACE_ID_MX, "na", "Mexico", "MX"));
    items.push(item!(MARKETPLACE_ID_US, "na", "USA", "US"));

    items.push(item!(MARKETPLACE_ID_DE, "eu", "Germany", "DE"));
    items.push(item!(MARKETPLACE_ID_ES, "eu", "Spain", "ES"));
    items.push(item!(MARKETPLACE_ID_FR, "eu", "France", "FR"));
    items.push(item!(MARKETPLACE_ID_IT, "eu", "Italy", "IT"));
    items.push(item!(MARKETPLACE_ID_GB, "eu", "United Kingdom", "GB"));

    items.push(item!(MARKETPLACE_ID_IN, "in", "India", "IN"));

    items.push(item!(MARKETPLACE_ID_JP, "fe", "Japan", "JP"));
    items.push(item!(MARKETPLACE_ID_AU, "fe", "Australia", "AU"));
    items.push(item!(MARKETPLACE_ID_SG, "fe", "Australia", "AU"));

    items.push(item!(MARKETPLACE_ID_CN, "cn", "China", "CN"));

    items
  };
  pub static ref REGIONS: Vec<AmazonRegion> = {
    vec![
      AmazonRegion {
        id: REGION_ID_NA,
        name: "North America (NA)",
        endpoint: "mws.amazonservices.com",
        marketplace_id_list: get_region_marketplace_id_list("na"),
        marketplaces: get_region_marketplace_list("na"),
      },
      AmazonRegion {
        id: REGION_ID_EU,
        name: "Europe (EU)",
        endpoint: "mws-eu.amazonservices.com",
        marketplace_id_list: get_region_marketplace_id_list("eu"),
        marketplaces: get_region_marketplace_list("eu"),
      },
      AmazonRegion {
        id: REGION_ID_IN,
        name: "India (IN)",
        endpoint: "mws.amazonservices.com",
        marketplace_id_list: get_region_marketplace_id_list("in"),
        marketplaces: get_region_marketplace_list("in"),
      },
      AmazonRegion {
        id: REGION_ID_CN,
        name: "China (CN)",
        endpoint: "mws.amazonservices.com.cn",
        marketplace_id_list: get_region_marketplace_id_list("cn"),
        marketplaces: get_region_marketplace_list("cn"),
      },
      AmazonRegion {
        id: REGION_ID_FE,
        name: "Far East (FE)",
        endpoint: "mws.amazonservices.com.au",
        marketplace_id_list: get_region_marketplace_id_list("fe"),
        marketplaces: get_region_marketplace_list("fe"),
      },
    ]
  };
}

pub fn get_marketplace_region(id: &str) -> Option<&str> {
  get_marketplace(id).map(|m| m.region_id)
}

pub fn get_region_marketplace_id_list(region_id: &str) -> Vec<&'static str> {
  MARKETPLACES
    .iter()
    .filter_map(|m| {
      if m.region_id == region_id {
        Some(m.id)
      } else {
        None
      }
    })
    .collect()
}

pub fn get_region_marketplace_list(region_id: &str) -> Vec<&'static AmazonMarketplace> {
  MARKETPLACES
    .iter()
    .filter_map(|m| {
      if m.region_id == region_id {
        Some(m)
      } else {
        None
      }
    })
    .collect()
}

pub fn get_region(id: &str) -> Option<&'static AmazonRegion> {
  REGIONS.iter().find(|r| r.id == id)
}

pub fn get_marketplace(id: &str) -> Option<&'static AmazonMarketplace> {
  MARKETPLACES.iter().find(|r| r.id == id)
}

impl AmazonMarketplace {
  pub fn resolve_state_code(&self, country_id: &str, state: &str) -> Option<String> {
    match self.id {
      MARKETPLACE_ID_US if country_id == "US" => resolve_usa_state_code(state),
      _ => Some(state.to_string()),
    }
  }
}

const US_STATES: &'static [(&'static str, &'static str)] = &[
  ("AK", "alaska"),
  ("AL", "alabama"),
  ("AP", "apo/fpo: asia, pacific"),
  ("AR", "arkansas"),
  ("AZ", "arizona"),
  ("CA", "california"),
  ("CO", "colorado"),
  ("CT", "connecticut"),
  ("DC", "district of columbia"),
  ("DE", "delaware"),
  ("FL", "florida"),
  ("GA", "georgia"),
  ("HI", "hawaii"),
  ("IA", "iowa"),
  ("ID", "idaho"),
  ("IL", "illinois"),
  ("IN", "indiana"),
  ("KS", "kansas"),
  ("KY", "kentucky"),
  ("LA", "louisiana"),
  ("MA", "massachusetts"),
  ("MD", "maryland"),
  ("ME", "maine"),
  ("MI", "michigan"),
  ("MN", "minnesota"),
  ("MO", "missouri"),
  ("MS", "mississippi"),
  ("MT", "montana"),
  ("NC", "north carolina"),
  ("ND", "north dakota"),
  ("NE", "nebraska"),
  ("NH", "new hampshire"),
  ("NJ", "new jersey"),
  ("NM", "new mexico"),
  ("NV", "nevada"),
  ("NY", "new york"),
  ("OH", "ohio"),
  ("OK", "oklahoma"),
  ("OR", "oregon"),
  ("PA", "pennsylvania"),
  ("RI", "rhode island"),
  ("SC", "south carolina"),
  ("SD", "south dakota"),
  ("TN", "tennessee"),
  ("TX", "texas"),
  ("UT", "utah"),
  ("VA", "virginia"),
  ("VT", "vermont"),
  ("WA", "washington"),
  ("WI", "wisconsin"),
  ("WV", "west virginia"),
  ("WY", "wyoming"),
];

fn resolve_usa_state_code(state: &str) -> Option<String> {
  let v = normalize(state);
  if v.len() == 2 {
    let id_match = US_STATES.iter().find(|(id, _)| id == &v.to_uppercase());
    if let Some((id, _)) = id_match {
      return Some(id.to_string());
    }
  }

  let text_match = US_STATES.iter().find(|(_, text)| text == &v);
  if let Some((id, _)) = text_match {
    return Some(id.to_string());
  }

  None
}

#[test]
fn test_resolve_usa_state_code() {
  [
    "Al",
    "AL",
    "Alabama",
    "ALABAMA",
    "Alaska",
    "AR",
    "Arizona",
    "ARIZONA",
    "Arkansas",
    "az",
    "Az",
    "AZ",
    "ca",
    "Ca",
    "Ca.",
    "CA",
    "California",
    "CALIFORNIA",
    "Co",
    "CO",
    "Colorado",
    "COLORADO",
    "Connecticut",
    "CONNECTICUT",
    "Ct",
    "CT",
    "DC",
    "DE",
    "Delaware",
    "DELAWARE",
    "District of Columbia",
    "DISTRICT OF COLUMBIA",
    "fl",
    "Fl",
    "FL",
    "florida",
    "Florida",
    "FLORIDA",
    "Ga",
    "GA",
    "Georgia",
    "GEORGIA",
    "Hawaii",
    "HI",
    "IA",
    "ID",
    "Idaho",
    "il",
    "Il",
    "IL",
    "Illinois",
    "ILLINOIS",
    "IN",
    "Indiana",
    "INDIANA",
    "Iowa",
    "IOWA",
    "Kansas",
    "KANSAS",
    "Kentucky",
    "KENTUCKY",
    "ks",
    "KS",
    "Ky",
    "KY",
    "LA",
    "Louisiana",
    "ma",
    "Ma",
    "MA",
    "Maine",
    "MAINE",
    "Maryland",
    "MARYLAND",
    "massachusetts",
    "Massachusetts",
    "MASSACHUSETTS",
    "Md",
    "MD",
    "Me",
    "ME",
    "mi",
    "MI",
    "Michigan",
    "MICHIGAN",
    "Minnesota",
    "MINNESOTA",
    "Mississippi",
    "MISSISSIPPI",
    "Missouri",
    "MISSOURI",
    "mn",
    "MN",
    "mo",
    "Mo",
    "MO",
    "Montana",
    "MONTANA",
    "MS",
    "MT",
    "nc",
    "Nc",
    "NC",
    "ND",
    "NE",
    "Nebraska",
    "Nevada",
    "NEVADA",
    "New Hampshire",
    "New Jersey",
    "NEW JERSEY",
    "New Mexico",
    "NEW MEXICO",
    "new york",
    "New York",
    "NEW YORK",
    "NH",
    "nj",
    "Nj",
    "NJ",
    "N.J.",
    "NM",
    "North Carolina",
    "North Dakota",
    "NV",
    "ny",
    "Ny",
    "NY",
    "N.Y.",
    "OH",
    "ohio",
    "Ohio",
    "OHIO",
    "OK",
    "Oklahoma",
    "OKLAHOMA",
    "OR",
    "Oregon",
    "OREGON",
    "pa",
    "Pa",
    "PA",
    "Pennsylvania",
    "PENNSYLVANIA",
    "Rhode Island",
    "RHODE ISLAND",
    "RI",
    "SC",
    "SD",
    "South Carolina",
    "SOUTH CAROLINA",
    "Tennessee",
    "texas",
    "Texas",
    "TEXAS",
    "tn",
    "TN",
    "Tx",
    "TX",
    "TX ",
    "UT",
    "Utah",
    "UTAH",
    "Va",
    "VA",
    "Vermont",
    "VERMONT",
    "Virginia",
    "VIRGINIA",
    "VT",
    "wa",
    "Wa",
    "WA",
    "WA ",
    "Washington",
    "WASHINGTON",
    "West Virginia",
    "WEST VIRGINIA",
    "wi",
    "WI",
    "Wisconsin",
    "WISCONSIN",
    "WV",
    "WY",
    "Wyoming",
    "WYOMING",
  ]
  .iter()
  .for_each(|v| {
    assert!(
      resolve_usa_state_code(v).is_some(),
      "{}: {}",
      v,
      normalize(v)
    )
  })
}

fn normalize(text: &str) -> String {
  text
    .to_lowercase()
    .chars()
    .filter(|c| c.is_alphabetic() || c.is_whitespace())
    .collect::<String>()
    .split_whitespace()
    .collect::<Vec<_>>()
    .join(" ")
}