import analytics from "@react-native-firebase/analytics" ; import fetch from "cross-fetch" ; const baseUrl = "https://plantrecog.herokuapp.com/v1/" ; const FetchBuilder = ( route , conf ) => { return new Promise ( async ( resolve , reject ) => { try { const rawResp = await fetch ( baseUrl + route , conf ) ; const resp = await rawResp . json ( ) ; console . log ( `Service->PlantRecog->${ route } response:` , resp . message ) ; resolve ( resp ) ; } catch ( err ) { console . log ( `Service->PlantRecog->${ route } error:` , err . message ) ; await analytics ( ) . logEvent ( "plantrecog-api-error" , { message : err . message , } ) ; reject ( err ) ; } } ) ; } ; export const isServiceAvailable = ( ) => { return new Promise ( async ( resolve , reject ) => { try { const resp = await FetchBuilder ( "" , undefined ) ; resolve ( resp . payload ) ; } catch ( err ) { reject ( err ) ; } } ) ; } ; export const getFlowerImagePrediction = ( photoUri ) => { return new Promise ( async ( resolve , reject ) => { try { const imageToSend = { uri : photoUri , type : "image/jpeg" , name : "image.jpg" , } ; const body = new FormData ( ) ; body . append ( "image" , imageToSend ) ; const resp = await FetchBuilder ( "predict" , { headers : { "Content-Type" : "multipart/form-data" , } , method : "POST" , body , } ) ; resolve ( resp . payload ) ; } catch ( err ) { reject ( err ) ; } } ) ; } ;