Correct way of using REST DELETE? - rest

I am trying to delete some items connected to a product. Like this:
DELETE /products/Special product/11
So item 11 in "Special product" will be deleted? Is this a correct way of using the DELETE verb? I should probably url encode Special product right?
Special product is not stored in a DB. So I can't use some id. I have to write it out like this (alpha+digits).

If
/products/Special prodcut
is the collection resource of all "special products", and if
/products/Special product/11
is the "special product" resource with ID 11, then your usage of DELETE is RESTful.

As far as I understand, you have a collection of products
/products
You are selecting the "special product" (its some kind of an identifier?)
/products/special product/
This one product contains multiple "items"? And you want to delete the item connected to "special product" with the id=11? If this is the case, the uri should look more like
DELETE /products/special product/items/11
As Example, you have albums with images. To get all albums:
GET /albums
To get one album
GET /albums/{albumId}
To get all images for one album
GET /albums/{albumId}/images
To delete/remove one image from one/this album
DELETE /albums/{albumId}/images/{imageId}

Related

Determine Correct JSON Fields for REST API based on Endpoint and Screen names (Customers & Sales Orders) - Acumatica

My project is to import Customers and Sales orders. I take a CSV file from an external program, and am to import them -- field by field -- into Sales Orders and Customers.
My challenge is determining the correct field names to get the REST API to work correctly.
I have the following fields:
I am looking to include the three fields: Description, Gift Message, and Public Comment. I inspect the element, which gives me nothing useful that I can tell. (i.e. none of the values map to the field name that I would use in JSON.)
When I go to the Endpoint for Customers, Description already exists:
And therefore, this JSON Record succeeds in an ADD or UPDATE:
{
"OrderNbr" : {"value": "SO003525"},
"Description" : {"value": "This is the Description"},
}
I extend the default customer (version 18.200.001) and I find and add the "Gift Description" and the "Public Comments" -- and they are listed as part of the "Order Summary" -- which is exactly where Description was.
However, this JSON data does add anything to Gift or Public:
{
"OrderNbr" : {"value": "SO003525"},
"Description" : {"value": "This is the Description"},
"PublicComment" : {"value": "This is the Public Comment"},
"GiftMessage": { "value": "This is the Gift Message"},
}
In talking to the (in-house) developer who set up the screen, he told me that the "Gift Message" and "Public Comments" fields were added on. But they are not showing as "User Defined" fields in the Endpoint.
Questions:
1) How can I know what the JSON structure should look like? Is there some sort of a mapping document, or a printout that shows JSON-to-Endpoint data? What I mean is that sometimes I need to use a sub-object to reference data in JSON, for example, if I want to update "Main Contact" information, I do it like this:
{
"CustomerClass":{"value":"INDIVIDUAL"},
"CustomerID":{"value":"78577"},
"CustomerName":{"value":"TEST CUSTOMER "},
"MainContact":
{
"CompanyName":{"value":"TEST CUSTOMER COMPANY"},
"DisplayName":{"value": "TEST CUSTOMER DISPLAY"},
}
}
How can I know to do that? (I know only because I saw it in the data that got returned when I added a record... Which brings up a second question:
2) Is there a way for retrieving an entire data record in its JSON format. Sort of like a : "GET - SELECT ALL - EXPAND ALL" so I can see all of the data in a record in a properly set up JSON format?
3) Any idea why the gift Message and Public Comments are showing as "Order Summary" but aren't updating like Description is?
4) Why are some things "un-expandable?" For example, I cannot expand Payments in Sales Order. I get an error:
1) How can I know what the JSON structure should look like
The easiest way is to execute a GET request. The field names are equivalent to the DisplayName property stripped of whitespaces. So if on screen the field is labelled Order Type the field name will be OrderType in the request.
To make a GET request, the URL format is /Entity/Keys.
For example entity SalesOrder has two keys: OrderType and OrderNbr.
To get entity SalesOrder of type SO and with the number SO005051 the URL would end with: /SalesOrder/SO/SO005051.
2) Is there a way for retrieving an entire data record in its JSON
format. Sort of like a : "GET - SELECT ALL - EXPAND ALL" so I can see
all of the data in a record in a properly set up JSON format?
If you want all the details, you need to specify them all in the expand clause of the GET query: $expand=Contacts,MainContact,BillingContact
The name of the details array can be looked up in the Web Service Endpoint screen SM207060. They are the tree view elements with a empty array [] suffix.
3) Any idea why the gift Message and Public Comments are showing as
"Order Summary" but aren't updating like Description is?
Those are custom fields. You need to extend the endpoint to add those custom fields otherwise they will be ignored. If there's no issue with the custom endpoint maybe the field aren't bound to database.
But they are not showing as "User Defined" fields in the Endpoint.
If by User Defined you mean those fields were added with a wizard (without programming) then this is likely the reason why they don't work in the web service. You would need to add custom field with a DAC extension which requires programming instead of the User Defined field.

RESTful API desgin: retrieve number of items in category

I want to create a REST API with items in categories and list all categories alongside its number of items.
Schemas:
Category {id, name}
Item {id, name, categoryId}
Endpoints:
GET /categories/list
GET /categories/<id>
PUT /categories/<id>
GET /items/list[?category=<categoryId>]
GET /items/<id>
To update a category I take what I get from GET /categories/<id>, modify the JSON object and PUT it back.
So far so good.
My question is if there are one ore more best practices to retrieve the item count?
I can think of a few ways to do this:
Fire a GET /items/list?category=<categoryId> for each category, counting the resulting items.
Taking the count from a X-total-count or content range header or a total_count field returned from the endpoint will avoid having to actually load all items.
Add an item_count field to the resulting category JSON objects.
How should this read only field be handled for PUTs? Make the backend ignore it? Manually unset it?
Create a dedicated endpoint /categories/item_counts that returns a list of categories with each number of items.
I like option number 2 (e.g. the wordpress API does it this way) because it does not need extra requests. But I really dislike the idea of having a different object structure for the GET and PUT requests.
REST is really about representation of objects. Category doesn't have a count as it's a single object. Item doesn't have a count either for the same reason. Count is more like RPC where you tell the service to compute something.
GET /items/list[?category=<categoryId>]
is like RPC, passing the category parameter to the list method. Staying in that idiom, you could "chain" the methods to get the total count of items in the specified category:
GET /items/list/count[?category=<categoryId>]
although I'd use path parameters instead:
GET /items/list/<category_id>/count
but list is implied so you could remove it:
GET /items/<category_id>/count
It's straying a bit from "pure" REST but it keeps your actual REST objects clean, as you say, keeping total_count our of your Category objects.
I'm assuming you sometimes need the count but not all the Items otherwise you wouldn't need to ask the API for the count, you'd just count them yourself in the client. That suggests another option:
GET /categories/<id>/count
{
"total_count": 10
}
which fits better with the use case of finding out how many Items are in a Category.

Is there a way to assign the primary category for a product in demandware using the Open Commerce API (OCAPI)?

The primary category of a product is present in the product document (primary_category_id) in the DATA API but cannot be written. After sending a PATCH update of the product with a different primary_category_id, it doesn't change.
Is there a way of doing this through the OCAPI?
Can be some limitation for PATCH Method.Fields that can be updated:
name,
page_description,
long_descripton,
page_title,
page_keywords,
brand,
ean,
upc,
manufacture_sku,
manufacture_name,
searchable,
unit,
searchable,
online_flag,
default_variant_id.
Try with PUT Method. PUT https://hostname:port/dw/data/v19_1/products/{id}. Also,
please check Request Document.
At this time it does not appear that this is possible to manage via OCAPI.
I suspect that in the future you'd be able to achieve it using the following resources:
DELETE /catalogs/{catalog_id}/categories/{category_id}/products/{product_id}
followed by:
PUT /catalogs/{catalog_id}/categories/{category_id}/products/{product_id}
With a ProductCategoryAssignment document in the PUT call.
However, this would require that Salesforce adds those attributes to the ProductCategoryAssignment document.
The reason I suggest this is where it would be added is that within a catalog import document (XML) the flags are associated with a similar resource representation. eg:
<category-assignment category-id="gear-bags-backpacks" product-id="NSF4003100">
<primary-flag>true</primary-flag>
</category-assignment>

How to return only selected items from Pods Framework?

Just started using the PODS framework for Wordpress. Most of it I picked up pretty fast, the rest I'm struggling with. Below is an example of my confusion:
I created a custom post (Partner Bio) with auto-templates turned on.
I created a pods-template (Bio for Industry) that is sourcing post (Partner Bio).
I created another custom post (Industry) with multi-select post-type relationship field to post (Partner Bio).
The issue I'm having is that when I create an instance of post (Industry), then select specific items from that relationship (Partner Bio), (say there are 10, but I chose 3), I'm getting data returned for all of Partner Bio's instead of just the three I chose, so in other words the template pulling data from all Partner Bios regardless of what I choose in the post field drop down.
The template I'm using looks like this:
<div>{#post_thumbnail.thumbnail}<br/>
{#first_name} {#middle_initial} {#last_name}<br/>
{#title_of_partner}<br/>
{#position}<br/>
{#phone}<br/>
Email {#first_name}
</div>
This made me think that when you embed a pod usinge a shortcode, it's not being controlled by the fields selected when you edit your post. So then I tried grabbign the code from the template and then just dropping it in the post, that did not work either.
My goal is to display the block listed above one under another, based on which bios I choose. What am I doing wrong?
If my example above is confusing, here's a simpler sample illustrating the same issue, this maybe more clear:
Post A structure/fields:
Name,
Email,
Rel, // Post A's Relationship via post-type to Post B,
Auto templates: on,
Pods Template created: {#name}{#Emal}{#Image}
The issue, is when I create an instance of Post A and select items from the Rel, I'm getting values pulled in from all members of that relationship rather than just what I checked off.
If I turn off auto templates and drop in magic tags into the instance of Post A, I do get back only data for the relationship items I chose but I ge it back in this format:
Name and Name and Name
Emaile and Email and Email
Rather than:
Name
Email
Name
Email
...

Filtering and ordering

I am building a news feed app and I need to filter news by category while ordering them by data.
The only way I was able to achieve this was by duplicating the news, one for a category name 'All' and another for the category it belongs.
Is there a way filter by category and order by the timestamp?
Edit:
this is an example of my db structure.
news
-All
-category: "category1"
-sortDate: -1477585147
-Category1
-Category2
-Category3
Doing this way i am able to filter as i want, but i have to duplicate all the news.