Call Firebase database from Google App Engine

93 views
Skip to first unread message

widowmaker110

unread,
Jun 17, 2016, 9:02:29 PM6/17/16
to Google App Engine
I followed this tutorial for setting up my Google App Engine instance and I am also using Firebase. My goal is to put all of the "computation" on Google App Engine. I want to call a function like this one below:


MyEndpoint:

package productions.widowmaker110.backend;

/** An endpoint class we are exposing */
@Api(
 name
= "myApi",
 version
= "v1",
 
namespace = @ApiNamespace(
 ownerDomain
= "backend.widowmaker110.productions",
 ownerName
= "backend.widowmaker110.productions",
 packagePath
=""
 
)
)
public class MyEndpoint {

 
/** A simple endpoint method that takes a name and says Hi back */
 
@ApiMethod(name = "sayHi")
 
public MyBean sayHi(@Named("name") String name) {

 
// Write a message to the database
 
FirebaseDatabase database = FirebaseDatabase.getInstance();
 
DatabaseReference myRef = database.getReference("message");
 
 
// Read from the database
 myRef
.addValueEventListener(new ValueEventListener() {
 
@Override
 
public void onDataChange(DataSnapshot dataSnapshot) {
 
// This method is called once with the initial value and again
 
// whenever data at this location is updated.
 
String value = dataSnapshot.getValue(String.class);
 
Log.d(TAG, "Value is: " + value);
 
}

 
@Override
 
public void onCancelled(DatabaseError error) {
 
// Failed to read value
 
Log.w(TAG, "Failed to read value.", error.toException());
 
}
 
});

 
MyBean response = new MyBean();
 response
.setData("Hi, " + name);

 
return response;
 
}

}


MainActivity:

package productions.widowmaker110.gpsweather;

// imports...

public class MainActivity extends AppCompatActivity {

 
@Override
 
protected void onCreate(Bundle savedInstanceState) {
   
super.onCreate(savedInstanceState);
    setContentView
(R.layout.activity_main);

   
new EndpointsAsyncTask().execute(new Pair<Context, String>(this, "Manfred"));
 
}

 
class EndpointsAsyncTask extends AsyncTask<Pair<Context, String>, Void, String> {
   
private MyApi myApiService = null;
 
private Context context;

 
@Override
 
protected String doInBackground(Pair<Context, String>... params) {
   
if(myApiService == null) { // Only do this once
     
MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
     
new AndroidJsonFactory(), null)
     
// options for running against local devappserver
     
// - 10.0.2.2 is localhost's IP address in Android emulator
     
// - turn off compression when running against local devappserver
     
.setRootUrl("http://10.0.2.2:8080/_ah/api/")
     
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
 
@Override
 
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
    abstractGoogleClientRequest
.setDisableGZipContent(true);
 
}
 
});
 
// end options for devappserver

 myApiService
= builder.build();
 
}

 context
= params[0].first;
 
String name = params[0].second;

 
try {
 
return myApiService.sayHi(name).execute().getData();
 
} catch (IOException e) {
 
return e.getMessage();
 
}
 
}

 
@Override
 
protected void onPostExecute(String result) {
 
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
 
}
 
}
}

I understand the above code for Firebase is Android specific so running on a Google App Engine instance doesn't work. I was wondering if any one knows how to perform CRUD 
operations on a firebase database from Google App Engine backend. Any help is appreciated.

Adam (Cloud Platform Support)

unread,
Jun 19, 2016, 1:07:48 PM6/19/16
to Google App Engine
For this you'd want to look at the documentation for using Firebase from a 'Server'. The Firebase SDK supports both Java and NodeJS at this time.  You can also call the Firebase API from other App Engine languages by using the REST API directly (eg. directly calling the API over HTTP using App Engine's URL Fetch service).
Reply all
Reply to author
Forward
0 new messages