SevenSpikes / nopCommerce-Api-SampleApplication

This project demonstrates the OAuth 2 Authorization Code grant type process against the nopCommerce API
https://github.com/SevenSpikes/api-plugin-for-nopcommerce
GNU General Public License v3.0
88 stars 62 forks source link

No examples of POST/PUT/DELETE #5

Closed alaingmail closed 7 years ago

alaingmail commented 7 years ago

Hi,

Are you able to add POST/PUT/DELETE examples of Api plugin usage to this sample application?

This request relates to an issue I posted for the Api Plugin repository which is causing errors within the plugin.

Regards, Alain

poyker commented 7 years ago

@alaingmail

I just added sample code that demonstrates how you can update a customer.

Please see this commit: https://github.com/SevenSpikes/nopCommerce-Api-SampleApplication/commit/79a539e041cf4404d4dda64ef12f4338aeb95994

Your feedback is welcome!

alaingmail commented 7 years ago

Thanks Boyko,

The sample code for customer add and update worked when using simple anonymous types. I repeated this approach for adding and updating products. It worked as well.

I then created a product dto object. It's class was copied from the api plugin project (ProductDto) and wrapped it in another class called ProductRootObjectDto for it to serialize into a JSON objec:

Here is what it looks like when serialized before posting it:

{"product":{"id":null,"visible_individually":null,"name":"Test Product","short_description":"Test short description","full_description":"Test full decsription","show_on_home_page":null,"meta_keywords":null,"meta_description":null,"meta_title":null,"allow_customer_reviews":null,"approved_rating_sum":null,"not_approved_rating_sum":null,"approved_total_reviews":null,"not_approved_total_reviews":null,"sku":null,"manufacturer_part_number":null,"gtin":null,"is_gift_card":null,"require_other_products":null,"automatically_add_required_products":null,"is_download":null,"unlimited_downloads":null,"max_number_of_downloads":null,"download_expiration_days":null,"has_sample_download":null,"has_user_agreement":null,"is_recurring":null,"recurring_cycle_length":null,"recurring_total_cycles":null,"is_rental":null,"rental_price_length":null,"is_ship_enabled":null,"is_free_shipping":null,"ship_separately":null,"additional_shipping_charge":null,"is_tax_exempt":null,"is_telecommunications_or_broadcasting_or_electronic_services":null,"use_multiple_warehouses":null,"stock_quantity":null,"display_stock_availability":null,"display_stock_quantity":null,"min_stock_quantity":null,"notify_admin_for_quantity_below":null,"allow_back_in_stock_subscriptions":null,"order_minimum_quantity":null,"order_maximum_quantity":null,"allowed_quantities":null,"allow_adding_only_existing_attribute_combinations":null,"disable_buy_button":null,"disable_wishlist_button":null,"available_for_pre_order":null,"pre_order_availability_start_date_time_utc":null,"call_for_price":null,"price":null,"old_price":null,"product_cost":null,"special_price":null,"special_price_start_date_time_utc":null,"special_price_end_date_time_utc":null,"customer_enters_price":null,"minimum_customer_entered_price":null,"maximum_customer_entered_price":null,"baseprice_enabled":null,"baseprice_amount":null,"baseprice_base_amount":null,"has_tier_prices":null,"has_discounts_applied":null,"weight":null,"length":null,"width":null,"height":null,"available_start_date_time_utc":null,"available_end_date_time_utc":null,"display_order":null,"published":false,"deleted":null,"created_on_utc":null,"updated_on_utc":null,"product_type":null,"parent_grouped_product_id":null,"role_ids":null,"discount_ids":null,"store_ids":null,"manufacturer_ids":null,"images":null,"associated_product_ids":null,"tags":null,"vendor_id":null,"se_name":null}}

I then get the same error as described by my post in https://github.com/SevenSpikes/api-plugin-for-nopcommerce/issues/14.

Can you supply an example using populated dto objects which get serialized before making post/put calls? This would be more of a real world example.

Regards, Alain

poyker commented 7 years ago

@alaingmail

You get these validation errors because you have most of the product properties set to null in the JSON when you don't set them in the ProductDTO. If you are going to use ProductDTO that has the same properties then you should ignore the null values as otherwise the server will not be able to validate some of them as they could not be null. That is why the API allows you to specify only certain properties and the rest will be ignored (not validated) just like the example with the anonymous objects. The easiest way to do this if you are using ProductDTO (with the same properties as the one used in the API) is to ignore null values. Please refer to this example how to serialize to JSON and ignore the null properties.

alaingmail commented 7 years ago

Boyko,

Thank you again for your help. Your insight into the problem and suggestion on how to solve the problem was perfect. All I had to do was ignore the properties with null values when serializing the product object as shown here:

string productJson = JsonConvert.SerializeObject(productRootToAdd, 
     Formatting.None, 
     new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

var response = nopApiClient.Post(jsonUrl, productJson);

All the best, Alain