This repository has been archived by the owner on Feb 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
amazon_endpoint.rb
133 lines (108 loc) · 3.77 KB
/
amazon_endpoint.rb
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
Dir['./lib/**/*.rb'].each &method(:require)
class AmazonEndpoint < EndpointBase
set :logging, true
post '/get_orders' do
amazon_client = AmazonClient.new(@config, @message)
@base_response = { message_id: @message[:message_id] }
begin
orders = amazon_client.orders
if orders.any?
# updates last_updated_at
parameters = { parameters: [{ name: 'amazon.last_updated_after',
value: orders.last.last_update_date }] }
response = Builder.new(orders).build_response(parameters)
end
code = 200
rescue => e
code, response = handle_error(e)
end
process_result code, @base_response.merge(response.to_h)
end
post '/get_order_by_number' do
amazon_client = AmazonClient.new(@config, @message)
@base_response = { message_id: @message[:message_id] }
begin
order = amazon_client.order_by_number(@message[:payload]['amazon_order_id'])
response = Builder.new(order).build_response
code = 200
rescue => e
code, response = handle_error(e)
end
process_result code, @base_response.merge(response.to_h)
end
post '/get_inventory_by_sku' do
amazon_client = AmazonClient.new(@config, @message)
@base_response = { message_id: @message[:message_id] }
begin
inventory = amazon_client.inventory_by_sku(@message[:payload]['sku'])
response = Builder.new(inventory).build_response
code = 200
rescue => e
code, response = handle_error(e)
end
process_result code, @base_response.merge(response.to_h)
end
post '/confirm_shipment' do
feed = AmazonFeed.new(@config)
@base_response = { message_id: @message[:message_id] }
begin
order = Feeds::OrderFulfillment.new(@message[:payload]['shipment'], @config['amazon.seller_id'])
response = feed.submit(order.feed_type, order.to_xml)
code = 200
rescue Feeds::RequestThrottled => e
response = { delay: e.delay_in_seconds }
code = 200
rescue Feeds::QuotaExceeded => e
response = { delay: e.reset_quota_in_seconds }
code = 200
rescue => e
code, response = handle_error(e)
end
process_result code, @base_response.merge(response.to_h)
end
post '/feed_status' do
feed = AmazonFeed.new(@config)
@base_response = { message_id: @message[:message_id] }
begin
response = feed.status(@message[:payload]['feed_id'])
code = 200
rescue Feeds::FeedProcessingResultNotReady => e
response = { delay: 2.minutes }
code = 200
rescue Feeds::RequestThrottled => e
response = { delay: e.delay_in_seconds }
code = 200
rescue Feeds::QuotaExceeded => e
response = { delay: e.reset_quota_in_seconds }
code = 200
rescue => e
code, response = handle_error(e)
end
process_result code, @base_response.merge(response.to_h)
end
post '/update_inventory_availabitity' do
feed = AmazonFeed.new(@config)
@base_response = { message_id: @message[:message_id] }
begin
inventory = Feeds::InventoryAvailabitity.new(@message[:payload], @config['amazon.seller_id'])
response = feed.submit(inventory.feed_type, inventory.to_xml)
code = 200
rescue Feeds::RequestThrottled => e
response = { delay: e.delay_in_seconds }
code = 200
rescue Feeds::QuotaExceeded => e
response = { delay: e.reset_quota_in_seconds }
code = 200
rescue => e
code, response = handle_error(e)
end
process_result code, @base_response.merge(response.to_h)
end
private
def handle_error(e)
response = { notifications: [ { level: 'error',
subject: e.message,
description: e.backtrace.to_a.join('\n\t') }] }
[500, response]
end
end