Rest Apis for mobile apps

How to configure and adapt Aimeos based shops as developer
Forum rules
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
traiyani75
Advanced
Posts: 114
Joined: 08 Nov 2019, 11:56

Rest Apis for mobile apps

Post by traiyani75 » 08 Nov 2019, 12:04

Hello, We are using your laravel package for eCommerce app. Now, we need to integrate eCommerce into our mobile applications.
For that, we need JSON apis to return data. We checked this document too
https://aimeos.org/docs/Developers/Client/JSONAPI

But From this, we are not getting information which we want to show in mobile apps. Like we need to show a basic flow of eCommerce beginning with category display to order checkout. Can you help us to prepare all APIs for that?

paravista
Posts: 4
Joined: 15 Feb 2017, 20:41

Re: Rest Apis for mobile apps

Post by paravista » 08 Nov 2019, 15:28

I am not too much into Laravel, but I have it working for a TYPO3 site.

You bascially need to find the API route for the controller "Jsonapi".
Maybe just start by calling this route (of course needs to be adapted to your installation / system):
http://testshop.local/products/api

To get products listet:
http://testshop.local/products/api?ai[resource]=product

or to get a certain product:
http://testshop.local/products/api?ai[r ... t&ai[id]=1

For TYPO3 the GET parameters need to be prefixed by "ai". This might be different for Laravel. But you can see the "prefix" within the meta data of the response.

Hope this helps,
Michael

User avatar
aimeos
Administrator
Posts: 7836
Joined: 01 Jan 1970, 00:00

Re: Rest Apis for mobile apps

Post by aimeos » 09 Nov 2019, 11:28

For Laravel the API entpoint is /jsonapi and no parameter prefix is required. If you are using more than one shop/site and you've changed the route, it might be also /{site}/jsonapi. The JSON API route is defined here:
https://github.com/aimeos/aimeos-larave ... #L102-L137
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

traiyani75
Advanced
Posts: 114
Joined: 08 Nov 2019, 11:56

Re: Rest Apis for mobile apps

Post by traiyani75 » 12 Nov 2019, 07:21

We have base urls for all. But we want to create special apis which will be helpful to ecommerce mobile application side.
Currently what is happening that response structure is not specialize for mobile apps.
Like if we call for catalog APIs with media and text include, then it returns data well. But in that, it returns all data into the main included object, instead of returning property into a nested object.
For example. For car category, I defined a name, desc, and image.
Now response like this
{
"id": "2",
"type": "catalog",
"attributes": {
"catalog.code": "cars",
"catalog.label": "Cars",
"catalog.config": [],
"catalog.status": 1,
"catalog.hasChildren": true
},
"links": {
"self": {
"href": "http://ebdaa-ecommerce-dev.mzadqatar.co ... talog?id=2",
"allow": [
"GET"
]
}
},
"relationships": {
"text": {
"data": [
{
"id": "127",
"type": "text",
"attributes": {
"catalog.lists.id": "13",
"catalog.lists.domain": "text",
"catalog.lists.refid": "127",
"catalog.lists.datestart": null,
"catalog.lists.dateend": null,
"catalog.lists.config": [],
"catalog.lists.position": 0,
"catalog.lists.status": 1,
"catalog.lists.typename": "Standard",
"catalog.lists.type": "default"
}
}
]
},
"media": {
"data": [
{
"id": "33",
"type": "media",
"attributes": {
"catalog.lists.id": "307",
"catalog.lists.domain": "media",
"catalog.lists.refid": "33",
"catalog.lists.datestart": null,
"catalog.lists.dateend": null,
"catalog.lists.config": [],
"catalog.lists.position": 0,
"catalog.lists.status": 1,
"catalog.lists.typename": "Standard",
"catalog.lists.type": "default"
}
}
]
}
}
}
now it has the image and other text. but if we need those text then we need to go through full response. and we need to find media with "catalog.lists.refid": "33",

so, it will be found like this in the bottom of the response
{
"id":"33",
"type":"media",
"attributes":{
"media.id":"33",
"media.domain":"catalog",
"media.label":"1600img.jpg",
"media.languageid":null,
"media.mimetype":"image/jpeg",
"media.type":"default",
"media.typename":"Standard",
"media.preview":"preview/a/0/a07b040d08e6372b4704bb4e5b1a4953.jpg",
"media.url":"files/1/d/1d92635586c6dfef5cad862c58007a48.jpg",
"media.status":1
}

User avatar
aimeos
Administrator
Posts: 7836
Joined: 01 Jan 1970, 00:00

Re: Rest Apis for mobile apps

Post by aimeos » 12 Nov 2019, 09:39

It's defined by the JSON:API standard. I think they wrote the specification in that way to avoid sending the same items several times which is very likely for e.g. product attributes. So this way the amount of traffic and data to be parsed is reduced which is more important than avoiding lookups for included data. You can get the included items quickly by using that code:

Code: Select all

let included = [];
const data = response.data || {};

if(data.included && Array.isArray(data.included)) {
	for(let entry of response.data.included) {
		if(!included[entry['type']]) {
			included[entry['type']] = {}
		}
		included[entry['type']][entry['id']] = entry;
	}
}
Then, lookups for an included item is very fast:

Code: Select all

let data = response.data.data;

if(data['relationships'] && data['relationships']['text'] && data['relationships']['text']['data']) {
	for(let rel of data['relationships']['text']['data']) {
		if(included['text'][rel['id']]) {
			console.log(included['text'][rel['id']]);
		}
	}
}
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

traiyani75
Advanced
Posts: 114
Joined: 08 Nov 2019, 11:56

Re: Rest Apis for mobile apps

Post by traiyani75 » 13 Nov 2019, 10:54

Hello, Thanks for your reply.

I have checked your loops, but on the android side, I can not create that kind of array. So, in that case I need to perform for loops to match the property with related relationships. I supposed that will take some time after receiving data from API response.

Can you suggest any other flow perform similar kind of loops which will be fast and efficient too?

User avatar
aimeos
Administrator
Posts: 7836
Joined: 01 Jan 1970, 00:00

Re: Rest Apis for mobile apps

Post by aimeos » 13 Nov 2019, 12:03

Android uses Java an there you can use Map objects instead (similar in iOS I think):
https://docs.oracle.com/javase/8/docs/a ... l/Map.html
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

traiyani75
Advanced
Posts: 114
Joined: 08 Nov 2019, 11:56

Re: Rest Apis for mobile apps

Post by traiyani75 » 15 Nov 2019, 03:43

Hello,
For catalogs hashmap works.
But for other things like products. It is being even more complicated in response. Can't we return in a simple way and easy to connect things instead of doing for loops.

For example, I have added one product in the category. In that product, I added one related product and add one product into brought together option. Now, I tried to get a response of products by category

http://ebdaa-ecommerce-dev.mzadqatar.co ... perty,text

You can check the above link, It is being very much confusing and complicated too to parse the response and displayed in mobile side. Can you please help us with how to handle things and what will be the solution to these confusions. ?

User avatar
aimeos
Administrator
Posts: 7836
Joined: 01 Jan 1970, 00:00

Re: Rest Apis for mobile apps

Post by aimeos » 15 Nov 2019, 09:03

There's no other way as long as you want to follow the JSON:API standard. You only have to use the same code for the "relationships" in the "included" section as you already do for the "data" section.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

traiyani75
Advanced
Posts: 114
Joined: 08 Nov 2019, 11:56

Re: Rest Apis for mobile apps

Post by traiyani75 » 15 Nov 2019, 09:58

Ok thanks.

Can you tell us all possible includes for products? For example, variants, stocks, characteristics, suggested products, similar products, etc.
Like I want to know all possible includes for products. Thanks.

Post Reply