Magento 2 REST API - Creating a new cart deactivates old one - magento2

I'm using the Magento 2 REST API to process orders. I'm using the admin token to handle all the API calls.
Problem is that if I get two requests at the same time, the first request fails because the second one creates a new cart.
I'm wondering if there is a way to work with multiple carts at the same time?
I'm able to get the first cart details and the value of is_active is false.
I'll have to add a queue if there is no option.

Related

Discuss - How to make an application registration and OTP generation/verify service to be RESTful

We are developing a portal application where an already existing customer can register their account to see the details of their account (something like you have a credit card and then you register on Bank's portal to see the transactions details - here you are already a customer of the Bank). So when a user is coming very first time for registration then this whole registration flow is not authenticated (as user still doesn't have a username and Password- one will have this after the registration)
We also want that a customer can not have concurrent registrations i.e. if a customer opens multiple tabs (or uses Postman to call our registration API) then only one request should be allowed to register and all other be rejected. For this we have used a registration_session.
So when the first request comes, we find the customer from our master record and generates a GUID/UUID and save it as a registeration_session value against CustomerId as key in Redis (with set expiration). So if any other registration request comes for the same customer then we first search the Redis to see if there is a registeration_session value against the CusrtomerId, and if it exists we will reject this request saying that registration is already in progress.
Now my first question is this: is this behavior is Stateless or not for RESTful APIs? as I am kind of maintaining a request context via registration_session on the server. One may argue that I am not maintaining the application state, true; but if we see it like this: every other registration request has to check the status of any previous registration request - then this means we are no longer having statelessness as per REST principles as now two requests for registrations are no longer independent of each other.
Next requirement is of OTP generation and verify. During registration, we ask the user to identify oneself via an OTP sent to their mobile number (we already have their mobile number from their customer record). A user may request to resend a token multiple times but if use provides wrong input for 3 times, we will put the user's account in locked status. Also we want that once user has verified the OTP check successfully, then any other OTP generation request for the same registration session should not be allowed - as once OTP is verified successfully then to generate OTP again is futile operation (a malicious user may still want to do this via Postman/curl)
Now server has to maintain following information for OTP:
retry count for OTP verification, as the moment it reaches 3, account is to be locked.
verification status for OTP i.e. once it is verified, other request to generate the OTP for same registration session is not allowed
and my second question is this: Is it again violates the REST stateless principal, as it seems we are maintaining the context for requests and every request is dependent on the context of the previous request?
Or is there a gap in my understanding of application state and stored context and above mentioned scenarios do not break Restfulness of an API? OR we can not design Restful APIs for the above mentioned requirements?
Note: I have read enough questions on REST and Context state on SO, but none offered a solution that eliminates my confusion for the specific scenarios that I have asked.

Rest API to a Magento 2 Site

Is there a way to make a REST API call to a website built in Magento 2? The scenario is I have multiple site and the header of these sites are shared whether I go to website 1 or website 2. Now when I go to website 1 and select/add an item to the cart I would like this cart count to be available in the other websites. So I was thinking having an API call to a certain website then retrieve that value to be displayed.
But I'm not sure if this is the best approach or if there is available REST API resource I can use to retrieve information of a website data. Like if I'm in website 2 I would like to get the information of website 1 I would just send a REST request to retrieve this information.

Creating a ShoppingCart using API's in Unity

I Have created a ShoppingCart in Unity using rest APIs. I am using the Intershop rest APIs. I have just fololwed this Reference Link
So there is a web-site which is already running on online which is a E-Commerce Web-Site in that some products are displayed as regular web-site's view only and some of products are displayed in 3D view which is created in Unity. i am just i have written backed-end C# code that is also for create a ShoppingCart. i have written the code for ShoppingCart First i have generate a basketId with that authentication-token has generated and also authorization if users logged only, with that BasketId, Authentication-Token, and Authorization under the these i have developed the code to adding item to cart and also successfully removing item from cart.
So here my problem was when i add the item to cart it was not showing in my WebSite cart, so how could i solve this problem.
You cant mix stateless rest api call with stateful web session calls. The stateful web base shop wont be able to access the basket you create using the rest apis out of the box.
So either only use the rest api or implement the answer from Nils on the intershop server. The last approach you'll need access to the intershop server.

Populating new form data from REST api

I've come from a background of ASP.Net MVC where when the user wants to create a new entity the server returns values to populate drop down lists(for example).
Now I'm doing a UI that is invoking a REST Api. I have my urls for creating/retrieving etc but this is purely for actual resources. How would the REST be defined to get data to populate the create form.
For example:
a user wants to create a new order. They go to the 'create order' screen and need to select their payment method. They have 3 payment methods (card, paypal, amazon) but the logic on the server side knows that amazon cannot be accepted on this order. How would I go about letting the UI know what can be populated in the drop down list using REST?
I can't seem to get this to fit into REST principles but then I don't want the user to submit amazon and then the server return a Bad Request, just seems bad implementation.
Thanks

Uber sandbox api returns duplicate ride request id for mutiple ride requests

I have created a session with sandbox_mode = True, with all scopes, including request, using uber_rides python library. The api calls work fine, but I get duplicate request_ids for mutiple ride requests(fired one after the other, and not concurrent). Though these requests are using the same lat, longs and product_id. Can it be possible that uber creates a ride id using lat, long ?
Is this okay ? Or am I actually missing something, and this should never happen ?
I think the issue you might be running into is that if you are already on a trip and you try to create a new one, the API will just return the trip details for your current trip. So I think what is happening is you're creating one trip, then trying to create another and you're getting back a "processing" status so it looks like you created a new trips, but its actually still the first trip.
You can verify this by creating a trip, checking the UUID, canceling the trip, then requesting a new one and seeing if the UUID is different.
Best,
Riche
product_id is based on the start_latitude/start_longitude or start_place_id parameters.
request_id is not (it's just a UUID identifying the ride request)
If you are doing multiple ride requests with the same OAuth Bearer token and you do not cancel the first ongoing ride request you are basically getting back the status details of the current ongoing trip.
According to Uber Help - CAN I REQUEST MORE THAN ONE UBER?
At this time, it’s not possible to request more than one ride from a
single account.
If your party cannot be accommodated by a single vehicle, have
multiple people in your group request rides. We offer vehicle options
accommodating up to 6 people in many cities.
If no one else in your group has an Uber account, you can invite them
to sign up from the main menu of your app. As a bonus, both you and
your friend will receive the referral promotion.
This also applies to the Uber API.
However, from my use of the Uber API I've noticed the following rate limiting headers sent back on some responses:
X-Rate-Limit-Limit-Concurrent-Trips: 10
X-Rate-Limit-Remaining-Concurrent-Trips: 10
so they probably plan to add the possibility of making concurrent ride requests from the same Uber rider.