Nutrition API - Mobile SDK interoperability
Food search
Mobile SDK: searchForFoodSemantic
API: https://api.passiolife.com/v2/products/food/search/semantic?term=$query
or
Mobile SDK: searchForFood
API: https://api.passiolife.com/v2/products/food/search/advanced?term=$query
The SDK adds the
Localization-ISO
header to the request if the locale code was set using the functionupdateLanguage
The API response provides two JSON arrays: search responses and alternate names (suggestions)
The search responses JSON is mapped to a
PassioFoodDataInfo
object using PassioFoodDataInfomapping
Image recognition
MobileSDK: recognizeImageRemote
API: https://api.passiolife.com/v2/products/sdk/tools/vision/extractIngredients
The SDK actually executes several steps before sending the request to the aforementioned API, which include barcode scanning, and OCR for nutrition facts recognition
If the SDK did not detect a barcode or nutrition facts in the image, it will proceed with the preparation of the request
The body of the request will contain a base64 encoded string of the native image
The SDK adds the
Localization-ISO
header to the request if the locale code was set using the functionupdateLanguage
The successful response is mapped to a list of
PassioAdvisorFoodInfo
objects, using PassioAdvisorFoodInfo mapping
Speech recognition
MobileSDK: recognizeSpeechRemote
API: https://api.passiolife.com/v2/products/sdk/tools/extractMealLogAction
The body contains the plain text passed as an argument to the recognizeSpeechRemote function
The SDK adds the
Localization-ISO
header to the request if the locale code was set using the functionupdateLanguage
The successful response is mapped to a list of
PassioSpeechRecognitionModel
objects, using PassioSpeechRecognitionModel mapping
RefCode result
MobileSDK: fetchFoodItemForRefCode
API: https://api.passiolife.com/v2/products/food/search/result/refCode/$refCode
The response contains the same JSON as the Food searchresponse, but here the alternatives field is not parsed, and the response will only contain one result in the JSON array
That result is parsed as a PassioFoodItem object, using Parsing from search routes mapping
The refCode is also parsed for extracting the boolean variable called
shortName
val responseItem = response.results.first()
val shortName = DataUtils.metadataHasShortName(refCode)
val foodItem = PassioFoodItem.fromSearchResponse(responseItem, shortName)
callback(foodItem)
fun metadataHasShortName(encoded: String): Boolean {
val decoded = Base64.decode(encoded, Base64.DEFAULT).toString(Charset.defaultCharset())
val jsonObject = JSONObject(decoded).optJSONObject("metadata")
return jsonObject?.optBoolean(SHORT_NAME) ?: false
}
Barcode result
MobileSDK: fetchFoodItemForProductCode
API: https://api.passiolife.com/v2/products/food/productCode/$upcCode?metadata=$encoded
The SDK will add the encoded metadata into the url of the request. The metadata contains a boolean key called shortName, used in the PassioFoodDataInfo
private const val SHORT_NAME = "shortName"
fun encodeMetadata(shortName: Boolean): String {
val metadata = """{"$SHORT_NAME": $shortName}"""
return Base64.encodeToString(metadata.toByteArray(), Base64.NO_WRAP)
}
The SDK adds the
Localization-ISO
header to the request if the locale code was set using the functionupdateLanguage
That result is parsed as a PassioFoodItem object, using Parsing from packaged product routes mapping
Last updated