Bad request using peddler to create outbound fulfillment - ruby-on-rails

I'm trying to use the Peddler gem to create outbound fulfillments in MWS.
The only feedback I'm getting is Excon::Error::BadRequest (Expected(200) <=> Actual(400 Bad Request) hence it's a little hard to figure out what's going wrong. Here's the line that calls the API (with the values parsed):
#client.create_fulfillment_order("186", "186", 2016-08-01T07:35:48Z, "Test shipment number: 186", "Standard", {"Name"=>"Bert the Destroyer", "Line1"=>"Teststreet", "Line2"=>"123", "Line3"=>"", "DistrictOrCounty"=>"", "City"=>"Testcity", "StateOrProvinceCode"=>"CO", "CountryCode"=>"US", "PostalCode"=>"60401", "PhoneNumber"=>"12345678"}, [{"SellerSKU"=>"4785000045", "SellerFulfillmentOrderItemId"=>"4785000045", "Quantity"=>15}], {:fulfillment_policy=>"FillAll", :notification_email_list=>["bertthedestroyer#gmail.com"]})
I can't seem to figure out how to get a 200 back. Can anyone help?
The actual code:
address = {
"Name" => shipment.order.ship_to_name.to_s,
"Line1" => shipment.order.ship_to_address_1.to_s,
"Line2" => shipment.order.ship_to_address_2.to_s,
"Line3" => "",
"DistrictOrCounty" => "",
"City" => shipment.order.ship_to_city.to_s,
"StateOrProvinceCode" => shipment.order.ship_to_state_code.to_s,
"CountryCode" => shipment.order.ship_to_country_code.to_s,
"PostalCode" => shipment.order.ship_to_zipcode.to_s,
"PhoneNumber" => shipment.order.ship_to_phonenumber.to_s
}
items = []
shipment.m2m_line_item_shipments.each do |m2m|
items << {"SellerSKU" => m2m.vendor_sku.name.to_s, "SellerFulfillmentOrderItemId" => m2m.vendor_sku.name.to_s, "Quantity" => m2m.line_item.actual_quantity }
end
order_comment = "#{shipment.order.store.name} shipment number: " + shipment.id.to_s
opts = {:fulfillment_policy => "FillAll", :notification_email_list => [shipment.order.ship_to_email.to_s] }
created_at = shipment.order.created_at.iso8601
response = #client.create_fulfillment_order(shipment.id.to_s, shipment.id.to_s, created_at, order_comment.to_s, 'Standard', address, items, opts)
order = response.parse
logger.debug "order.inspect: #{order.inspect}"
Edit: After some more digging I found this. I tried sending item quantity as integer and string but the same error occurs:
<Error>
<Type>Sender</Type>
<Code>InvalidRequestException</Code>
<Message>Value AllQuantityZero for parameter is invalid.</Message>
</Error>

After more searching, I found my answer. Turns out this error means that the SKU is out of stock. Great error message amazon!
Source:
https://sellercentral.amazon.com/forums/message.jspa?messageID=2745103

Related

How to make a POST query for multi city flight offers search with Amadeus Ruby gem

I am trying to test out the flight offers search API for multi city flight searches.
I am using the amadeus gem. see: https://github.com/amadeus4dev/amadeus-ruby
I am using Rails 6.
I have the following in a controller action
def flight_offer_results
destinations = {
"originDestinations" => [
{
"id" => 1,
"originLocationCode" => "LON",
"destinationLocationCode" => "NYC",
"departureDateTimeRange" => {
"date" => "2020-12-30"
}
},
{
"id" => 2,
"originLocationCode" => "IAH",
"destinationLocationCode" => "SFO",
"departureDateTimeRange" => {
"date" => "2021-01-15"
}
}
],
"travelers" => [
{
"id" => 1,
"travelerType" => "ADULT"
}
],
"sources" => [
"GDS"
],
"searchCriteria" => {
"maxFlightOffers" => 1
}
}
begin
amadeus = Amadeus::Client.new
response = amadeus.shopping.flight_offers_search.post(destinations)
#flight_offers = response.data
puts "#flight_offers =========>>>>>>>> #{#flight_offers}"
rescue Amadeus::ResponseError => e
raise e
end
end
When I run this code I get the following error in the console:
Amadeus::NetworkError ([---]):
I can make a get request and return results for one origin and destination.
How can I make the POST request to be able to query for multi city flight searches?
The problem is that the code above is not propper json. So, I added .to_json to destinations in the line that performs the post request and it worked.
response = amadeus.shopping.flight_offers_search.post(destinations.to_json)

Getting error text on the client when a model does not save

I am using the Hyperstack.org framework, so working with Opal compiled Ruby code. Hyperstack's integration with Rails creates a representation of some of the Models on the client and I have a question about error validation in the response.
When saving a Model with validators, when one of the validators is triggered I am unable to get the full error message in the promise response.
In this code:
#User.save(validate: true).then do |result|
if result[:success]
puts 'successs'
mutate #open = false
else
result[:models].each do |response_model|
puts "response_model.errors.class = #{response_model.errors.class}" #ActiveModel::Errors
puts "response_model.errors.full_messages = #{response_model.errors.full_messages}" #nothing puts
end
end
The first puts returns #ActiveModel::Errors but I seem unable to use the methods of that model.
I can see the tests for this: https://github.com/hyperstack-org/hyperstack/blob/a09bc601b3ad289c9f75156416ed39dff88059c9/ruby/hyper-model/spec/batch1/misc/errors_spec.rb#L340 so I would expect this to be working and it must be me!
Also, I have noticed that that JSON response to the promise does actually include the error message:
{
"success" => false, "saved_models" => [
[227154, "User", {
"id" => 48,
"first_name" => "ds",
"last_name" => nil,
"email" => nil,
"image_src" => nil,
"date_of_birth" => nil,
"is_admin" => false,
"is_female" => false,
"is_enabled" => true,
"created_at" => "2019-03-23T12:29:05.728Z",
"updated_at" => "2019-03-23T12:29:05.728Z"
}, {
"last_name" => ["can't be blank"]
}]
], "message" => "HyperModel saving records failed!", "models" => [ < User: 0x37752(0x37750)[errors {
"last_name" => ["can't be blank"]
}] > ]
}
Any help appreciated!
Looks like you hit a bug in HyperModel with the full_messages method. AFAIK all the other methods would work fine in your example.
If you look at https://github.com/hyperstack-org/hyperstack/issues/143 there is a work around patch to apply if you really need full_messages

Monkey Patch the Peddler Gem

This gem sends data to Amazon. However, its missing one data element that I need to send. Basically the declared value of the item. I am trying to monkey patch this method to also send the declared value.
The method I'm using inside this gem is create_fulfillment_order here is a link to the method, and the method is also pasted below. What I need to do is change the items struct from this:
(:seller_sku, :seller_fulfillment_order_item_id, :quantity)
to this:
(:seller_sku, :seller_fulfillment_order_item_id, :quantity, :per_unit_declared_value)
And then this is the complete code for the method
def create_fulfillment_order(seller_fulfillment_order_id, displayable_order_id, displayable_order_date_time, displayable_order_comment, shipping_speed_category, destination_address, items, opts = {})
if opts.key?(:cod_settings)
opts['CODSettings'] = opts.delete(:cod_settings)
end
operation('CreateFulfillmentOrder')
.add(
opts.merge(
'SellerFulfillmentOrderId' => seller_fulfillment_order_id,
'DisplayableOrderId' => displayable_order_id,
'DisplayableOrderDateTime' => displayable_order_date_time,
'DisplayableOrderComment' => displayable_order_comment,
'ShippingSpeedCategory' => shipping_speed_category,
'DestinationAddress' => destination_address,
'Items' => items
)
)
.structure!('Items', 'member')
.structure!('NotificationEmailList', 'member')
run
end
I call the method like this:
client = MWS::FulfillmentOutboundShipment::Client.new(
marketplace_id: "XXXXXX",
merchant_id: "XXXXX",
aws_access_key_id: "XXXX",
aws_secret_access_key: "XXXXX+",
auth_token: "XXXXXX")
Address = Struct.new(:name, :line_1, :line_2, :line_3, :city, :state_or_province_code,
:country_code, :postal_code)
address = Address.new("Todd T", "19712 50th Ave W", "#5", "", "Lynnwood","WA" ,"US" , "98036" )
Value = Struct.new(:currency_code, :value)
value = Value.new("CAD", "10")
Item = Struct.new(:seller_sku, :seller_fulfillment_order_item_id, :quantity, :per_unit_declard_value)
sku = []
first = Item.new("636391317719", "s2", 1, value)
sku << first
begin
client.create_fulfillment_order("z536", "z536", Time.now.getutc, "Thank You", "Standard", address, sku)
rescue Exception => e
pp e.response
end
The error I'm getting is The request must contain the parameter PerUnitDeclaredValue.

Google freebusy api call in rails will not recognizing parameters

I am trying to find all the freebusy times from my primary calendar, but I cannot get the query to recognize my parameters.
In my controller I have:
#freetimes = client.execute(
:api_method => service.freebusy.query,
:parameters => {
'timeMin' => '2013-06-15T17:06:02.000Z',
'timeMax' => '2013-06-29T17:06:02.000Z',
'items' => [{'id' => 'myemail#gmail.com'}]
},
:headers => {'Content-Type' => 'application/json'})
the response I get is:
--- !ruby/object:Google::APIClient::Schema::Calendar::V3::FreeBusyResponse
data:
error:
errors:
- domain: global
reason: required
message: Missing timeMin parameter.
code: 400
message: Missing timeMin parameter.
However is shows that it took the parameters, but they did not get attached to the query:
--- !ruby/object:Google::APIClient::Result
request: !ruby/object:Google::APIClient::Request
parameters:
timeMin: '2013-06-15T17:06:02.000Z'
timeMax: '2013-06-29T17:06:02.000Z'
items:
- id: myemail#gmail.com
Any help solving this would be greatly appreciated!
solved this by specifying the request body
client.execute(
:api_method => service.freebusy.query,
:body => JSON.dump({
:timeMin => ,
:timeMax => ,
:items =>
}),
:headers => {'Content-Type' => 'application/json'})
I was having a similar problem. An alternative, is that you can build a FreeBusyRequest object.
Like this:
body = Google::Apis::CalendarV3::FreeBusyRequest.new
body.items = [calendar_id]
body.time_min = "2016-06-29T13:00:00z"
body.time_max = "2016-06-29T21:00:00z"
body
```
and then you can pass it into a CalendarService object like this:
service = Google::Apis::CalendarV3::CalendarService.new
service.authorization = client
service.query_freebusy(body)
this will definitely return a status 200 response with a body.

json format from rails to sproutcore

For those of you using Rails as a backend to their Sproutcore clients,
which one is the best way to format the data into json?
From the Sproutcore guides there was this approach:
def as_json(options = {})
event_hash = {
"guid" => self.id,
"id" => self.id,
"designation" => self.designation,
"category" => self.category,
"scheduled_for" => self.scheduled_for,
"location" => self.location,
"groups" => self.groups,
"resources" => self.resources
}
event_hash
end
But it fails, send an "Illegal statement error". Then, I changed to this other method:
def as_json(options = {})
# event_hash = options.merge(:include => [:groups, :resources], :methods => :guid)
event_hash = options.merge(:methods => :guid)
super(event_hash)
end
which seems to be working as far as the formatting is concerned, although I am suspecting it to causing some trouble regarding the representation in the dataHash of the store. Anyway, ha anyone been having similar issues with the first version of as_json? If not, is there anything I am doing wrong?
Appreciate any help
On the first method you need to call super:
def as_json(options = {})
event_hash = {
"guid" => self.id,
"id" => self.id,
"designation" => self.designation,
"category" => self.category,
"scheduled_for" => self.scheduled_for,
"location" => self.location,
"groups" => self.groups,
"resources" => self.resources
}
super(event_hash)
end
However you should get the options param and process to do this apropiately.

Resources