Whats the most RESTFul way to add resources? - api

For example, if there is a rest api for a shop system to add articles to my shopping cart without knowing the id, what would be the most restful way to design the url to call and what about the serverside implementation? I know there is some flexibility when designing rest apis, but what is "state of the art"?
Lets say my database does have the table "articles", "shoppingcarts", "article_pos".
articles (id, article_name, price, color),
shoppingcarts (id, description),
article_pos (id, shoppingcart_id, article_id)
Example to create an article:
POST "/api/shop/article/"
Request:
{
"article_name": "table",
"price": "100.00",
"color": "brown"
}
Example to get an existing article:
GET "/api/shop/article/4711"
Response:
{
"id": "4711",
"article_name": "table",
"price": "100.00",
"color": "brown"
}
But how I should design the url if I want to add article "4711" to the shopping cart?
Like this?
POST "/api/shop/shoppingcart/addArticle/4711?amount=1"
or Like this?
POST "/api/shop/article_pos/"
Request:
{
"shoppingcart_id": "1",
"article_id": "4711"
}
And how to get the article ID if a client doesn't know the ID (4711) but the articlename (table)?

But how I should design the url if I want to add article "4711" to the shopping cart?
POST /api/shop/shoppingcart/addArticleById/{4711}
fetch article details from article table, get the amount.
update shopping cart with these details
how to get article ID if a client doesn't know the ID (4711) but the articlename (table)?
POST /api/shop/shoppingcart/addArticleByName/{table}
fetch article details from article table, get the amount.
update shopping cart with these details
State Of the Art:
indentify resource by ID only, and use it for creating, updating details.
REST -> deals with resource first, hence a resource is first class citizen identified by a Id.

Related

How to get the products in a category thats based on Dynamic Product Groups from the shopware API

I have a high volume shop with a lot of products and categories.
The products are seasonal, so a lot of new products are added.
Because of this, dynamic product groups are perfect. We set conditions and add the group to the category.
Right now i'm trying to get all the products of the category in the api's (both backend and store), but i dont get any product back. I do get a streamId back in the store-api https://localhost//store-api/product-listing/9cf2c65bd7084144aa4d4a35bb3f568e But there is no endpoint i can call for the streamId.
Next to that, the breadcrumbs are also missing on the front-end.
What is the best way to get a the products from a category back in the API?
Thanks!
If you do have the id of the product_stream you can filter product entities by them having that id as part of their streamIds:
// POST /api/search-ids/product
{
"page": 1,
"limit": 500,
"filter": [
{
"type": "equalsAny",
"field": "streamIds",
"value": [
"65103ca79e1e4e3ba846f551fbb1cb36" // your product stream id
]
}
]
}

Directus - how to show Many2Many relationship in api

I am trying to make a blog in Directus.
I created Blog Collection and Categories Collection.
I am using junction table with many to many relationship to connect categories to blog.
I can create a new blog post and select category from category collection and I can add a new category also.
But when I am calling in api, it is not showing the category field in my rest api call.
I can call category endpoint and view the list of all the categories in rest api but in blog collection endpoint, I cant view related category name.
I want to view all the selected category name to each blog posts in my rest api.
And I want to view all the blog posts when I call category collection in my rest api by putting category name like projectname/items/categories/aliens/ so I can see all the blog posts of aliens categories.
Currently I am getting this by calling my blog collection in json -
{
"data": [
{
"id": 1,
"status": "published",
"owner": 1,
"created_on": "2020-03-16 21:15:25",
"blog_title": "first blog post title",
"blog_content": "<p>sdlkjfhsdkfol8ysdmfhj sd,fh klsdhf msoiudf oihsdyfghm soreht4,7shetc,oiuh,scenclkr</p>\n<p><strong>dsfgdsfgds</strong></p>"
}
]
}
How to get the category name on rest api ?
Did you try using the fields parameter? This is used to fetch deeply nested relational data:
?fields=*.*.*
In this example, the asterisk is a wildcard for_all_ fields at that relational depth... so this fetches three levels deep.
https://docs.directus.io/api/query/fields.html
You should have a relational table created between those 2 tables so you should be able to filter the information like this ?fields=id,blog_title,blog_content,category.category_id.category_name,category.category_id.status
In case this is confusing let me try to simplify the relationship part:
category.category_id.category_name
<field_name_on_current_item>.<identifier_on_junction_table_related_to_category_id>.<relation_field_name_to_display>
I realise that this is an old post, but it might help someone looking for the answer.
To select only the relational data use
?fields=category.*
This shows only the related category field of the blog article.

In Amadeus self service APIs, is price.total inclusive of all taxes and fees?

For the Amadeus self-service rest APIs: v1/shopping/flight-offers and flight-dates, is the price.total response field inclusive of all taxes and fees?
I seem to need to add total + totalTaxes to get a price that's close to the one provided by the airline directly.
If there are developer docs that explain in more detail each response field, please do point me to them. I have been unable to find any.
In reference to these APIs:
https://developers.amadeus.com/self-service/category/203/api-doc/4/api-docs-and-example/10002
https://developers.amadeus.com/self-service/category/203/api-doc/5/api-docs-and-example/10003
and this field:
"price": {
"total": "259.91",
"totalTaxes": "185.91"
},
“total” is the total price you will have to pay, it includes the totalTaxes.
You can access the model directly on Swagger (under the Response Class (Status 200)) you have the Model button. But you are right the description of those parameters need to be added, point taken.
As highlighted in the accepted answer, the total price is the total including all taxes, personally, I was also initially confused and had to manually check with a travel agent to confirm. I would suggest Amadeus splits the description as follows
"price": {
"totalFare": "74.00",
"totalTaxes": "185.91"
"totalAmount": "259.91"
},

How to retrieve all products from a Smart Collection through Shopify API?

What's the best way to retrieve all products from a Smart Collection through Shopify API?
Thanks!
Update: API version 2020-04
See Codesmith's answer below. As of API version 2020-04, you can now use a smart collection id with the collections endpoint:
GET /admin/api/2020-04/collections/{collection_id}/products.json
See the Collection API docs for more info.
Pre-2020 solution
From the SmartCollection page in the Shopify API docs:
Smart Collections also use collects to connect a product to that smart collection.
And on the Collects page:
List only collects for a certain collection
GET /admin/collects.json?collection_id=841564295
HTTP/1.1 200 OK
{
"collects": [
{
"collection_id": 841564295,
"created_at": "2014-05-23T14:16:47-04:00",
"featured": false,
"id": 1071559648,
"product_id": 921728736,
"sort_value": "0000000001",
"updated_at": "2014-05-23T14:16:47-04:00",
"position": 1
},
...
]
}
The collects contain the product_ids for each of the products in the collection with the specified collection_id.
2020 Update,
For the latest API (2020-04 as of now), you can simply use the /collections/ endpoint, using the Smart Collection ID:
GET https://<shopname>.myshopify.com/admin/api/2020-04/collections/<smart collection id>/products.json
More details can be found in Shopify's Collection API Docs.

Use a single POST request to update to create two objects Bad API design?

Consider the scenario, an unknown unauthenticated user is looking at the list of nerddinners and then goes to a particular dinner, enter his name and email and clicks "Attend". This should result in two things. Create the user and create the DinnerAttendRequest for that user.
The user also has a property called FavProgLanguage which is set to the prog language property of the dinner which he wants to attend.
Assuming it is a single page javascript app which talks to an API, there are two approaches which come to mind.
1) On the client, set the users FavProgLanguage and then POST to /user with name, email and favproglanguage to create the user. Use the created UserId and POST to /DinnerAttendRequest with DinnerId and UserId to create DinnerAttendRequest.
2) POST to /somename with Name, email and dinnerId and then use dinnerId at server to populate favproglanguage of user. create user and then use userid to create DinnerAttendRequest
The first approach seems more natural/RESTful, however if the logic of computing the favproglanguage is a bit complex, all the api consumers would have to implement that logic and with the second approach that code is written just once on the server.
Which is a better approach? Is the second approach RESTful?
Your 1st design would place the burden of logic, workflow and the fav lang decision, upon the client, this would make handling the user creation and reservation a single transaction difficult and something that a client app would need to orchestrate. Your fav lang logic sounds like an important business rule that again should ideally sit at the server for re-use...
Why don't you look at having some resources like so:
Dinner e.g. { "name", "date", etc. }
Booking e.g. { "user" { NESTED USER RESOURCE }, "bookingStatus", etc. }
User e.g. { "email", "name", "fav lang", etc.}
Some example urls
/dinners/{uid}
/dinners/{uid}/bookings
/users/{uid}
Basically I would POST a Booking resource containing a nested User resource to the dinner bookings url and run the logic for checking is a user exists, creating if needed and updating their fav lang in a transaction.
So to create a booking I would POST a Booking Resource:
{
"user": {
"email": "john#doe.com",
"name": "name"
},
"bookingStatus": "requested"
}
To /dinners/{uid}/bookings
And expect a 201 created response with a response like this:
{
"uid": "4564654",
"user": {
"uid": "1234564",
"email": "john#doe.com",
"name": "name",
"favLang": "C#"
},
"bookingStatus": "booked"
}
Obviously the properties are largely just for example but hopefully this demonstrates some of the concepts and shows that a single POST can be considered RESTful...